I am trying to pull visits per isoweek from big query.
however I am failing with the date transformation.
Could you support?
StandardSQL
SELECT count (visitid) as Sessions, date,
EXTRACT (ISOYEAR FROM date) AS isoyear
FROM `xxx_*`
WHERE _TABLE_SUFFIX BETWEEN '201806020' AND '20180630'
GROUP BY date
order by date DESC
Have you tried a query like this?
SELECT EXTRACT(ISOYEAR FROM date) as yyyy,
EXTRACT(ISOWEEK FROM DATE) as ww,
COUNT(*) as Sessions
FROM `xxx_*`
WHERE _TABLE_SUFFIX BETWEEN '201806020' AND '20180630'
GROUP BY yyyy, ww
ORDER BY MIN(date) DESC;
Related
In CockroachDB, I want to have such this query on a specific month for its every day:
select count(*), sum(amount)
from request
where code = 'code_string'
and created_at >= '2022-07-31T20:30:00Z' and created_at < '2022-08-31T20:30:00Z'
the problem is that I want it on my local date. What should I do?
My goal is:
"month, day, count, sum" as result columns for a month.
UPDATE:
I have found a suitable query for this purpose:
select count(amount), sum(amount), extract(month from created_at) as monthTime, extract(day from created_at) as dayTime
from request
where code = 'code_string' and created_at >= '2022-07-31T20:30:00Z' and created_at < '2022-08-31T20:30:00Z'
group by dayTime, monthTime
Thanks to #histocrat for easier answer :) by replacing
extract(month from created_at) as monthTime, extract(day from created_at) as dayTime
by this:
date_part('month', created_at) as monthTime, date_part('day', created_at) as dayTime
To group results by both month and day, you can use the date_part function.
select month, day, count(*), sum(things)
from request
where code = 'code_string'
group by date_part('month', created_at) as month, date_part('day', created_at) as day;
Depending on what type created_at is, you may need to cast or convert it first (for example, group by date_part('month', created_at::timestamptz)).
I'm writing an SQL query for Apache Druid and I would like to group results by date. I'm used to DB2 and I would typically do something like:
SELECT DATE(TIMESTAMP), COUNT(*) FROM my_table GROUP BY (DATE(TIMESTAMP))
I'm using the /druid/v2/sql API endpoint and passing in the query in a POST. I get an SQL parsing error when I try this. I know that I can group by day with something like
SELECT EXTRACT(day FROM timestamp), COUNT(*) FROM my_table GROUP BY 1
but I would like the full date if possible.
Thanks in advance.
Does this work?
SELECT DATE_TRUNC('day', TIMESTAMP), COUNT(*)
FROM my_table
GROUP BY DATE_TRUNC('day', TIMESTAMP);
Druid houses timeseries data having __time as PK.
Following SQL script in Druid might work grouping data Year, Month, Day wise.
SELECT TIME_EXTRACT(__time, 'DAY') AS dt, TIME_EXTRACT(__time, 'MONTH') AS mn, TIME_EXTRACT(__time, 'YEAR') AS yr, COUNT(1) AS cnt
FROM <datasource_name/table_name>
WHERE __time BETWEEN '<start_datetime>' and '<end_datetime>'
GROUP BY TIME_EXTRACT(__time, 'DAY'), TIME_EXTRACT(__time, 'MONTH'), TIME_EXTRACT(__time, 'YEAR')
HAVING cnt >= <Threshold_Value>
ORDER BY yr, mn, dt, cnt DESC
I have a table called 'daily_prices' where I have 'sale_date', 'last_sale_price', 'symbol' as columns.
I need to calculate how many times 'last_sale_price' has gone up compared to previous day's 'last_sale_price' in 10 weeks.
Currently I have my query like this for 2 weeks:
select count(*) as "timesUp", sum(last_sale_price-prev_price) as "dollarsUp", 'wk1' as "week"
from
(
select last_sale_price, LAG(last_sale_price, 1) OVER (ORDER BY sale_date) as prev_price
from daily_prices
where sale_date <= CAST('2020-09-18' AS DATE) AND sale_date >= CAST('2020-09-14' AS DATE)
and symbol='AAPL'
) nest
where last_sale_price > prev_price
UNION
select count(*) as "timesUp", sum(last_sale_price-prev_price) as "dollarsUp", 'wk2' as "week"
from
(
select last_sale_price, LAG(last_sale_price, 1) OVER (ORDER BY sale_date) as prev_price
from daily_prices
where sale_date <= CAST('2020-09-11' AS DATE) AND sale_date >= CAST('2020-09-07' AS DATE)
and symbol='AAPL'
) nest
where last_sale_price > prev_price
I'm using 'UNION' to combine the weekly data. But as the number of weeks increase the query is going to be huge.
Is there a simpler way to write this query?
Any help is much appreciated. Thanks in advance.
you can extract week from sale_date. then apply group by on the upper query
select EXTRACT(year from sale_date) YEAR, EXTRACT('week' FROM sale_date) week, count(*) as "timesUp", sum(last_sale_price-prev_price) as "dollarsUp"
from (
select
sale_date,
last_sale_price,
LAG(last_sale_price, 1) OVER (ORDER BY sale_date) as prev_price
from daily_prices
where symbol='AAPL'
)
where last_sale_price > prev_price
group by EXTRACT(year from sale_date), EXTRACT('week' FROM sale_date)
to extract only weekdays you can add this filter
EXTRACT(dow FROM sale_date) in (1,2,3,4,5)
PS: make sure that monday is first day of the week. In some countries sunday is the first day of the week
You can filter on the last 8 weeks in the where clause, then group by week and do conditional aggregation:
select extract(year from sale_date) yyyy, extract(week from saledate) ww,
sum(last_sale_price - lag_last_sale_price) filter(where lag_last_sale_price > last_sale_price) sum_dollars_up,
count(*) filter(where lag_last_sale_price > last_sale_price) cnt_dollars_up
from (
select dp.*,
lag(last_sale_price) over(partition by extract(year from sale_date), extract(week from saledate) order by sale_date) lag_last_sale_price
from daily_price
where symbol = 'AAPL'
and sale_date >= date_trunc('week', current_date) - '8 week'::interval
) dp
group by 1, 2
Notes:
I am asssuming that you don't want to compare the first price of a week to the last price of the previous week; if you do, then just remove the partition by clause from the over() clause of lag()
this dynamically computes the date as of 8 (entire) weeks ago
if there is no price increase during a whole week, the query still gives you a row, with 0 as sum_dollars_up and cnt_dollars_up
I have a table with the following columns:
NETID is a unique identifier for the user, OCCURRENCES is the number of times they've logged in in a month, and EARLIEST_MONTHLY_DATE is the earliest time they logged in for a month. I've currently using:
SELECT
to_char(earliest_monthly_date, 'yyyy-mm-dd') MARCH,
COUNT(NETID) unique_login_count
FROM
REPORT_SERVICE_USAGE
WHERE
earliest_monthly_date >= to_date('2014-03-01', 'yyyy-mm-dd')
AND earliest_monthly_date <= to_date('2014-03-31', 'yyyy-mm-dd')
GROUP BY
to_char(earliest_monthly_date, 'yyyy-mm-dd')
ORDER BY
to_char(earliest_monthly_date, 'yyyy-mm-dd') ASC
which gives me the total number of logins per day in a given month. It returns something like this:
Now, I want to set up my query so that it groups the login count by month instead of by day of a given month. I'm not sure how to do this (or if it can be done), as I'm not very familiar with SQL, but any help would be greatly appreciated. If you need any more info, feel free to ask. By the way, it's an Oracle database.
SELECT
to_char(earliest_monthly_date, 'yyyy-mm') MARCH,
COUNT(NETID) unique_login_count
FROM
REPORT_SERVICE_USAGE
WHERE
earliest_monthly_date >= to_date('2014-03-01', 'yyyy-mm-dd')
AND earliest_monthly_date <= to_date('2014-03-31', 'yyyy-mm-dd')
GROUP BY
to_char(earliest_monthly_date, 'yyyy-mm')
ORDER BY
to_char(earliest_monthly_date, 'yyyy-mm') ASC
Just change the select and group by 'to_char' to extract
SELECT
extract(MONTH from earliest_monthly_date) MARCH,
COUNT(NETID) unique_login_count
FROM
REPORT_SERVICE_USAGE
WHERE
earliest_monthly_date >= to_date('2014-03-01', 'yyyy-mm-dd')
AND earliest_monthly_date <= to_date('2014-03-31', 'yyyy-mm-dd')
GROUP BY
extract(MONTH from earliest_monthly_date)
ORDER BY
extract(MONTH from earliest_monthly_date) ASC
Edit
If you like the groups to by by month and year the answer from #Lamak is a better option.
Very closely related to SQL - Select most 'active' timespan fromdb but different question.
"I have a table of transactions. In this table I store the transaction datetime in UTC. I have a few months of data, about 20,000 transactions a day."
How would change
select datepart(hour, the_column) as [hour], count(*) as total
from t
group by datepart(hour, the_column)
order by total desc
so that I can select the specific year, month, day, hour, minute, and second that was the most 'active'.
To clarify, I'm not looking for which hour or minute of the day was most active. Rather, which moment in time was the most active.
Select
DATEPART(year, the_column) as year
,DATEPART(dayofyear,the_column) as day
,DATEPART(hh, the_column) as hour
,DATEPART(mi,the_column) as minute
,DATEPART(ss, the_column) as second
,count(*) as count from t
Group By
DATEPART(year, the_column)
, DATEPART(dayofyear,the_column)
, DATEPART(hh, the_column)
, DATEPART(mi,the_column)
, DATEPART(ss, the_column)
order by count desc
If minute resolution is enough:
select top 1 cast(the_column as smalldatetime) as moment, count(*) as total
from t
group by cast(the_column as smalldatetime)
order by total desc