How to write SQL to query avg of data from timestamp every hour of the day - sql

I have Climate data stored in table such as (Temperature,Humidity,CO2,Save_Timestamp) in realtime.
How can i write a sql to select average of data by every hour of the day
because when i do full select and render it on html5 with Chart.js
It's BOOM!!

try Something like this
For avg for current date by hour:
select hour(Save_Timestamp) HourSave,
avg(Temperature) avgTemperature, avg(Humidity) avgHumidity, avg(CO2) avgCO2
from yourtable
where date(Save_Timestamp)=current date
group by hour(Save_Timestamp)
For avg for all date by hour:
select date(Save_Timestamp) DateSave, hour(Save_Timestamp) HourSave,
avg(Temperature) avgTemperature, avg(Humidity) avgHumidity, avg(CO2) avgCO2
from yourtable
group by date(Save_Timestamp) , hour(Save_Timestamp)

SELECT CONVERT(VARCHAR(10),Save_Timestamp,112) AS Date,
DATEPART(hh,Save_Timestamp) AS Hour,
SUM(TEMPERATURE)/COUNT(*) AS AvgTemp
FROM CLIMATE_TABLE
GROUP BY
CONVERT(VARCHAR(10),Save_Timestamp,112),
DATEPART(hh,Save_Timestamp)
This might get you what you are looking for.

Related

Bigquery full date (year-month-day) to (year-month)

a noob question.
I want to query my database looking for pageviews for a given page, and i wrote a query that returns the page / number of pageviews daily. How i should change my query to get the same statistics but not daily but mothly?
So instead:
page pv date
/mysite 10 2017-01-01
get
page pv date
/mysite 500 2017-01
my query:
select
date,
hits.page.pagePath as pagePath,
count(totals.pageviews) as pageViews
from Table_DATE_RANGE ([818251235.ga_sessions_] , Timestamp('2016-01-01'), Timestamp('2017-11-01'))
group by 1,2
It's not clear what you are trying to count in your original query, but here is a query that uses standard SQL and performs the grouping on a monthly basis:
#standardSQL
SELECT
DATE_TRUNC(PARSE_DATE('%Y%m%d', date), MONTH) AS month,
hit.page.pagePath,
‎COUNT(*)
FROM `818251235.ga_sessions_*`,
UNNEST (hits) AS hit
WHERE _TABLE_SUFFIX BETWEEN
'20160101' AND '20181101'
GROUP BY 1, 2;
Edit: fixed to use DATE_TRUNC instead of EXTRACT(MONTH FROM ...) since both the year and month are relevant.
you can use date functions like UTC_USEC_TO_MONTH, UTC_USEC_TO_WEEK, UTC_USEC_TO_DAY to normalize them to the first day of the month, first day of the week.
select
date(UTC_USEC_TO_MONTH(date)) as monthly,
.....

Grouping with SQL using Max, Min, Avg and dates

I have a table as shown above, every 10 minutes a record for each ChannelID is put in the table.
I want to get the max, min and avg for each day for each channel, is this possible?
I am using MS SQL Server 2012
It looks like you know what to do, so I'm guessing the only thing you are missing is that you need to trunc your time value from your date, so each day will be recognized as a group. You can do it by using CAST AS DATE
SELECT cast([dateTime] as date) as Your_Date,ChannelID,
max(reading) as max_reading,
min(reading) as min_reading,
avg(reading) as avg_reading
FROM YourTable
GROUP BY cast([dateTime] as date) ,ChannelID

How to generate Month list in PostgreSQL?

I have a table A with startdate column which is TIMESTAMP WITHOUT TIME ZONE I need to write a query/function that generate a list of months from the MIN value of the column till MAX value of the column.
For example:
startdate
2014-12-08
2015-06-16
2015-02-17
will generate a list of: (Dec-14,Jan-15,Feb-15,Mar-15,Apr-15,May-15,Jun-15)
How do I do that? I never used PostgreSQL to generate data that wasn't there... it always has been finding the correct data in the DB... any ideas how to do that? Is it doable in a query?
For people looking for an unformatted list of months:
select * from generate_series('2017-01-01', now(), '1 month')
You can generate sequences of data with the generate_series() function:
SELECT to_char(generate_series(min, max, '1 month'), 'Mon-YY') AS "Mon-YY"
FROM (
SELECT date_trunc('month', min(startdate)) AS min,
date_trunc('month', max(startdate)) AS max
FROM a) sub;
This generates a row for every month, in a pretty format. If you want to have it like a list, you can aggregate them all in an outer query:
SELECT string_agg("Mon-YY", ', ') AS "Mon-YY list"
FROM (
-- Query above
) subsub;
SQLFiddle here

how do i calculate the total billed amount for a particular day?

I have a database table where there is a field- billed_amount which keeps the records of the billed amount for a particular person and another field- billing_date. Now,I want to display the total billed amount for all people for a particular day, for example today, to generate day to day sales report.
For a particular day you could run the following, changing the date to whatever date you're running it for.
select sum(billed_amount)
from tbl
where billing_date = '2014-07-19'
Note that each database varies with its default date format. (you didn't specify a database)
To get the total for each date ("grouped by" date), you can use the following:
select billing_date, sum(billed_amount)
from tbl
group by billing_date
order by billing_date
You can group by day, and sum the amounts billed:
select cast(billing_date as date)
, sum(billing_amount)
from YourTable
group by
cast(billing_date as date)
Date operations vary by database. In SQL Server 2008+, you can cast a datetime to the date type to strip off the time.

How to get number of hits by time regardless of Date?

I am working on a sql view that should get the average number of hits by hour of the day, regardless of what day/date it is for traffic monitoring (12:00:00.000 - 12:59:59.999). Any ideas?
EDIT
Now I have the total, how do I get the average? SELECT AVG("FUNCTION BELOW") DOES NOT WORK
SELECT COUNT(*) AS total, DATEPART(hh, LogDate) AS HourOfDay
FROM dbo.Log
GROUP BY DATEPART(hh, LogDate)
Convert to DATEPART(hh,.....
Example SELECT DATEPART(hh,GETDATE())
Since you are on SQL Server 2008, you can use the time data type, just convert to time
example
SELECT CONVERT(TIME,GETDATE())
Then you can filter that also
Since I am not sure what your output is supposed to be like I am showing you both, but if all you need is to group by hour, then just do a datepart(hh.....
The query below may be good enough for you. It divides the count by the difference between todays date and the minimum date in the LogDate column.
SELECT DATEPART(hh,LogDate) as Hour
,CAST(COUNT(*)as decimal)/DATEDIFF(d,(SELECT MIN(LogDate) from log)
,CURRENT_TIMESTAMP) as AverageHits
, COUNT(*) as Count
FROM log
GROUP BY DATEPART(hh,LogDate)
ORDER by DATEPART(hh,LogDate) asc