I have a table with a calendar_date and user_id column.
I want to a new table where I aggregate the calendar_dates into calendar_weeks and find the difference in the sum of user_ids between each week. Essentially, this would be a combination of aggregating from daily values to weekly values, as well as a week-to-date function.
You can use the DATE_TRUNC function to summarize each date to a weekly date and then group by your aggregation. https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#date_trunc
SELECT
DATE_TRUNC(calendar_date, WEEK(SUNDAY)) as calendar_week,
user_id,
etc...
FROM
table
GROUP BY
calendar_week,
user_id
Related
I have a google bigquery table with orders with a DATE column and other columns related to the orders. The starting date of the dataset is from 2021-01-01 (yyyy-mm-dd).
My aim is to filter on the DATE column from last year and this year to the previous iso week. For this, I used the ISOWEEK to create a new column:
WITH
last_week_last_year AS (
SELECT
DATE,
EXTRACT(ISOWEEK FROM DATE) AS isoweek,
FROM
`orders`
WHERE
EXTRACT(ISOWEEK FROM DATE) = EXTRACT(ISOWEEK FROM CURRENT_DATE())-1
GROUP BY 1, 2
ORDER BY DATE
)
SELECT * FROM last_week_last_year
This query results as the following table:
The issue is that when I filter on the original orders table by the DATE from the last_week_last_year table I get all the orders back instead of just the filtered version.
My method to filter is WHERE DATE IN (SELECT DATE FROM last_week_last_year) as seen below.
SELECT
*
FROM
`orders`
WHERE
DATE IN (SELECT DATE FROM last_week_last_year)
ORDER BY DATE DESC;
A snapshot of resulting table. It contains all of the records from 2021-01-01 until the latest day.
How can I make sure that on the latter query the table is filtered based on the first query's dates in DATE column?
I have a table say testTable in Hive(with data for 3 years) with the following columns:
retailers, order_total, order_total_qty, order_date
I have to create a new table with these columns:
'source_name' as source, sum(retailers), sum(order_total), sum(order_total_qty)
for each week from the starting order_date.
I am stuck with this. How can I group following data in the way that it will sum up on weekly basis.
Use WEEKOFYEAR() function to calculate aggregation on weekly basis.
select
'source_name' source,
sum(retailers) sum_retailers,
sum(order_total) sum_order_total,
sum(order_total_qty) sum_order_total_qty,
WEEKOFYEAR(order_date) week,
year(order_date) year
from testTable
where order_date >= '2015-01-01' --start_date
group by WEEKOFYEAR(order_date), year(order_date)
order by year, week; --order if necessary
I have a table with 3 columns: id, date and amount, but I would like to get accumulated SUM for each date (Last column).
Do you have an easy solution how to add this column?
I am trying with this:
SELECT date, sum(amount) as accumulated
FROM table group by date
WHERE max(date);
Should I user OVER() for this?
Use a window function to the total for each day:
SELECT date,
amount,
sum(amount) over (partition by date) as accumulated
FROM the_table;
However this will only work, if your dates all have the same time part (in Oracle a DATE column also contains a time). To make sure you ignore the time part, use trunc() to make sure all time parts are normalized to 00:00:00
SELECT date,
amount,
sum(amount) over (partition by trunc(date)) as accumulated
FROM the_table;
Use This:
SELECT T.ID, T.DATE, T.AMOUNT, (SELECT SUM(S.AMOUNT) FROM TABLE S WHERE S.DATE=T.DATE) ACCUMULATED
from
table T
This will give you the records from the table with a sum for all records for the date.
I have a have a hive table which is partitioned with the table_date.I want to get the count of individual partition for the particular day for the particular month and particular year.
When I run the following query I am getting a count for an entire month but I want it as individual day.
select count(*) from table where month(table_date)=1 and year(table _date)=2016
If it is partitioned on date, I would expect this to work:
select table_date, count(*)
from table
where table_date >= '2016-01-01' and table_date < '2016-02-01'
group by table_date;
select table_date, count(*)
from table
where table_date between '2016-01-01' and '2016-02-01'
group by table_date;
I have a table in which minimum and maximum temperature is stored per order no and date. I want to pick the minimum temperature and maximum temperature for each day. This should be done using SQL script.
You have to use group by clause, and aggregate functions min, max as below:
select date, min(temperature), max(temperature)
from table
group by date
It will work if your date have only year, month and day (01/11/2012).
In oracle:
SELECT TO_CHAR(DATE_VAL,'DD-MM-YYYY'), MAX(temperature),
MIN(temperature) FROM table_name group by TO_CHAR(DATE_VAL,'DD-MM-YYYY');
In MySQl:
SELECT DATE_FORMAT(DATE_VAL, '%d-%m-%Y'), max(temperature),
min(temperature) from table_name group by DATE_FORMAT(DATE_VAL, '%d-%m-%Y');