Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one table "tblEmployeeDetails" where employee details are saved.

Emp ID  Emp-Name Age  Date_of_Joining
-------------------------------------
E001    Mahesh   25   18-01-2016
E004    Suresh   29   13-01-2016 
E002    Kumar    23   10-01-2016
E003    Raj      27   26-01-2016


when search from date as '01-01-2016' and to date as 31-01-2016.The summary result will show in the Output as :

[01-2016]  [02-2016]  [03-2016] [04-2016] [05-2016]
----------------------------------------------------
   0           2          1        1         0
----------------------------------------------------


in the output, the header is showing [Week No - Year ],
from date and to date ,(1-jan-2016 to 31-Jan-2016) - 5 weeks are there.
so in the output it is showing 5 weeks with year.
in the 1st week, no employees have joined.
in the 2nd week , 2 employees have joined.
in the 3rd week , 1 employee has joined.
in the 4th week, 1 employee has joined.
in the 5th week, no employees has joined.

i don't know how to show this output..
Please help..
Thanks in advance...

Regards
Mahesh

What I have tried:

Emp ID  Emp-Name Age  Date_of_Joining
-------------------------------------
E001    Mahesh   25   18-01-2016
E004    Suresh   29   13-01-2016 
E002    Kumar    23   10-01-2016
E003    Raj      27   26-01-2016
Posted
Updated 28-Nov-16 0:07am
Comments
OriginalGriff 28-Nov-16 5:11am    
What have you tried?
Where are you stuck?
What help do you need?
Show us the SQL you have used, and explain what it does that you didn't expect, or doesn't do that you did.
Use the "Improve question" widget to edit your question and provide better information.
Mahesh Pattnayak 28-Nov-16 5:18am    
sir i don't know what to do in this ?
Mahesh Pattnayak 28-Nov-16 5:17am    
sir i don't know what to do in this ?

Quote:
sir i don't know what to do in this ?

So start by getting the data into a aggregated form:
SQL
SELECT DatePart(ww, Date_of_Joining) AS WeekNo, COUNT(*) AS PerMonth 
FROM tblEmployeeDetails 
GROUP BY DatePart(ww, Date_of_Joining)
This gives you the info you need, without the zeroed info, and without the PIVOT.
Next, add the zero information: That's complex, and the simplest way to do it is to create a table of all the week numbers: a single column, containing 1 to 53 inclusive.
SELECT w.WeekNo AS WeekNo, COUNT(s.EMPId) AS PerMonth 
FROM tblEmployeeDetails s
RIGHT JOIN dbo.WeekNumbers w ON w.WeekNo = DatePart(ww, s.Date_of_Joining)
WHERE w.WeekNo BETWEEN 1 AND 5
GROUP BY w.WeekNo

Now, you have the raw data you need:
WeekNo	PerMonth
1	0
2	0
3	2
4	1
5	1
And all you have left to do is PIVOT that. I'll leave that to you! :laugh:
 
Share this answer
 
v2
Comments
Mahesh Pattnayak 28-Nov-16 7:18am    
Sir here you have fixed the week no between 1 to 5 . but if the from date is 1-feb-2016 and to date is 1-Apr-2016, then how to get the week no .
OriginalGriff 28-Nov-16 7:24am    
Use
DATEPART(ww, ...
It returns the week number from a date.
Mahesh Pattnayak 28-Nov-16 7:23am    
and how to pivot the query if the date changed.
OriginalGriff 28-Nov-16 7:24am    
Use PIVOT!
Mahesh Pattnayak 28-Nov-16 7:27am    
in pivot, we have to mention the column names , but here how to mention the week names dyanamically. because if select 4 months data then 16 weeks, if select 2 months , then 8 weeks .
I'd say you have an almost identical case in the example 3 at Using PIVOT with SQL Server[^]

I hope that is useful for you
 
Share this answer
 

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