Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

-- SALES

SELECT
calendar.standard.week_start_date,
calendar.standard.week_end_date,
sales_transaction.transaction_country AS Country,
location_time_dependant.location_channel,
SUM(sales_transaction.gross_sales_amount_local) AS gross_sales_amount_local
FROM
`atz-data-selfserve-prd.sales.sales`
WHERE 1=1
AND transaction_started_at_local BETWEEN '2020-03-02' AND
DATE_TRUNC(current_date, week(monday))
AND sales_transaction.transaction_country in ('CA', 'US')
AND sales_transaction.transaction_type_group = 'Sales'
GROUP BY ALL

-- RETURNS

WITH
calendar AS (
SELECT
DISTINCT date,
calendars.standard.week_start_date,
calendars.standard.week_end_date
FROM
`atz-data-selfserve-prd.shared.calendars`
WHERE
date BETWEEN '2020-03-02' AND DATE_TRUNC(current_date, week(monday))
)
SELECT
c.week_start_date,
c.week_end_date,

sales_transaction.reference_sales_transaction.location_channel,
sales_transaction.transaction_country AS Country,
CASE WHEN DATE_DIFF(sales_transaction.transaction_started_date_local,
sales_transaction.reference_sales_transaction.transaction_date_local, week(Monday))
> 6 THEN "Older"
ELSE CAST(DATE_DIFF(sales_transaction.transaction_started_date_local,
sales_transaction.reference_sales_transaction.transaction_date_local, week(Monday))
AS STRING) END AS weeks_since_sale,
SUM(sales_transaction.net_sales_amount_local) AS gross_sales_amount_local
FROM
`atz-data-selfserve-prd.sales.sales` s
LEFT JOIN
calendar c
ON
sales_transaction.reference_sales_transaction.transaction_date_local=c.date
WHERE 1=1
AND sales_transaction.reference_sales_transaction.transaction_date_local BETWEEN
'2020-03-02' AND DATE_TRUNC(current_date, week(monday))
AND sales_transaction.transaction_type_group='Returns'
AND DATE_DIFF(sales_transaction.transaction_started_date_local,
sales_transaction.reference_sales_transaction.transaction_date_local,
week(Monday))>=0
AND sales_transaction.transaction_country in ('CA', 'US')
GROUP BY ALL

You might also like