Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have a table for sql
SQL
alter PROCEDURE [dbo].[SpGetEmpAttendance_test]	
		
	@DateFrom datetime
	
	
AS
BEGIN
	
	
	select t5.SempAtndSempMstrId,SgShiftMaster.SshftShiftId,t5.SdeptDepartmentName,t5.available_strength  from (
	
	select t3.SempAtndSempMstrId,t3.SempAtndSempMstrSshftId,t4.SdeptDepartmentName,t3.available_strength from (
    
	
	select t1.SempAtndSempMstrId,t1.SempAtndSempMstrSshftId,t2.SempMstrSdeptId,t2.available_strength from (SELECT SempAtndSempMstrId,SempAtndSempMstrSshftId,SempMstrSdeptId
	from SgEmployeeAttendance inner join SgEmployeeMaster on SempAtndSempMstrId=SempMstrId and SempAtndSempMstrSshftId =SempMstrSshftId
	
	 where (SempAtndSempMstrSshftId=15 or SempAtndSempMstrSshftId=12 or SempAtndSempMstrSshftId=13 or SempAtndSempMstrSshftId=14 or SempAtndSempMstrSshftId=16)
	
	 and  cast(convert(varchar(10),SempAtndTimeIn,101)as datetime) =''+cast(convert(varchar(10),@DateFrom,101) as varchar)+''
	 )as t1 
	 
	 inner join (
select SempMstrSdeptId, COUNT(*)as available_strength from SgEmployeeMaster
 where (SempMstrSshftId=15 or SempMstrSshftId=12 or SempMstrSshftId=13 or SempMstrSshftId=14 or SempMstrSshftId=16) 
 group by SempMstrSdeptId)as t2 on t1.SempMstrSdeptId =t2.SempMstrSdeptId	 
 ) as t3
 
 inner join (select * from SgDepartment)as t4 on t3.SempMstrSdeptId = t4.SdeptId)as t5
 
 inner join SgShiftMaster on t5.SempAtndSempMstrSshftId =SgShiftMaster.SshftId
  order by t5.SdeptDepartmentName,t5.SempAtndSempMstrSshftId
 
	 
 
	
	
END


as below

SempAtndSempMstrId  SshftShiftId SdeptDepartmentName available_strength
3                         A                Attenders          6
16                        A                Attenders          6
17                        B                Attenders          6
21                        B                Attenders          6
15                        C                Attenders          6
22                        G1                 Doctors          4
18                        G2                 Doctors          4
20                        G1                Pharmasy          2
14                        G1                Pharmasy          2

then i want a result as below.

deptment    strength    A  B   C   G1  G2
-------      -------   --- --  --  --  --
Attenders       6        2  2   1   0   0

Doctors         4        0  0   0   1   1

Pharmasy        2        0  0   0   2   0


how to write a sql to get above result.
Thanks.


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 22-Jan-14 23:28pm
v2

You can make a pivot Select.

Try this:

SQL
SELECT SdeptDepartmentName, available_strength, A, B, C, G1, G2
FROM 
(SELECT SempAtndSempMstrId, SshftShiftId, SdeptDepartmentName, available_strength
FROM tmpTable) AS p
PIVOT
(
COUNT (SempAtndSempMstrId)
FOR SshftShiftId IN
( [A], [B], [C], [G1], [G2] )
) AS pvt


tmpTable is a temporary table where You store the result from Your query.
 
Share this answer
 
v2
Comments
hasbina 24-Jan-14 1:46am    
@Jorge
thank you sir..thanks a lots..
SQL
SELECT  SdeptDepartmentName AS deptname, available_strength AS strength,
[A],[B],[C],[G1],[G2]
FROM
(SELECT SdeptDepartmentName, available_strength, SshftShiftId FROM tablename) AS sourcetable
PIVOT
(
  COUNT(SshftShiftId) FOR SshftShiftId IN ([A],[B],[C],[G1],[G2])
)AS somename ORDER BY SdeptDepartmentName ASC
 
Share this answer
 
Comments
hasbina 24-Jan-14 1:45am    
@Peter
thank you sir..thanks a lots..

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