0% found this document useful (0 votes)
9 views2 pages

CTE

Uploaded by

nsaranya89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

CTE

Uploaded by

nsaranya89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

drop table if exists order_summary;

CREATE TABLE order_summary(orderid integer,amount integer,quantity integer);

INSERT INTO order_summary(orderid,amount,quantity)


VALUES (1,4922,8),
(2,7116,8),
(3,1206,4),

(4,2841,7),
(5,2522,2),
(6,5084,3),
(7,6680,4),
(8,8123,7),
(9,6015,2),
(10,4092,3),
(11,7224,2),
(12,7679,8),
(13,1303,2),
(14,5185,7),
(15,2139,8);

drop table if exists customer;


CREATE TABLE customer(cust_id integer,cust_first_name text,cust_last_name text);

INSERT INTO customer(cust_id,cust_first_name,cust_last_name)


VALUES (1,'Henry','Brown'),
(2,'James','Williams'),
(3,'Jack','Taylor');

drop table if exists orders;


CREATE TABLE orders(order_id integer,date date,cust_id integer);

INSERT INTO orders(order_id,date,cust_id)


VALUES
(1,'05-08-2020',1),
(2,'04-08-2020',2),
(3,'03-08-2020',3),
(4,'04-08-2020',1),
(5,'05-08-2020',2),
(6,'05-08-2021',3),
(7,'04-08-2021',1),
(8,'03-08-2021',2),
(9,'04-08-2021',3),
(10,'05-08-2021',2),
(11,'05-08-2022',1),
(12,'04-08-2022',2),
(13,'03-08-2022',3),
(14,'04-08-2022',1),
(15,'05-08-2022',2);

select * from orders;


select * from order_summary;
select * from customer;

with cte_2021_sales (cust_id,year,cust_name,total_sales)as (


select c.cust_id,c.year, c.cust_name,sum(d.amount*d.quantity) total_sales from
(
select order_id,year(date) year,a.cust_id,concat(cust_first_name,'
',cust_last_name) cust_name
from orders a
join customer b on a.cust_id=b.cust_id)c
join order_summary d on d.orderid=c.order_id
group by cust_id,year,cust_name)

select * from cte_2021_sales where year=2021


-----------------------------------------------------------------------------------
--------------------------------
with cte_2021_sales (cust_id,year,cust_name,total_sales_2021)as (
select c.cust_id,c.year, c.cust_name,sum(d.amount*d.quantity) total_sales from
(
select order_id,year(date) year,a.cust_id,concat(cust_first_name,'
',cust_last_name) cust_name
from orders a
join customer b on a.cust_id=b.cust_id)c
join order_summary d on d.orderid=c.order_id where year=2021
group by cust_id,year,cust_name),
cte_2020_sales (cust_id,year,cust_name,total_sales_2020)as (
select c.cust_id,c.year, c.cust_name,sum(d.amount*d.quantity) total_sales_2020 from
(
select order_id,year(date) year,a.cust_id,concat(cust_first_name,'
',cust_last_name) cust_name
from orders a
join customer b on a.cust_id=b.cust_id)c
join order_summary d on d.orderid=c.order_id where year=2020
group by cust_id,year,cust_name),
cte_2022_sales (cust_id,year,cust_name,total_sales_2022)as (
select c.cust_id,c.year, c.cust_name,sum(d.amount*d.quantity) total_sales from
(
select order_id,year(date) year,a.cust_id,concat(cust_first_name,'
',cust_last_name) cust_name
from orders a
join customer b on a.cust_id=b.cust_id)c
join order_summary d on d.orderid=c.order_id where year=2022
group by cust_id,year,cust_name)

select a.cust_id,a.cust_name,
a.total_sales_2021,b.total_sales_2022,c.total_sales_2020
from cte_2021_sales a
join cte_2022_sales b on b.cust_id=a.cust_id
join cte_2020_sales c on c.cust_id=a.cust_id;

You might also like