DATE COMPARISON in GOOGLE APP ENGINE DATASTORE / PYTHON - google-app-engine

I am trying to fetch records after a certain date.
I used the below in code:
qstr = "SELECT * FROM Comment where date > '"+str(max_date)+"' order by date desc limit 10"
comments = db.GqlQuery(qstr)
I have the console log I have qStr as follows:
SELECT * FROM Comment where date > '2013-03-07 04:33:31' order by date desc limit 10"
But this does not yield any records (There are records in the DB).
I also tried passing as date time:
comments = db.GqlQuery("SELECT * FROM Comment where date > :1 order by date desc limit 10",
miscUtils._datetime_from_str(max_date))
This also does not yield any results. Can you please let me know as what I'm doing wrong?
(I'm using the following code to convert date string to date http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/)
Also, I tried debugging the statement in App Engine console, and I was not able to do so.
Thanks in advance

I got it to work as follows:comments = db.GqlQuery("select * from Comment where date > :1 order by date desc limit "+MAX_COMMENTS_PER_FETCH_STR, datetime.datetime.strptime(max_date, "%Y-%m-%d %H:%M:%S").date())
Thanks everyone for your help

Related

How to limit the result data in flexible seach query

I want to limit the result data in flexible search query.
Let's say query should return only 10 records each time (like LIMIT)
How can I do this?
You already answered your query, you can use LIMIT cause same as we use in MySQL.
Try this
SELECT * FROM {Product} LIMIT 10
or
SELECT TOP 10 * FROM {Product}
For Oracle
SELECT * FROM {Product} WHERE rownum <= 10
Though API
final FlexibleSearchQuery query = new FlexibleSearchQuery("SELECT * FROM {Product}");
query.setCount(10);
Find more detail in helphybris
Use the query.setCount(int) method of the API.

How to identify missing weeks using SQL code?

I am trying to identify specific weeks that are missing in my data. I have multiple states with different date ranges and would like to output all of the missing weeks for the various states that I do have. Not even sure where to begin with a SQL code that would begin to identify missing weeks. Any help on this would be greatly appreciated. BTW I'm using SQL Server 2016.
Thanks!
Sample Data:
State WeekEndingDate Sales
ID 7/5/2015 125000
ID 12/13/2015 127263
IN 8/20/2016 126589
IN 8/27/2016 124568
IN 10/15/2016 119654
MI 01/02/2017 105687
MI 02/05/2017 145962
An example of my desired output would be:
MI 01/09/2017 136589
MI 01/16/2017 125641
MI 01/23/2017 145769
MI 01/30/2017 135697
IN 09/03/2016 145693 and so on....
This is what's commonly known as a "gaps and islands" problem.
To solve, I strongly suggest you create a date table. Then, assuming that you have established that your week ending day is always Saturday, you can include that date table in the query.
select MD.state,DT.weekendingdate,MD.sales
from DateTable DT
left outer join MyData MD on DT.Weekendingdate = MD.Weekendingdate
where dt.weekendingdate >= '2016-08-01' and dt.weekendingdate <= '2016-08-31'

Date error in DateProperty() save via ndb

Strange results on a saved date in GAE (Python with ndb library).
The inbound string from a web form is %a %m/%d/%y formatted (Fri 3/3/17).
That's parsed with datetime.strptime to get a date value.
When it's saved in a DateProperty() field, the value is consistently the day before, at 16:00:00.000 PST.
postDate = datetime.datetime.strptime(self.request.get('date-'+
hashed_id),'%a %m/%d/%y')
logging.info('postDate as Date: %s',postDate)
postDateStr = datetime.datetime.strftime(postDate,'%D')
logging.info('postDateStr: %s',postDateStr)
thisPost = ScheduledPost(id = postID,
...
postDate = postDate,
postDateStr = postDateStr
)
log results:
postDate as Date: 2017-03-03 00:00:00
postDateStr: 03/03/17
so far so good, right? but in the Datastore interface, that record shows:
PostDate: 2017-03-02 (16:00:00:000 PST)
PostDateStr: 03/03/17
Oops.
Workstation is in Pacific time - but leaving that aside - date queries seem to confirm that the date is wrong. Assuming today is 3/3/17 -
today = dt.datetime.now()
ScheduledPost.query(ScheduledPost.postDate == today).fetch()
No record returned.
Saving date as string, and querying on date as string, are feasible workarounds for this project. Just thought it merited posting - anyone seen this? Advice?
The datetime you're generating on appengine is UTC, and it gets stored as that datetime, without timezone information.
When you're viewing it, it's being converted to pacific-time.
The query you're doing is incorrect: you're generating a datetime, not a date, so the time you're comparing with is going to be different.

Assign date in salesforce.

I have written a batch job. In this batch job i have a condition to take date > = 1/1/2012
I am not able to give the date in the query. can you please help me.
global Database.QueryLocator start(Database.BatchableContext BC) {
system.debug('Inside start');
//date mydate = date.valueof(1/1/2012);
return Database.getQueryLocator('select name from opportunity');
}
I have given it in 2 ways
1st is :
took the date into a date field and give the condition as date >= :mydate
(shows an error in debug log as Invalid date: 0 )
and
2nd way when i gave the date their itself as date >=: 1/1/2012
(it shows as error in debug log as unexpected token: / )
can you please help me
Thanks
Anu
You must follow the proper date format
YYYY-MM-DD
select name from opportunity where mydate >= 2012-01-01
More information here

PostgreSQL Select Query for Date

I have a table in Postgres database "logs" which holds the error logs with their creation date
sample query : Select creation_date from logs
returns
"2011-09-20 11:27:34.836"
"2011-09-20 11:27:49.799"
"2011-09-20 11:28:04.799"
"2011-09-20 11:28:19.802"
I can find out the latest error using the command
SELECT max(creation_date) from log;
which will return "2012-02-06 12:19:28.448"
Now what I am looking for a query which could return the errors occured in last 15 minutes.
Any help on this will be appreciated
This should do the trick:
SELECT * FROM logs WHERE creation_date >= NOW() - INTERVAL '15 minutes'

Resources