Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider a table containing a list of employees and sales done by employees, the question is I need to display their sales details depend on the date i.e. on Quarter basis. (all four quarters of a year).


For example:
Date        | EmpName  | ProductSell
--------------------------------------------- 
1st Quarter | A        |    3
            | B        |    6
----------------------------------------------
2nd Quarter | C        |    12 
-----------------------------------------------

Please do reply a query for it.

What I have tried:

I have done with join tables and get their name of the month and by in() like [In('january','february','march')] and used group by month name.
Posted
Comments
Tomas Takac 12-Nov-17 10:10am    
You actually didn't state what your problem is. Please put your code in the "What I have tried" section and explain what is not working.

BTW you can use the DATEPART function[^] to get the quarter.

1 solution

CREATE TABLE #Sales(Empname CHAR(2),ProductSell INT,SellingDate DATE);

INSERT INTO #Sales 
  VALUES('A',7,'2017-11-01'),
        ('A',5,'2016-12-14'),
        ('C',5,'2017-02-11'),
        ('A',5,'2017-05-11'),
        ('B',10,'2016-11-04'),
        ('B',3,'2017-09-05'),
        ('D',1,'2017-11-05'),
         ('A',4,'2017-11-05');

SELECT Empname,SUM(productsell) AS productsell,YEAR(SellingDate) AS SaleYear,
       (CASE WHEN DATEPART(QQ,SellingDate)=1 THEN '1st QUARTER'
            WHEN DATEPART(QQ,SellingDate)=2 THEN '2nd QUARTER'
            WHEN DATEPART(QQ,SellingDate)=3 THEN '3rd QUARTER'
            WHEN DATEPART(QQ,SellingDate)=4 THEN '4th QUARTER'
                  END) AS Sale_QUARTER,
       MAX(DateName(MOnth,SellingDate)) AS Sale_MONTH
  FROM #Sales
     GROUP BY YEAR(SellingDate), DATEPART(QQ,SellingDate),
               DATEPART(MONTH,SellingDate), Empname 
      ORDER BY YEAR(SellingDate),DATEPART(QQ,SellingDate),DATEPART(MONTH,SellingDate); 

--------------------------------------------------------------------
Empname	productsell	SaleYear	Sale_QUARTER	Sale_MONTH
------------------------------------------------------------------------
B 	10	2016	4th QUARTER	November
A 	5	2016	4th QUARTER	December
C 	5	2017	1st QUARTER	February
A 	5	2017	2nd QUARTER	May
B 	3	2017	3rd QUARTER	September
A 	11	2017	4th QUARTER	November
D 	1	2017	4th QUARTER	November
 
Share this answer
 
Comments
cgprakash 22-Jan-18 1:28am    
Thnq Santosh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900