I am new in bigquery and I can show timestamp like this.
select event_timestamp as timestamp1
FROM `alive-ios.analytics_160092165.events_201810*`
GROUP BY timestamp1
Output is like this. How can I group those by month? Is it like this?
https://www.pascallandau.com/bigquery-snippets/convert-timestamp-date-datetime-to-different-timezone/
I try with to_char, DATE , etc and it is not okay.
It sounds like you want the TIMESTAMP_TRUNC function, e.g.
select TIMESTAMP_TRUNC(event_timestamp, MONTH) as timestamp1
FROM `alive-ios.analytics_160092165.events_201810*`
GROUP BY timestamp1
Below is for BigQuery Standard SQL
SELECT
FORMAT_TIMESTAMP('%Y-%m', TIMESTAMP_MICROS(event_timestamp)) month,
COUNT(1) events
FROM `project.dataset.table`
GROUP BY month
Note: most likely you want to count events for each month, so I added COUNT(1), but you can add whatever you need - like SUM(amount) for example if you want to calculate some metric named value
Also, your wildcard expression is build in such a way that it will have only events for month of October 2018 (assuming the table name represent time of event) - so you will need to relax a little you wildcard expression to (for example) alive-ios.analytics_160092165.events_2018* so you will have events for months of whole 2018 year
Above assuming your event_timestamp is represented in microseconds
If in reality they are of TIMESTAMP type - just remove use of TIMESTAMP_MICROS() function
Building on Elliott's example, I think you need to convert the value to a timestamp first. From your example data I think you need TIMESTAMP_MICROS
TIMESTAMP_MICROS
select TIMESTAMP_TRUNC(TIMESTAMP_MICROS(event_timestamp), MONTH) as timestamp1
FROM `alive-ios.analytics_160092165.events_201810*`
GROUP BY timestamp1
Related
I have data with millisecond precision timestamp. I want to only filter for the most recent timestamp within a given second. Ie. records (2020-07-13 5:05.38.009, event1), (2020-07-13 5:05.38.012, event2) should only retrieve the latter.
I've tried the following:
SELECT
timestamp as time, event as value, event_type as metric
FROM
table
GROUP BY
date_trunc('second', time)
But then I'm asked to group by event as well and I see all the data (as if no group by was provided)
In Postgres, you can use distinct on:
select distinct on (date_trunc('second', time)) t.*
from t
order by time desc;
SELECT SUM(Total_A ) FROM Materials_List
This is the snippet of code that I have.
I need it to calculate by month and display by month using SQL.
I also would like it to be a code I can use for any month in the year not just one month at a time.
You seem to be looking for simple aggregation:
select
year(materials_datetime) yr,
month(materials_datetime) mn,
sum(total_a) sum_total_a
from materials_list
group by
year(materials_datetime),
month(materials_datetime)
order by yr, mn
This assumes that column materials_datetime contains the date/time that you want to use to aggregate the data.
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,
.....
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
I have a table with a date column where date is stored in this format:
2012-08-01 16:39:17.601455+0530
How do I group or group_and_count on this column by month?
Your biggest problem is that SQLite won't directly recognize your dates as dates.
CREATE TABLE YOURTABLE (DateColumn date);
INSERT INTO "YOURTABLE" VALUES('2012-01-01');
INSERT INTO "YOURTABLE" VALUES('2012-08-01 16:39:17.601455+0530');
If you try to use strftime() to get the month . . .
sqlite> select strftime('%m', DateColumn) from yourtable;
01
. . . it picks up the month from the first row, but not from the second.
If you can reformat your existing data as valid timestamps (as far a SQLite is concerned), you can use this relatively simple query to group by year and month. (You almost certainly don't want to group by month alone.)
select strftime('%Y-%m', DateColumn) yr_mon, count(*) num_dates
from yourtable
group by yr_mon;
If you can't do that, you'll need to do some string parsing. Here's the simplest expression of this idea.
select substr(DateColumn, 1, 7) yr_mon, count(*) num_dates
from yourtable
group by yr_mon;
But that might not quite work for you. Since you have timezone information, it's sure to change the month for some values. To get a fully general solution, I think you'll need to correct for timezone, extract the year and month, and so on. The simpler approach would be to look hard at this data, declare "I'm not interested in accounting for those edge cases", and use the simpler query immediately above.
It took me a while to find the correct expression using Sequel. What I did was this:
Assuming a table like:
CREATE TABLE acct (date_time datetime, reward integer)
Then you can access the aggregated data as follows:
ds = DS[:acct]
ds.select_group(Sequel.function(:strftime, '%Y-%m', :date_time))
.select_append{sum(:reward)}.each do |row|
p row
end