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.
Related
id,date,source,target,identifier
1,2020-10-10,internal,external,abc-123
2,2020-10-10,internal,internal,xyz-123
3,2020-10-11,external,external,abc-123
4,2020-10-12,external,external,abc-123
There are three entries for the same record (abc-123) and I would like to filter out the oldest and the newest record. For all the records, if there are duplicates then I would like to get the oldest and newest record.
I have no idea how to construct such a query. Any help will be greatly appreciated.
You could use analytic functions here:
WITH cte AS (
SELECT *, MIN(date) OVER (PARTITION BY identifier) min_date,
MAX(date) OVER (PARTITION BY identifier) max_date
FROM yourTable
)
SELECT id, date, source, target, identifier
FROM cte
WHERE date IN (min_date, max_date);
The CTE above adds to your table two new columns for the min and max date per each identifier. The outer query then restricts to only records having those min or max dates.
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');
I am calculating Bounced sessions (sessions with only 1 pageview) via BQ.
Query is joining a table that gives me number of all sessions and a table that gives me bounced sessions.
When I run my query on just one specific date, my numbers match with the numbers in GA, but if I select bigger timeframe, for example a month, the numbers (only for Bounced sessions) are off.
Also, if I run each subquery separately, I get correct numbers for any timeframe.
Here is my query:
SELECT
A.date AS Date,
A.Landing_Content_Group AS Landing_Content_Group,
MAX(A.sessions) AS Sessions,
MAX(B.Bounced_Sessions) AS Bounced_Sessions
FROM (
SELECT
date,
hits.contentGroup.contentGroup2 AS Landing_Content_Group,
COUNT(DISTINCT CONCAT(CAST(visitStartTime AS string),fullVisitorId)) AS sessions
FROM
`122206032.ga_sessions_201808*`,
UNNEST(hits) AS hits
WHERE
hits.type="PAGE"
AND hits.isEntrance = TRUE
GROUP BY
date,
Landing_Content_Group
ORDER BY
date DESC,
sessions DESC ) A
LEFT JOIN (
SELECT
date,
hits.contentGroup.contentGroup2 AS Landing_Content_Group,
COUNT(DISTINCT CONCAT(CAST(visitStartTime AS string),fullVisitorId)) AS Bounced_Sessions
FROM
`122206032.ga_sessions_201808*`,
UNNEST(hits) AS hits
WHERE
hits.type="PAGE"
AND totals.pageviews = 1
AND hits.isEntrance = TRUE
GROUP BY
date,
Landing_Content_Group
ORDER BY
date DESC,
Bounced_Sessions DESC ) B
ON
a.Landing_Content_Group = b.Landing_Content_Group
GROUP BY
Date,
Landing_Content_Group
ORDER BY
Date DESC,
Sessions DESC
What I should get:
GA results
What I get in BQ for that date when a time frame is a month:
BQ results
I tried different JOINs and Aggregations but so far still in the unknown :)
Ok, I solved it, the solution was to also join the tables on the date.
ON
a.date = b.date
AND a.Landing_Content_Group = b.Landing_Content_Group
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') = ?