SQL Inner Query Alias And Join
use sample_first_db; select * from sample_first_db..customers; select * from sample_first_db..[orders]; -- april may month customers order count --step 1 select *, month([order date]) mnth from orders where month([order date]) in (4,5) --step2 select [customer id], mnth, count(distinct [order id]) cnt from (select *, month([order date]) mnth from orders where month([order date]) in (4,5)) a group by [customer id],mnth --step3 select [customer id],MONTH([order date]) as month, COUNT(DISTINCT [order id]) count from orders where MONTH([order date]) in (4,5) group by [customer id],MONTH([order date]) select [customer id], case when months = 4 then 'April' When months = 5 then 'May' end as month_name, counts from (select [customer id], MONTH([order date]) as months, COUNT(DISTINCT [order id]) counts from orders where MONTH([order date]) in (4,5) group by [customer id],MONTH([order date]))inner_table; -- inner table alias name use. use