https://www.youtube.com/watch?v=eMQDHHfUJtU&list=PLBTZqjSKn0IfuIqbMIqzS-waofsPHMS0E&index=19
-- Create the flights table
CREATE TABLE flights (
cid VARCHAR(512),
fid VARCHAR(512),
origin VARCHAR(512),
Destination VARCHAR(512)
);
-- Insert values into the flights table
INSERT INTO flights (cid, fid, origin, Destination) VALUES
('1', 'f1', 'Del', 'Hyd'),
('1', 'f2', 'Hyd', 'Blr'),
('2', 'f3', 'Mum', 'Agra'),
('2', 'f4', 'Agra', 'Kol');
SELECT f1.cid,f1.origin,f2.destination FROM flights f1
INNER JOIN flights f2 ON f1.destination = f2.origin
-- Create the sales table
CREATE TABLE sales (
order_date DATE,
customer VARCHAR(512),
qty INT
);
-- Insert values into the sales table
INSERT INTO sales (order_date, customer, qty) VALUES
('2021-01-01', 'C1', 20),
('2021-01-01', 'C2', 30),
('2021-02-01', 'C1', 10),
('2021-02-01', 'C3', 15),
('2021-03-01', 'C5', 19),
('2021-03-01', 'C4', 10),
('2021-04-01', 'C3', 13),
('2021-04-01', 'C5', 15),
('2021-04-01', 'C6', 10);
-- Find the count of new customer added in each month
SELECT Month,count(distinct customer) AS New_Customer FROM (
SELECT *,MONTH(order_date) AS Month,ROW_NUMBER() OVER (PARTITION BY customer ORDER BY month(order_date)) AS rn FROM sales
) xyz
WHERE rn = 1
GROUP BY month(order_date);