Average temperature by day of year with SQLite DDB - sqlite

I'm looking for a good solution to show the average temperature day by day of the year from a SQLite Database.
In my database, to be sample, I have a date column and a temp column, for each day since 5 years like that.
example
I would like to get, for each day of the year, the average temperature from my database.
I found the request to calculate the average for one day, but I don't how can I do like that for each day
SELECT avg(min) FROM historique WHERE strftime('%m-%d', date )= "04-01";
Could you help me please ?

You must use use GROUP BY:
SELECT strftime('%m-%d', date) day, avg(min)
FROM historique
GROUP BY day

Related

Create a comparison with more than one month in datastudio

Good morning to all,
I would need your help of the most professional!
In my datastudio I have a column MONTH and I would like to create an automatism, where I can take the column MONTH -1 and based on the previous month, and once done this take into account the column RATING and return the value.
I used this formula but it doesn’t work:
WHEN Month < DATETIME_TRUNC(TODAY(), MONTH) AND Month >= DATETIME_TRUNC(DATETIME_SUB(TODAY(), INTERVAL 1 MONTH), MONTH) THEN 1 "
can you help me?
I would like to take the case Month and then take into account the previous Month and on this basis calculate the rating

SQL: Chunking hourly samples into 7-day averages

GIVENS:
Tools: SQL Server, SSMS 2016, R
Data: Hourly samples starting 2017-12-31 23:00:00 thru 2021-02-05 08:00:00
WANT: To chunk data into 7-day blocks ideally coinciding with week of year and grab averages for each 7-day period. Willing to sacrifice some data frontend and/or backend. Would like to reduce data frequency from 12x365 points down to perhaps 52 points per year. For end use in R.
PROBLEM(S):
A) SQL datepart(week,...) method does not consider 1st seven days of 2018 as week 1. Considers that week starts on a certain day of week, not necessarily on Jan. 1.
B) I suspect SQL datepart(week,...) will assign repeating week value across several years of data. So if I group by datepart(week...), won't it combine week 1 of 2018, 2019, 2020, 2021?
Here's my starting query (AvgDate is for debug purposes):
SELECT datepart(week,Date) Week,
FORMAT(AVG(HeadElev), '###.###') as AvgHeadEl,
COUNT(HeadElev) as Count,
FORMAT(AVG(datepart(Day, Date)), '##.###') as AvgDate
FROM [dbo].[Chickamauga] as CWL
WHERE '20171231' < Date AND Date <= '20181231'
GROUP BY datepart(week,Date)
ORDER BY Week
GO
Here's what my table looks like (I've split date & time from original data):
CREATE TABLE [dbo].[SomeLake](
[Date] [date] NULL,
[HourCT] [time](0) NULL,
[HeadElev] [float] NULL,
[TailElev] [float] NULL,
[Flow] [float] NULL
) ON [PRIMARY]
Again, trying to create simple 7-day blocks of samples and grab averages. (Not moving averages, I only want 1 data point per 7-day block.) I'm trying to reduce the data frequency from (hourly down to weekly data.)
End goal is to import into R and used time series functions that cannot accept high per year frequencies like 365. Trying to bring the frequency down to 52, ie. weekly data.)
THANK YOU for your kind assistance!
create simple 7-day blocks of samples and grab averages.
Group by something like:
1+datepart(dy,some_date)/7 week
which takes the day-of-year and performs integer division to group them into 7-day buckets, starting with 0.

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');

Apache Drill: Group by week

I tried to group my daily data by week (given a reference date) to generate a smaller panel data set.
I used postgres before and there it was quite easy:
CREATE TABLE videos_weekly AS SELECT channel_id,
CEIL(DATE_PART('day', observation_date - '2016-02-10')/7) AS week
FROM videos GROUP BY channel_id, week;
But it seems like it is not possible to subtract a timestamp with a date string in Drill. I found the AGE function, which returns an interval between two dates, but how to convert this into an integer (number of days or weeks)?
DATE_SUB may help you here. Following is an example:
SELECT extract(day from date_sub('2016-11-13', cast('2015-01-01' as timestamp)))/7 FROM (VALUES(1));
This will return number of weeks between 2015-01-01 and 2016-11-13.
Click here for documentation

SQLite : obtain current week

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.

Resources