I have a Sqlite3 database, with a reports table, the table has an date INTEGER field, it stores Unix timestamps, I want to make two choices:
CREATE TABLE reports (id INTEGER PRIMARY KEY, user_id, report, date INTEGER);
SELECT * FROM reports;
1|123456|report|1546965098
1.All entries for the previous month;
2.All entries from the first day of this month to today.
I tried it, but it did not work out.
SELECT * FROM reports WHERE datetime(date, 'unixepoch') >= date('now', '-1 month);
I understand that something is lacking, but unfortunately, not enough knowledge of sql.
Tell me, please, how to make such records?
To get the 1st day of the previous month, you need this:
date('now','start of month','-1 month')
so you want dates starting from the above date up to and not including the 1st day of the current month, which is:
date('now','start of month')
So your statement to get the rows of the previous month can be:
SELECT *
FROM reports
WHERE
datetime(date, 'unixepoch') >= date('now','start of month','-1 month')
AND
datetime(date, 'unixepoch') < date('now','start of month');
The 2nd statement is simpler:
SELECT *
FROM reports
WHERE
datetime(date, 'unixepoch')
BETWEEN
datetime('now', 'start of month') AND datetime('now', 'localtime');
Related
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.
I have an sqlite table as follow:
I have to select the rows where 'start'< now time in order to have just the ongoing program; logically as:
if time now is 10:05 then the result should be:
How I can formulate the right select statement?
Thank u in advance for any help.
You can use the function strftime() to get the current time in the format HH:MM so that you can compare it to the column start:
SELECT *
FROM tablename
WHERE start < strftime('%H:%M', 'now');
If you want only the last row for each dpt:
SELECT *
FROM tablename
WHERE start < strftime('%H:%M', 'now')
GROUP BY dpt
HAVING MAX(start);
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.
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') = ?
I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.