This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output for finding the MIN & MAX product unit sold of a month.
Input Data:
1 2 3 4 5 6 7 8 9 10 |
Month_No Product_Name Total_Unit_Sold ----------- -------------------- --------------- 1 computer 20 1 mobile 50 1 laptop 30 2 computer 40 1 speaker 70 2 mobile 60 2 laptop 60 2 speaker 90 |
Expected Output:
1 2 3 4 |
Month_No Min_Sales Max_Sales ----------- ----------- ----------- 1 20 70 2 40 90 |
Create a sample table:
1 2 3 4 5 6 7 8 9 10 11 |
create table tbl_Products ( Month_No int, Product_Name varchar(20), Total_Unit_Sold int ) insert into tbl_Products values (1,'computer',20),(1,'mobile',50),(1,'laptop',30) ,(2,'computer',40),(1,'speaker',70),(2,'mobile',60) ,(2,'laptop',60),(2,'speaker',90) |
Solution using simple Group By:
1 2 3 4 5 6 |
select Month_No ,min(Total_Unit_Sold) as Min_Sales ,max(Total_Unit_Sold) as Max_Sales from tbl_Products group by Month_No |
Check the below input data and expected output to find a distinct combination of the cross columns.
Leave a Reply