Calculate the sum of a column on weekly basis in hive - hive

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

Related

Doing Week To Date in BigQuery

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

SQL - Aggregate dates from different columns into Month/Year table

So I have an 'Orders' table that lists the 'Ordered' and 'Shipped' dates for each order.
These are custom products and it takes 1 week to fill orders.
This is pretty representative of the table I have:
I want to aggregate this into a table so that I can see how many orders were ordered and shipped for each month during the date range specified when the report is run, and I want the Months and years to automatically populate without me having to hardcode for each month and year:
What's the best way to do this with SQL?
I eventually want to place the aggregated table into an SSRS report so that you can expand/collapse each year, if needed.
Date/time functions are notoriously database dependent. Here is a typical approach, though:
select yyyy, mm, sum(num_ordered), sum(num_shipped)
from ((select year(ordered) as yyyy, month(ordered) as mm, count(*) as num_ordered, 0 as num_shipped
from orders
group by year(ordered), month(ordered)
) union all
(select year(shipped) as yyyy, month(shipped) as mm, 0 count(*) as num_shipped
from orders
group by year(shipped), month(shipped)
)
) ym
group by yyyy, mm;

SQL QUERY - Filter date from the beginning

How to write SQL query that will sum the amount from the previous days/years. Like from the start.
Scenario I want to compute accumulated sales of the store from the day it was opened.
Example
SELECT SUM(AMOUNT)
FROM TransactionTable
WHERE TransactionDate = ???
The plan that I have is to query on this table and get the oldest transaction date record, then I will use that in the WHERE condition. Do you think that it is the best solution?
You can try below using having min(transaction) which will give you the date when transaction first started
select sum (amt) from
(
SELECT SUM(AMOUNT) as amt from TransactionTable
group by TransactionDate
having TransactionDate between min(TransactionDate) and getdate()
)A
To compute accumulated sales of the store from the day it started you can use SUM with OVER clause
SELECT TransactionDate, SUM(AMOUNT) OVER (ORDER BY TransactionDate) AS AccumulatedSales
FROM TransactionTable
use group by TransactionDate
SELECT convert(date,TransactionDate), SUM(AMOUNT) from TransactionTable
group by convert(date,TransactionDate)

Comparing two date columns and adding derived column using Oracle SQL

My table structure is this:
ID,
country,
month,
year,
total amt in previous period,
total amt during period,
incr/decr in total amt in previous period,
incr/decr in total amt during (month, year)
The ID, month, year and total amt fields are available in table abc.
The incr/decr in total amt in previous period is the difference between total amt in previous period and total amt during period columns.
I wrote this query:
select m.id, m.month, m.year, m.total_amt
from abc m
order by year, month desc;
For the total amt in previous period I could not use Between Date( ) And DateAdd("M", -1, Date( )); as I have no date but just year and month.
How to compare the two columns with the columns year and month and how to have the last two derived columns using subqueries?
For comparing Month there is an specific function (that works for MS SQL and ORACLE). There's one function for days and years too. See links below:
YEAR
MONTH
DAY
Examples of this and getting derived columns from subqueries can be found in this topic already discussed in the forum:
Stackoverflow topic

Select per month

I've got the following table:
purchases(id, item, user, price, time);
The time field is a timestamp.
I need a query that would return one row per month and that row would contain the sum of price for each item in that month.
Try this:
SELECT MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY MONTH(`time`)
If you have more than one year's data you may also want to include the year in your group by:
SELECT YEAR(`time`) AS year, MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY YEAR(`time`), MONTH(`time`)
what about GROUP BY YEAR(DATE(time)) ASC, MONTH(DATE(time)) ASC?