Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir. i have a sql query:


select
case when [P/A]='P' then Count([P/A]) end as 'P',
case when [P/A]='A' then Count([P/A]) end as 'A',
case when [P/A]='L' then Count([P/A]) end as 'L',EmpID
from attendence
where attendence.dated between '01/Feb/2016' and '28/Feb/2016' and empid=909 group by Empid,[P/A]

It show result like :-

[P] -- [A] -- [L] --[Empid]
C++
NULL --	3	-- NULL --	909
NULL --	NULL	-- 1	--      909
12   --	NULL	-- NULL	--      909


but i need Result like:-

[P] -- [A] -- [L] --[Empid]
12 -- 3 -- 1 -- 909

What I have tried:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Posted
Updated 21-May-16 2:14am
Comments
Richard MacCutchan 21-May-16 7:51am    
So you tried screaming "aaaaaa"; what happened then?
Karthik_Mahalingam 21-May-16 7:52am    
:)
TCS54321 21-May-16 7:55am    
my question not save without writing 30 words in what i have tried. its complicated to write what i have tried. thats why i am just typing this. u have any solution of my problem??
Karthik_Mahalingam 21-May-16 8:02am    
check out the solution and let me know if it helps

try this
SQL
 with T as (
select  
empid,
case when [P/A]='P' then 1 end as 'P',
case when [P/A]='A' then 1 end as 'A',
case when [P/A]='L' then 1 end as 'L' 
from attendance   where empid = 909 )
select sum(P) as P, sum(A) as A, sum(L) as L,empid  from T group by empid
 
Share this answer
 
Comments
TCS54321 21-May-16 8:01am    
thanku sir.. its work for me..
Karthik_Mahalingam 21-May-16 8:02am    
welcome
What you need is PIVOT[^]
Adapt this example:
SELECT empid, [P],[A],[L] FROM
(SELECT empid, [P/A]  FROM attendance) as src
PIVOT
(
   COUNT([P/A]) FOR [P/A] IN ([P],[A],[L])
) AS output
 
Share this answer
 
Comments
Karthik_Mahalingam 21-May-16 8:33am    
5,works fine. much simpler. but i dont know pivot :)
Peter Leow 21-May-16 8:44am    
Thank you, KARTHIK. Yours works too. 5!

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