I am trying to find a certain field(timestamp) for today to send in a report. The code I have been using only returns back 4 results. I do not think that is right. I was thinking about using a wildcard for the timestamp seconds,milliseconds part...I don't want the seconds part hence my current code, I just want the results for the current date. I also tried select current_timestamp(processed_time) as now; to no avail.
SELECT *
FROM `prod.ods_simp.audit`
WHERE DATE(processed_time) = DATE(TIMESTAMP('2020-04-06')) or date(next_transmission) = DATE(TIMESTAMP('2020-04-06'))
LIMIT 1000;
Moving from comments to answer for closing:
Instead of DATE(TIMESTAMP('2020-04-06')) you can save code and time with DATE('2020-04-06').
And even better and shorter:
WHERE DATE(processed_time) = '2020-04-06'
(BigQuery auto-casts to DATE in this case)
Related
This sounds like a very naive question but I can not get this simple requirement to work. A selection is made on one page of the website which passes a parameter time_period (which is 1, 7, 31 or 365) to the next page which should list all of the selected records where the LastUpdated date (a standard date field in Access db) is within that many days of the current date.
With single digit numbers (1 and 7) I get a result, but not 100% accurate and with 31 and 365 only very strange results. I guess there is an inconsistency in formats somewhere but I am at a loss to find a solution.
Dim my_time_period
my_time_period = Request.QueryString("time_period")
selection_list.Source = "SELECT * FROM document WHERE DateDiff('d',LastUpdated,now) <= '"+my_time_period+"'"
I have tried hard coding a number as below but get the same results.
selection_list.Source = "SELECT * FROM document WHERE DateDiff('d',LastUpdated, now) <= '7'"
I have also tried using Today instead of now but it gets thrown out as an error. Can anyone please help? Thank you
You're comparing a number
DateDiff('d',LastUpdated, now)
to a string
'7'
Instead compare a number to a number:
"SELECT * FROM document WHERE DateDiff('d',LastUpdated,now) <= "+my_time_period
Note the difference: no single quotes
Also be aware this is inviting SQL Injection.
Also be aware that in a database that uses indexes, this expression should be rewritten something like this:
SELECT * FROM document WHERE LastUpdated > DateAdd('d',now,-"+my_time_period+")"
That way you're comparing a column (which may be indexed) to a fixed value. It doesn't matter so much in MS Access though
I have a list of items I'd like to view by the date they most recently occurred. I am currently using this query.
SELECT Cleaning1, Max(Date1) AS most_recent
FROM CleaningLog
GROUP BY Cleaning1;
It worked yesterday when I was at work, but suddenly stopped working. It won't show to most recent date an event occurred for some of the items. I was wondering why this would suddenly stop working. I added date parameters to the four queries I have with this code. When that stopped working correctly, I decided to create a test query, without date parameters, and it still won't show me the most recent event that I typed in.
I tried playing around with the <, >, = signs in the date portion of the code, but nothing seems to capture the dates I added this morning. I should mention this is what I have for codes that have a date parameter:
SELECT Cleaning1, Max(Date1) AS most_recent
FROM CleaningLog
GROUP BY Cleaning1
HAVING Max(Date1) < Now() - 30;
What do you think would be a potential cause for this query to stop capturing dates? The dates in my database are not in chronological order, because I had to manually type in events that occurred in the past. Any help would be appreciated!
It seems like it won't use the data after ID 89. It worked fine yesterday afternoon. I added from ID 59 on this morning. All of the data was added the same way, through a form:
Convert your text date to a true date:
SELECT
Cleaning1,
Max(CDate(Date1)) AS most_recent
FROM
CleaningLog
GROUP BY
Cleaning1
HAVING
Max(CDate(Date1)) < Date() - 30;
I am trying to use the LIKE function to get data with similar names. Everything looks fine but the data I get in return is missing some values when I get back more than ~20 rows of data.
I have a very basic query. I just want data that starts with Lab, ideally for the whole day, or at least 12 hours. The code below misses some data and I cannot discern a pattern for what it picks to skip.
SELECT History.TagName, DateTime, Value FROM History
WHERE History.TagName like ('Lab%')
AND Quality = 0
AND wwRetrievalMode = 'Full'
AND DateTime >= '20150811 6:00'
AND DateTime <= '20150811 18:00'
To give you an idea of the data I am pulling, I have Lab.Raw.NTU, Lab.Raw.Alk, Lab.Sett.NTU, etc. Most of the data should have values at 6am/pm, 10am/pm, and 2am/pm. Some have more, few have less, not important. When I change the query to be more specific (i.e. only 1 hour window or LIKE "Lab.Raw.NTU") I get all of my data. Currently, this will spit out data for all tags and I get both 6am data and 6pm data, but certain values will be missing such as Lab.Raw.NTU at 6pm. There seem to be other data that is missing if I change the window for the previous day or the night shift, so I don't think it has to be with the data itself. Something weird is going on with the LIKE function but I have no idea what.
Is there another way to get the tagnames that I want besides like? Such as Tagname > Lab and Tagname <= Labz? (that gives me an error, so I am thinking not)
Please help.
It appears that you are using the Like operator correctly; that could be a red herring. Check the data type of the DateTime field. If it is character based such as varchar you are doing string comparisons instead of date comparisons, which could cause unexpected results. Try doing an explicit cast to ensure they are compared as dates:
DateTime >= convert(datetime, '20150811 6:00')
suppose I have a table MyTable with a column some_date (date type of course) and I want to select the newest 3 months data (or x days).
What is the best way to achieve this?
Please notice that the date should not be measured from today but rather from the date range in the table (which might be older then today)
I need to find the maximum date and compare it to each row - if the difference is less than x days, return it.
All of this should be done with sqlalchemy and without loading the entire table.
What is the best way of doing it? must I have a subquery to find the maximum date? How do I select last X days?
Any help is appreciated.
EDIT:
The following query works in Oracle but seems inefficient (is max calculated for each row?) and I don't think that it'll work for all dialects:
select * from my_table where (select max(some_date) from my_table) - some_date < 10
You can do this in a single query and without resorting to creating datediff.
Here is an example I used for getting everything in the past day:
one_day = timedelta(hours=24)
one_day_ago = datetime.now() - one_day
Message.query.filter(Message.created > one_day_ago).all()
You can adapt the timedelta to whatever time range you are interested in.
UPDATE
Upon re-reading your question it looks like I failed to take into account the fact that you want to compare two dates which are in the database rather than today's day. I'm pretty sure that this sort of behavior is going to be database specific. In Postgres, you can use straightforward arithmetic.
Operations with DATEs
1. The difference between two DATES is always an INTEGER, representing the number of DAYS difference
DATE '1999-12-30' - DATE '1999-12-11' = INTEGER 19
You may add or subtract an INTEGER to a DATE to produce another DATE
DATE '1999-12-11' + INTEGER 19 = DATE '1999-12-30'
You're probably using timestamps if you are storing dates in postgres. Doing math with timestamps produces an interval object. Sqlalachemy works with timedeltas as a representation of intervals. So you could do something like:
one_day = timedelta(hours=24)
Model.query.join(ModelB, Model.created - ModelB.created < interval)
I haven't tested this exactly, but I've done things like this and they have worked.
I ended up doing two selects - one to get the max date and another to get the data
using the datediff recipe from this thread I added a datediff function and using the query q = session.query(MyTable).filter(datediff(max_date, some_date) < 10)
I still don't think this is the best way, but untill someone proves me wrong, it will have to do...
Could somebody recommend the query to retrieve records up to today or certain dates?
I'm required to produce an Oracle report where user needs to enter a date and records up to that date will be shown.
I tried
select * from the_table where the_date <= sysdate
However it seems to produce an inaccurate result. What is the better query for this. For now I'm just playing around with sysdate. Later I will need to use a certain date keyed in by the user and all the records up to that date needs to be shown.
Any suggestions?
Sometimes you get inaccurate records because of little differences like minutes and seconds when two dates have the same day/month/year. Try the following
select * from the_table where TRUNC(the_date) <= sysdate
The TRUNC removes the minute and the seconds. Sometimes you get inaccurate records without using that