SQL How to group data into separate month columns - sql

So I'm running this query to get the name of the customer, total amount ordered, and number of orders they've submitted. With this query, I get their entire history from March to July, what I want is the name, march amount total/# of orders, april amount total/# of orders, may amount total/# of orders, ..... etc.
SELECT customer_name,MONTH(created_on), SUM(amount), COUNT(order_id)
FROM customer_orders
WHERE created_on BETWEEN '2020-03-01' AND '2020-08-01'
GROUP BY customer_name, MONTH(created_on)

If you want the values in separate columns, then use conditional aggregation:
SELECT customer_name,
SUM(CASE WHEN MONTH(created_on) = 3 THEN amount END) as march_amount,
SUM(CASE WHEN MONTH(created_on) = 3 THEN 1 ELSE 0 END) as march_count,
SUM(CASE WHEN MONTH(created_on) = 4 THEN amount END) as april_amount,
SUM(CASE WHEN MONTH(created_on) = 4 THEN 1 ELSE 0 END) as april_count,
. . .
FROM customer_orders
WHERE created_on >= '2020-03-01' AND
created_on < '2020-08-01'
GROUP BY customer_name;
Notice that I changed the date filter so it does not include 2020-08-01.

Related

Get last 7 days, 20 days and YTD count

I have a table with columns sales_date, sales_id, sales_region and I am looking to display the count of sales for the past 7 days, 20 days and YTD.
I have this query below that returns the correct count for 7 and 20 days but the YTD shows the count minus the 7 and 20 days. How can I tweak this query to show the YTD correctly? Thank you
select region,
case when current_date- sales_date <=7 then 'Past7'
when current_date- sales_date <=28 then 'Past20'
else 'YTD'
end as "trendsales",
count(*) as salescount
from sales_table
where sales_date >= '2022-01-01'
group by 1
you could pivot it a bit. 4 columns Region, YTD, Past7, and Past20 would be columns.
select region,
sum(case when current_date- sales_date <=7 then 1 else 0 end) as Past7,
sum(case when current_date- sales_date <=28 then 1 else 0 end) as Past20,
count(*) as YTD
from sales_table
where sales_date >= '2022-01-01'
group by 1

How do I get transactions amount > 1000 of all months in SQL

I have been trying to pull customers who have transaction amount greater than 1000 in all months. This is what I have tried so far. But it doesn't look like it's working when I do individual customer test.
Select customer
,extract(month from trans_date) as mth
,extract(year from trans_date) as yr
,sum(trans_amount) as amt
, case when mth in (8) and amt > 1000 then 1 else 0 end as aug
, case when mth in (9) and amt > 1000 then 1 else 0 end as sep
, case when mth in (10) and amt > 1000 then 1 else 0 end as oct
, case when mth in (11) and amt > 1000 then 1 else 0 end as nov
, case when mth in (12) and amt > 1000 then 1 else 0 end as de_c
from transaction
group by 1,2,3
having (aug = 1 and sep = 1 and oct=1 and nov=1 and de_c = 1)
Select customer
,extract(month from trans_date) as mth
,extract(year from trans_date) as yr
,sum(trans_amount) as amt
from transaction
-- filter only those months you want to check, e.g.
where trans_date between date '2021-08-01' and date '2021-12-31'
group by 1,2,3
-- check that every month there was an individual transaction over 1000
qualify
min(max(trans_amount))
over (partition by customer) > 1000
Edit:
Same logic to get just the customer without detail rows:
select customer
from
(
Select customer, max(trans_amount) as maxamt
from transaction
-- filter only those months you want to check, e.g.
where trans_date between date '2021-08-01' and date '2021-12-31'
group by
customer
,trunc(trans_date, 'mon') -- for every month
) as dt
group by customer
-- check that every month there was an individual transaction over 1000
having min(maxamt) > 1000
You may want to try using Over (partition by) something like this.
Select customer
,extract(month from trans_date) as mth
,extract(year from trans_date) as yr
,sum(trans_amount) over (partition by customer , extract(month from trans_date)) as
total
From transaction
Order by total desc
Assuming your data is one record per customer per month.
To get unique customers where trans_amt > 1000 in every month :
select customer
from transaction
group by customer
having count(1) = count(case when trans_amt > 1000 then 1 else 0 end)
To get all records only for customers where trans_amt > 1000 in every month :
select customer, trans_date, trans_amt
from transaction
qualify count(1) over (partition by customer) = count(case when trans_amt > 1000 then 1 else 0 end) over (partition by customer)

I am looking to find customers repurchase frequency in SQL from their first purchase date

I am trying to find the customer's repurchase rates from their first order date. For example, for 2016, how many customer purchased 1X in days 1-365 from their initial purchase, how many purchased twice etc.
I have a transaction_detail table which looks like below:
txn_date Customer_ID Transaction_Number Sales
1/2/2019 1 12345 $10
4/3/2018 1 65890 $20
3/22/2019 3 64453 $30
4/3/2019 4 88567 $20
5/21/2019 4 85446 $15
1/23/2018 5 89464 $40
4/3/2019 5 99674 $30
4/3/2019 6 32224 $20
1/23/2018 6 46466 $30
1/20/2018 7 56558 $30
I am able to find the customers who have shopped in 2016 and how many times have they repurchased in 2016, but I need to find the customer who have shopped in 2016 and how many times have they come back from their first purchase date.
I need a starting point for the query, I am not sure how to build this logic in my SQL code.
Any help would be appreciated.
I am using the below query:
WITH by_year
AS (SELECT
Customer_ID,
to_char(txn_date, 'YYYY') AS visit_year
FROM table
GROUP BY Customer_ID, to_char(txn_date, 'YYYY')),
with_first_year
AS (SELECT
Customer_ID,
visit_year,
FIRST_VALUE(visit_year) OVER (PARTITION BY Customer_ID ORDER BY visit_year) AS first_year
FROM by_year),
with_year_number
AS (SELECT
Customer_ID,
visit_year,
first_year,
(visit_year - first_year) AS year_number
FROM with_first_year)
SELECT
first_year AS first_year,
SUM(CASE WHEN year_number = 0 THEN 1 ELSE 0 END) AS year_0,
SUM(CASE WHEN year_number = 1 THEN 1 ELSE 0 END) AS year_1,
SUM(CASE WHEN year_number = 2 THEN 1 ELSE 0 END) AS year_2,
SUM(CASE WHEN year_number = 3 THEN 1 ELSE 0 END) AS year_3,
SUM(CASE WHEN year_number = 4 THEN 1 ELSE 0 END) AS year_4,
SUM(CASE WHEN year_number = 5 THEN 1 ELSE 0 END) AS year_5,
SUM(CASE WHEN year_number = 6 THEN 1 ELSE 0 END) AS year_6,
SUM(CASE WHEN year_number = 7 THEN 1 ELSE 0 END) AS year_7,
SUM(CASE WHEN year_number = 8 THEN 1 ELSE 0 END) AS year_8,
SUM(CASE WHEN year_number = 9 THEN 1 ELSE 0 END) AS year_9
FROM with_year_number
GROUP BY first_year
ORDER BY first_year
Use window functions and aggregation:
select cnt, count(*), min(customer_id), max(customer_id)
from (select customer_id, count(*) as cnt
from (select td.*,
min(txn_date) over (partition by Customer_ID) as min_txn_date
from transaction_detail td
) td
where txn_date >= min_txn_date and txn_date < min_txn_date + interval '365' day
group by customer_id
) c
group by cnt
order by cnt;
So as per my understanding, you want to know the count of the distinct person who first purchased in 2016 and repurchased after one year or more from date of purchase.
Select * from
(
Select customer_id,
Floor(months_between(txn_date, lead_txn_date)/12) as num_years
From
(
Select customer_id,
txn_date,
row_number() over (partition by Customer_ID order by txn_date) as rn,
lead(txn_date) over (partition by Customer_ID order by txn_date) as lead_txn_date
From your_table
)
Where txn_date >= date '2016-01-01'
and txn_date < date '2017-01-01'
and rn = 1
And months_between(txn_date, lead_txn_date) >= 12
)
Pivot
(
Count(1) for num_year in (1,2,3,4)
)
Ultimately, we are finding the number of years between first and second purchase of the customer. And first purchase must be in 2016.
Cheers!!

SUM values BETWEEN specific dates in BigQuery

I need to query a 12, 24, 36 and 48 month total for each customer
I've got a dataset that includes customer information (customer_id, products, spend, qty, purchase_date, etc) I need to display the totals for the different periods per customer
SELECT customer_id, MIN(purchase_date) AS first_purchase,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 1 YEAR THEN spend END) AS 12_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 2 YEAR THEN spend END) AS 24_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 3 YEAR THEN spend END) AS 36_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 4 YEAR THEN spend END) AS 48_mnth_total
FROM SalesTable
GROUP BY customer_id, purchase_date
ORDER BY purchase_date
My query shows me the following error: Syntax error: Expected ")" but got keyword THEN
You seem to want to count from the first purchase. You cannot nest aggregation functions the way that you are doing it. Instead, use a window function to get the minimum date for each customer and then aggregate:
SELECT customer_id, MIN(purchase_date) AS first_purchase,
SUM(CASE WHEN purchase_date BETWEEN min_purchase_date AND DATETIME_ADD(min_purchase_date, INTERVAL 1 YEAR) THEN spend
END) AS 12_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN min_purchase_date AND DATETIME_ADD(min_purchase_date, INTERVAL 2 YEAR) THEN spend
END) AS 24_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN min_purchase_date AND DATETIME_ADD(min_purchase_date, INTERVAL 3 YEAR) THEN spend
END) AS 36_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN min_purchase_date AND DATETIME_ADD(min_purchase_date, INTERVAL 4 YEAR) THEN spend
END) AS 48_mnth_total,
FROM (SELECT s.*,
MIN(purchase_date) OVER (PARTITION BY customer_id) as min_purchase_date
FROM SalesTable s
) t
GROUP BY customer_id
ORDER BY first_purchase;
You ca simplify the logic by removing the first comparison in the case:
SELECT customer_id, MIN(purchase_date) AS first_purchase,
SUM(CASE WHEN purchase_date <= DATETIME_ADD(min_purchase_date, INTERVAL 1 YEAR) THEN spend
END) AS 12_mnth_total,
SUM(CASE WHEN purchase_date <= DATETIME_ADD(min_purchase_date, INTERVAL 2 YEAR) THEN spend
END) AS 24_mnth_total,
SUM(CASE WHEN purchase_date <= DATETIME_ADD(min_purchase_date, INTERVAL 3 YEAR) THEN spend
END) AS 36_mnth_total,
SUM(CASE WHEN purchase_date <= DATETIME_ADD(min_purchase_date, INTERVAL 4 YEAR) THEN spend
END) AS 48_mnth_total,
FROM (SELECT s.*,
MIN(purchase_date) OVER (PARTITION BY customer_id) as min_purchase_date
FROM SalesTable s
) t
GROUP BY customer_id
ORDER BY first_purchase;
Any purchase is logically on or after the first one.
The function DATETIME_ADD is not closed. I put it here INTERVAL 1 YEAR")".
Wouldnt know the exact sintax but its a good guess.
SELECT customer_id, MIN(purchase_date) AS first_purchase,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 1 YEAR) THEN spend END) AS 12_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 2 YEAR) THEN spend END) AS 24_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 3 YEAR) THEN spend END) AS 36_mnth_total,
SUM(CASE WHEN purchase_date BETWEEN MIN(purchase_date) AND DATETIME_ADD(MIN(purchase_date), INTERVAL 4 YEAR) THEN spend END) AS 48_mnth_total
FROM SalesTable
GROUP BY customer_id, purchase_date
ORDER BY purchase_date

infuse a sum of the value in the another column with a different filter than the total count column

First here's a sample table.
enter image description here
Provider_name patient date status length
AF AGUIR00001 07/05/2018 3 30
AF ABBOT00001 07/05/2018 30
BB ADAMS00001 07/05/2018 3 30
BB ACEVE00001 07/06/2018 3 30
I have created a query that lets me count the total number of appointments versus the number of appointments with a certain status(eg checked out). I was able to create it and group it by provider.
select provider_name,
count(patient) total,
sum(case when status = 3 then 1 else 0 end) as Checkedout
from appointment
group by provider_name
Then I moved on to the next phase which was to get the total length of those appointments with checkedout status. I made this query but it does not break down into each provider.
select provider_name,
count(patient) total,
sum(case when status = 3 then 1 else 0 end) as Checkedout,
(select sum(length) from appointment where status = 3
and date between '06/01/2018' and '07/06/2018')
from appointment where date between '06/01/2018' and '07/06/2018'
group by provider_name
I need it so that the last column in the query is segregated per provider_name.
Thank you in advance for helping me out.
Actually, you were on the right way, try this:
select provider_name,
count(patient) total,
sum(case when status = 3 then 1 else 0 end) as Checkedout,
sum(case when status = 3 then length else 0 end) as len_status3
from appointment
where date between '2018-01-06' and '2018-06-07'
group by provider_name;
According to your last comment, you need a WITH ROLLUP modifier for GROUP BY as in the following :
select coalesce(provider_name,'Total') as provider_name,
count(patient) total,
sum(case when status = 3 then 1 else 0 end) as Checkedout,
sum(case when status = 3 then length else 0 end) as len_status3
from appointment
where date between '2018-01-06' and '2018-06-07'
group by provider_name with rollup;
SQL Fiddle Demo
you shoul do as for checkedoutout
select provider_name,
count(patient) total,
sum(case when status = 3 then 1 else 0 end) as Checkedout,
sum( case when status = 3 then length else 0 ) as total_length
from appointment where date between '06/01/2018' and '07/06/2018'
group by provider_name