I have 5 years of wind data with values recorded every 10 minutes. I have to get the average, min and max of some columns of every month, what I´ve done is
SELECT avg(S10_AVG), MIN(S10_AVG), max(S10_AVG)
FROM CP
WHERE strftime('%Y', FECHA)='2008' and strftime('%m', FECHA)='01'
Is it possible to make this for every month and year without doing 60 different queries? Thanks
Yes, you could use GROUP BY:
SELECT strftime('%Y', FECHA), strftime('%m', FECHA), avg(S10_AVG), MIN(S10_AVG), max(S10_AVG)
FROM #WindData
GROUP BY strftime('%Y', FECHA), strftime('%m', FECHA)
SELECT
STRFTIME('%Y', fecha) AS year,
STRFTIME('%m', fecha) AS month,
AVG(s10_avg) AS avg,
MIN(s10_avg) AS min,
MAX(s10_avg) AS max
FROM cp
GROUP BY STRFTIME('%Y', fecha), STRFTIME('%m', fecha)
Related
I have two time stamps #starttimestamp and #endtimestamp. How to calculate number of working hours between these two
Working hours is defined below:
Mon- Thursday (9:00-17:00)
Friday (9:00-13:00)
Have to work in impala
think i found a better solution.
we will create a series of numbers using a large table. You can get a time dimension type table too. Make it doenst get truncated. I am using a large table from my db.
Use this series to generate a date range between start and end date.
date_add (t.start_date,rs.uniqueid) -- create range of dates
join (select row_number() over ( order by mycol) as uniqueid -- create range of unique ids
from largetab) rs
where end_date >=date_add (t.start_date,rs.uniqueid)
Then we will calculate total hour difference between the timestamp using unix timestamp considering date and time.
unix_timestamp(endtimestamp - starttimestamp )
Exclude non working hours like 16hours on M-T, 20hours on F, 24hours on S-S.
case when dayofweek ( dday) in (1,7) then 24
when dayofweek ( dday) =5 then 20
else 16 end as non work hours
Here is complete SQL.
select
end_date, start_date,
diff_in_hr - sum(case when dayofweek ( dday) in (1,7) then 24
when dayofweek ( dday) =5 then 20
else 16 end ) total_workhrs
from (
select (unix_timestamp(end_date)- unix_timestamp(start_date))/3600 as diff_in_hr , end_date, start_date,date_add (t.start_date,rs.uniqueid) as dDay
from tdate t
join (select row_number() over ( order by mycol) as uniqueid from largetab) rs
where end_date >=date_add (t.start_date,rs.uniqueid)
)rs2
group by 1,2,diff_in_hr
CREATE TABLE FiveMinutesData (
TimeStamp datetime NOT NULL,
V_Ph1 float, V_Ph2 float, V_Ph3 float,
P_Ph1 float, P_Ph2 float, P_Ph3 float,
P_Ph1_Day float, P_Ph2_Day float, P_Ph3_Day float,
Flags int,
PRIMARY KEY (TimeStamp)
);
sqlite> select min(timestamp), max(timestamp) FROM fiveminutesdata group by strftime('%Y',datetime(TimeStamp,'localtime'));
1290948000|1647001800
sqlite> select min(timestamp), max(timestamp) FROM fiveminutesdata group by strftime('%Y',datetime(TimeStamp,'unixepoch'));
1290948000|1293812700
1293873900|1325347800
1325410500|1356970500
1357032600|1388507700
1388565900|1420070100
1420070700|1451606100
1451606400|1483228500
1483228800|1514764500
1514764800|1546300500
1546300800|1577836500
1577836800|1609458900
1609459200|1640994600
1640997300|1647001800
It looks that the yearly grouping with , when using localtime, is grouping everything in on line.
Is there an explanation for this behavior?
A work around ?
The column timestamp contains integer values which are unix epoch values, so you have to use the modifier 'unixepoch' and 'localtime':
SELECT strftime('%Y', datetime(timestamp, 'unixepoch', 'localtime')) year,
MIN(timestamp) min_timestamp,
MAX(timestamp) max_timestamp
FROM fiveminutesdata
GROUP BY year;
Or, simpler:
SELECT strftime('%Y', timestamp, 'unixepoch', 'localtime') year,
MIN(timestamp) min_timestamp,
MAX(timestamp) max_timestamp
FROM fiveminutesdata
GROUP BY year;
I have a db with many years of ambient temperature, I need to make the average temperature from every month from 06:00 to 20:00 (day) and night. What I already made is
SELECT
STRFTIME('%Y', fecha) AS year,
STRFTIME('%m', fecha) AS month,
AVG(T10_AVG) AS Temp_prom,
FROM GER
GROUP BY STRFTIME('%Y', fecha), STRFTIME('%m', fecha)
But I have no idea how to include the day and night averages.
If you want to get those averages in the same monthly output record, you cannot add another GROUP BY column.
You have to compute these values with separate subqueries:
SELECT strftime('%Y', fecha) AS year,
strftime('%m', fecha) AS month,
(SELECT AVG(T10_AVG)
FROM Ger AS g2
WHERE g2.fecha GLOB strftime('%Y-%m*', Ger.fecha)
AND (g2.hour >= '06' AND g2.hour < '20')
) AS avg_day,
(SELECT AVG(T10_AVG)
FROM Ger AS g2
WHERE g2.fecha GLOB strftime('%Y-%m*', Ger.fecha)
AND (g2.hour < '06' OR g2.hour >= '20')
) AS avg_night,
AVG(T10_AVG) AS Temp_prom
FROM Ger
GROUP BY year, month
Hi my code doesn't work, Im trying to group my blogentries by year and month here's my sql
SELECT * FROM(
SELECT WP_BlogEntries.BlogEntryID, WP_BlogEntries.AddedDate,
WP_BlogEntries.AddedBy, WP_BlogEntries.BlogID,
WP_BlogEntries.Title, WP_BlogEntries.Description, WP_BlogEntries.Body,
WP_BlogEntries.ReleaseDate, WP_BlogEntries.ExpireDate,
WP_BlogEntries.Approved, WP_BlogEntries.Listed,
WP_BlogEntries.CommentsEnabled, WP_BlogEntries.OnlyForMembers,
WP_BlogEntries.ViewCount, WP_BlogEntries.Votes,
WP_BlogEntries.TotalRating
FROM WP_BlogEntries
WHERE WP_BlogEntries.ReleaseDate < GETDATE()
AND WP_BlogEntries.ExpireDate > GETDATE()
AND Approved = 1
AND Listed = 1
AND WP_BlogEntries.BlogID = #BlogID) MonthEntries
GROUP BY YEAR(ReleaseDate), MONTH(ReleaseDate)
It would be helpful to know the error message.
You can't do a SELECT * FROM if you specify GROUP BY.
The only valid columns are those in the GROUP BY or an aggregate function.
If you group by year and month, then each row will contain one year and month, it is impossible for SQL to know which other columns to display as there could be more than one. (e.g. two blog entries in one month)
Did you mean to ORDER BY instead?
Simething like This :
select convert(varchar(50),YEAR(date)) +'/'+convert (varchar(50), MONTH(date)) ,Name , COUNT( Name) ,[DATE] from table1
group by convert(varchar(50),YEAR(date)) +'/'+convert (varchar(50), MONTH(date)) ,Name,[date]
order by [Date] Desc
I use the sql below in sqlite to group by time intervals. The first is group by day and the second group by hour:
select strftime('%Y-%m-%dT%00:00:00.000', date_time),line, count() from entry group by strftime('%Y-%m-%dT%00:00:00.000', date_time)
select strftime('%Y-%m-%dT%H:00:00.000', date_time),line, count() from entry group by strftime('%Y-%m-%dT%H:00:00.000', date_time)
How do I group by 10 minutes interval
select ... from entry
group by strftime('%Y%m%d%H0', date_time) + strftime('%M', date_time)/10;