Get average from column gruped by date - sql

I have problem with query. I have data like this:
value
timestamp
22.12
2023-01-18T08:00:35.000Z
22.18
2023-01-18T09:13:12.000Z
22.15
2023-01-18T09:16:12.000Z
22.17
2023-01-18T09:49:35.000Z
16.12
2023-01-25T10:15:05.000Z
26.18
2023-01-25T10:40:05.000Z
25.52
2023-01-25T10:55:05.000Z
19.88
2023-01-26T11:40:05.000Z
16.12
2023-01-16T12:40:05.000Z
Is it possible to write query where I can get average of values gruped by date?
For example:
2023-01-18 - average: 22
2023-01-25 - average: 19

Convert timestamp to a date value and use it in GROUP BY.
Query
select cast(timestamp as date) as dt, AVG(value) as avg_val
from tbl_name
group by cast(timestamp as date);

Related

get count all with groupby timestamp into hourly intervals

I have a hive table that has a timestamp in string format as below,
20190516093836, 20190304125015, 20181115101358
I want to get row count with an aggregate timestamp into hourly as below
date_time count
-----------------------------
2019:05:16: 00:00:00 23
2019:05:16: 01:00:00 64
I followed several links like this but was unable to generate the desired results yet.
This is my final query:
SELECT
DATE_PART('day', b.date_time) AS date_prt,
DATE_PART('hour', b.date_time) AS hour_prt,
COUNT(*)
FROM
(SELECT
from_unixtime(unix_timestamp(`timestamp`, "yyyyMMddHHmmss")) AS date_time
FROM table_name
WHERE from_unixtime(unix_timestamp(`timestamp`, "yyyyMMddHHmmss"))
BETWEEN '2018-12-10 07:02:30' AND '2018-12-12 08:02:30') b
GROUP BY
date_prt, hour_prt
I hope for some guidance from you, thanks in advance
You can extract date_time already in required format 'yyyy-MM-dd HH:00:00'. I prefer using regexp_replace:
SELECT
date_time,
COUNT(*) as `count`
FROM
(SELECT
regexp_replace(`timestamp`, '^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$','$1-$2-$3 $4:00:00') AS date_time
FROM table_name
WHERE regexp_replace(`timestamp`, '^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$','$1-$2-$3 $4:$5:$6')
BETWEEN '2018-12-10 07:02:30' AND '2018-12-12 08:02:30') b
GROUP BY
date_time
This will also work:
from_unixtime(unix_timestamp('20190516093836', "yyyyMMddHHmmss"),'yyyy-MM-dd HH:00:00') AS date_time

SQL - Selecting rows within a date range?

I am trying to select all rows that are within a date range including the start and end day -
for example
Select *
from table
where timestamp between 2019-03-01 and 2019-03-08
I want all rows that are on 2019-03-01 and 2019-03-08 and all rows between the two dates as well
Thanks
You should use date() for timestamp and proper quote around the date value
SELECT *
FROM tbl_recordings
WHERE date(timestamp)
between str_to_date('2019-03-01', '%Y-%m-%d')
and str_to_date('2019-03-08', '%Y-%m-%d');
or
SELECT *
FROM tbl_recordings
WHERE date(timestamp) between '2019-03-01' and '2019-03-08';

Big Query for fetching Distinct Date and also Max of the Unique Count

I have same date twice by i have different Unique Count and Hour. I want to take Max count of Unique and Hour and Distinct Date.
Can some one help ?
ex:
Day 2018-08-11 00:00:00 UTC 12 1950
Day 2018-08-11 18:00:67 UTC 7 86
So now I need a single date with Max of Unique Count?
Your question is a little tricky to understand, but I assume you want to return the maximum value for each distinct date. You can do that by using a GROUP BY like so:
WITH `myTest` as (
SELECT date "2018-08-11" myDate, time "00:00:00" myTime, "UTC" myTimeZone, 8979 myValue UNION ALL
SELECT date "2018-08-11" myDate, time "18:00:60" myTime, "UTC" myTimeZone, 1274 myValue UNION ALL
SELECT date "2018-09-13" myDate, time "17:36:40" myTime, "UTC" myTimeZone, 6342 myValue UNION ALL
SELECT date "2018-09-13" myDate, time "05:05:32" myTime, "UTC" myTimeZone, 67 myValue )
SELECT myDate, max(myValue) as Max_Value
FROM `myTest`
GROUP BY myDate
You can plop this into your BigQuery editor to see it work and then shape it to match whatever date you're using.

MSSQL Sum by EntryDate

I have a table called "Data". With columns: "Number" and "EntryDate".
The EntryDate is Datetime( Y-m-d h:i:s ).
I need to calculate the sum of all entries from a date ignore h:i:s, group them by the date.
Example:
Number | EntryDate
-------------------
23 | 2018-10-01 13:22:10.520
25 | 2018-10-01 11:16:09.533
So basically I need to SUM the Number from 2018-10-01.
I have tried several variations but nothing seems to work, for example:
SELECT
SUM(Number) as 'Sum',
EntryDate AS DATE
FROM Data
GROUP BY EntryDate
Use cast() function for converting datetime to date
SELECT
SUM(Number) as 'Sum', cast(EntryDate as date) AS `DATE`
FROM Data
GROUP BY cast(EntryDate as date
Your date is at the moment in the datetime format, hence if you select date within your select query, you wont really get the date, instead you would get the respective datetimes.
What you can do is Convert the EntryDate as date:
Try:
select sum(number) as 'Sum', convert(date,EntryDate) as 'Date'
from Data
group by convert(date,EntryDate)
Should work.
Go seek more information from here https://www.w3schools.com/sql/func_sqlserver_convert.asp
Cheers

count group by conflict oracle sql

For the following query, I got the result below
select date, count(sales)
from table
where date between to_date('2015-09-01','YYYY-MM-DD') and to_date('2015-12-31','YYYY-MM-DD')
group by date
Date Count(sales)
01-SEP-15 480
01-SEP-15 2
01-SEP-15 3
01-SEP-15 2
16-SEP-15 12
16-SEP-15 7
It should just give me two rows-> 01-SEP-15 and count(sales) as 487.
and 16-SEP-15 and count(sales) as 19
How do I get that?
In Oracle a date also contains a time part. Your SQL client hides that from you by not including that in the output. You need to trunc() the date column to set the time part to 00:00:00
select trunc(date), count(sales)
from table
where date between to_date('2015-09-01','YYYY-MM-DD') and to_date('2015-12-31','YYYY-MM-DD')
group by trunc(date)
The logic that you probably intend is more like:
select trunc(date), count(sales)
from table
where trunc(date) between date '2015-09-01' and date '2015-12-31'
group by trunc(date);
However, if you have an index on date but not trunc(date), this would more naturally be written for performance as:
select trunc(date), count(sales)
from table
where date >= date '2015-09-01' and
date < date '2016-01-01'
group by trunc(date);