SQLite strftime Group By - sqlite

I have a table consisting of a date field and a barcode field; I want the number of barcodes grouped by day for the previous month.
This looked like it would work:
SELECT
COUNT(*) AS count,
strftime('%d-%m-%Y',date) AS day
FROM barcodes
WHERE date >= datetime('now', '-1 month')
GROUP BY day
ORDER BY date ASC;
But that gives me incorrect counts. E.g.:
341|30-01-2017
274|31-01-2017
288|01-02-2017
332|02-02-2017
224|03-02-2017
35|04-02-2017
1009|06-02-2017
1481|07-02-2017
1626|08-02-2017
507|09-02-2017
428|10-02-2017
125|11-02-2017
1838|13-02-2017
2591|
Whereas:
SELECT COUNT(*) FROM barcodes WHERE date LIKE '2017-02-10%';
579
If I do this:
SELECT
COUNT(*) AS count,
strftime('%d-%m-%Y',date) AS day
FROM barcodes
WHERE date LIKE '2017-02-10%'
GROUP BY day
ORDER BY date ASC;
I get:
428|10-02-2017
151|
So my question is: why is SQLite providing the result as two lines when I use strftime()?

%d-%m-%Y is not one of the supported date formats, so comparisons do not work correctly, and any of the built-in date functions will return NULL.

Related

How to add time to the output of this max/min select?

I have a simple sqlite3 database for recording temperatures,the database schema is trivially simple:-
CREATE TABLE temperatures (DateTime, Temperature);
To output maximum and minimum temperatures over 1 month I have the following query:-
SELECT datetime, max(temperature), min(temperature) from temperatures
WHERE datetime(DateTime) > datetime('now', '-1 month')
GROUP BY strftime('%d-%m', DateTime)
ORDER BY DateTime;
How can I get the times for maxima and minima as well? Does it need a sub-query or something like that?
Use window functions MIN(), MAX() and FIRST_VALUE() instead of aggregation:
SELECT DISTINCT date(DateTime) date,
MAX(temperature) OVER (PARTITION BY date(DateTime)) max_temperature,
FIRST_VALUE(time(datetime)) OVER (PARTITION BY date(DateTime) ORDER BY temperature DESC) time_of_max_temperature,
MIN(temperature) OVER (PARTITION BY date(DateTime)) min_temperature,
FIRST_VALUE(time(datetime)) OVER (PARTITION BY date(DateTime) ORDER BY temperature) time_of_min_temperature
FROM temperatures
WHERE datetime(DateTime) > datetime('now', '-1 month')
ORDER BY date;
If your DateTime column contains values in the ISO format YYYY-MM-DD hh:mm:ss there is no need for datetime(DateTime).
You can use directly DateTime.

Group by on Month in Oracle

I want group by the result data of oracle database. And also I got the result but the result is groupped as month start to next month start.
I need group by from month start to month end.
"GROUP BY TO_CHAR(COL_DATE,'MON-YYYY')"
As I am getting data from 01-Feb-2018 to 01-Mar-2018.
Required data from 01-Feb-2018 to 28-Feb-2018.
use the TRUNC function.
the following example shows the number of entries per month
SELECT TRUNC(COL_DATE, 'MONTH') AS MONTH, COUNT(*)
FROM TABLE
GROUP BY TRUNC(COL_DATE, 'MONTH');

SQLite strftime() weekday

I have been trying with no success to to count how many values were created in a specific week day:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn) = '1';
I have this values in timeIn
1472434822.60033
1472434829.12632
1472434962.34593
I don't know what I am doing wrong here.
furthermore, if I use this:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn) = '6';
I get
2
which makes no sense. Thank you in advance.
You appear to be storing the date as the number of seconds since 1970 (the Unix epoch) - a common representation. The time strings accepted by the SQLite date functions (see the Time Strings section) default to interpreting numeric time strings as a Julian day numbers:
Similarly, format 12 is shown with 10 significant digits, but the date/time functions will really accept as many or as few digits as are necessary to represent the Julian day number.
You can see this with the following SELECT:
SELECT strftime('%Y-%m-%d', 1472428800.6) AS t
the result of which is:
4026-48-26
For your date representation to be interpreted as a Unix epoch, you need to include 'unixepoch' in the strftime call:
SELECT strftime('%Y-%m-%d', 1472428800.6, 'unixepoch') AS t
which returns:
2016-08-29
If you modify your SELECT to be:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn, 'unixepoch') = '6'
you should see results more inline with your expectations.

SQLite - Returning records in a specific month and year in date

I'm trying to get the records with a specific month and year like this:
SELECT * from table where strftime('%m', date) = '?'
if I test this query:
SELECT strftime('%m', date) from table
it return 19, but there's only records with may in month, so I thought the result was 5, but it's 19! Why?
What's wrong with my query? How can I return specific records using a specific value for month and year, linke 5 (may) and 2015
Milliseconds is not one of SQLite's supported date formats.
You have to convert these values into some supported format first (here: Unix timestamp):
SELECT * FROM MyTable WHERE strftime('%m', date / 1000, 'unixepoch') = ?

Compare date columns

I need to retrieve the rows that service_date is greater than prod_date.
The data type for prod_date is VARCHAR(10) (2/20/2014 for example) and the data type for service_date is DATE (YYYYMMDD). If I query service_date using "select service_date from service where service_date ='20140201'", the result is showing "2/1/2014" in the result grid. However, it does not work in the query below when I convert service_date to varchar to compare with prod_date. It pulls out all the rows instead of the ones that have greater service_date.
SELECT P.PROD_ID, P.PROD_DESC, S.PROD_ID, S.SERVICE_LOC
FROM PRODUCT P
INNER JOIN SERVICE S
WHERE P.PROD_ID = S.PROD_ID
AND CAST(S.SERVICE_DATE AS VARCHAR(10)) >= P.PROD_DATE
I suggest you use date ordering instead of string/varchar ordering if possible for simplicity and since its [ probably ] closer to what your interested in and less likely to confuse
For example
'01/02/2014' >= '04/01/2013' -- if these are dates or cast to dates
but
'01/02/2014' < '04/01/2013' -- if these are strings
So to keep things simple, it makes sense to cast PROD_DATE to a date when comparing these two fields like :
SELECT P.PROD_ID, P.PROD_DESC, S.PROD_ID, S.SERVICE_LOC
FROM PRODUCT P
INNER JOIN SERVICE S
WHERE P.PROD_ID = S.PROD_ID
AND S.SERVICE_DATE >= cast(P.PROD_DATE as date format 'DD/MM/YYYY')
;
if theres any doubts on prod_dates quality as valid dates can check the conversion on all dates first ( before running/adjusting above )
This isn't 100% error proof given your date is character and could have unexpected values. It does show how you can append a leading 0 to the month value and cast it to a date when the month is determined to be a single digit based on the location of the / in the second position of the PROD_DATE value for a given row.
SELECT CASE WHEN POSITION('/' IN TRIM(P.PROD_DATE)) = 2
THEN CAST('0'|| TRIM(P.PROD_DATE) AS CHAR(10)) AS DATE FORMAT 'MM/DD/YYYY')
ELSE CAST(P.PROD_DATE AS DATE FORMAT 'MM/DD/YYYY')
END AS PROD_DATE_
FROM PRODUCT P;

Resources