| Author |
Topic |
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-29 : 06:52:52
|
| i have a similar problem.there is table where datas are getting saved as agentname saledate appno===============================ph0003 09/28/2009 A0010ph0003 09/28/2009 A0011ph0004 09/28/2009 A0012ph0004 09/28/2009 A0013ph0005 09/28/2009 A0014ph0005 09/28/2009 A0015ph0003 09/27/2009 A0002ph0003 09/27/2009 A0003ph0004 09/27/2009 A0004ph0004 09/27/2009 A0005ph0005 09/27/2009 A0006ph0005 09/27/2009 A0007the output should beAgentname 09/27/2009 09/28/2009 Total============================================ph0003 2 2 4ph0004 2 2 4Can anybody tell me how to do this?????somalia |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 06:54:03
|
| Read about PIVOT in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-29 : 08:07:31
|
| thanks madhi,this query is returning the total count per day,SELECT agentname, [09/28/2009] AS sep28, [09/26/2009] AS sep26, [09/25/2009] AS sep25, [09/24/2009] AS sep24 FROM (SELECT agentname, saledate, AppNo FROM vwTBL_MASTER_VISH) s PIVOT ( Count(AppNo) FOR saledate IN ([09/28/2009],[09/26/2009],[09/25/2009],[09/24/2009]) ) p ORDER BY [agentname]agntnm sep28 sep27 sep26 sep25cert01 3 1 1 0cert02 0 3 0 0cert03 0 3 0 0cert04 0 3 0 0cert05 0 4 0 0cert06 0 0 0 0CERT07 0 0 0 0CERT08 0 0 0 0however my next query is:i want the total count for each day and each agent as well like following:agntnm sep28 sep27 sep26 sep25 totalcert01 3 1 1 0 5cert02 0 3 0 0 3cert03 0 3 0 0 3cert04 0 3 0 0 3cert05 0 4 0 0 4cert06 0 0 0 0 0CERT07 0 0 0 0 0CERT08 0 0 0 0 0total 3 14 1 0 18can u help me in resolving this????somalia |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 08:09:58
|
| Try thisselect *,[09/28/2009]+[09/26/2009]+[09/25/2009]+[09/24/2009] from(SELECT agentname, [09/28/2009] AS sep28, [09/26/2009] AS sep26, [09/25/2009] AS sep25, [09/24/2009] AS sep24 FROM (SELECT agentname, saledate, AppNo FROM vwTBL_MASTER_VISH) s PIVOT ( Count(AppNo) FOR saledate IN ([09/28/2009],[09/26/2009],[09/25/2009],[09/24/2009]) ) p ) as tORDER BY [agentname]MadhivananFailing to plan is Planning to fail |
 |
|
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-29 : 08:14:26
|
This is not working ,[09/28/2009] is not a column name.Its the data in the column saledate.somalia |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 08:16:03
|
| I forget to note that. It should beselect *,sep28+ sep26+ sep25+ sep24 as total from(SELECT agentname, [09/28/2009] AS sep28, [09/26/2009] AS sep26, [09/25/2009] AS sep25, [09/24/2009] AS sep24 FROM (SELECT agentname, saledate, AppNo FROM vwTBL_MASTER_VISH) s PIVOT ( Count(AppNo) FOR saledate IN ([09/28/2009],[09/26/2009],[09/25/2009],[09/24/2009]) ) p ) as tORDER BY [agentname]MadhivananFailing to plan is Planning to fail |
 |
|
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-29 : 08:26:49
|
| its working perfect...but what about the last row total???it will give the total amount of appplications per daysomalia |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 08:32:06
|
| Where do you want to show data?MadhivananFailing to plan is Planning to fail |
 |
|
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-29 : 08:39:36
|
actually i want to generate a report in excel which would consist these above details.I want to do these sum,count jobs in backend and then fetch it in my frontend application by just giving parameters.So,For upto total application per agent is done.now the part which is left is application per day which i want to incorporate within this result.agntnm sep28 sep27 sep26 sep25 totalcert01 3 1 1 0 5cert02 0 3 0 0 3cert03 0 3 0 0 3cert04 0 3 0 0 3cert05 0 4 0 0 4cert06 0 0 0 0 0CERT07 0 0 0 0 0CERT08 0 0 0 0 0 total 3 14 1 0 18I hope I am able to make it understandable.. somalia |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 08:45:11
|
| [code]SELECT [agentname],COUNT(CASE WHEN saledate='09/28/2009' THEN AppNo ELSE NULL END) AS Sept28,COUNT(CASE WHEN saledate='09/28/2009' THEN AppNo ELSE NULL END) AS Sept29,..FROM tableGROUP BY AgentNameWITH ROLLUP[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-29 : 08:53:06
|
quote: Originally posted by somalia actually i want to generate a report in excel which would consist these above details.I want to do these sum,count jobs in backend and then fetch it in my frontend application by just giving parameters.So,For upto total application per agent is done.now the part which is left is application per day which i want to incorporate within this result.agntnm sep28 sep27 sep26 sep25 totalcert01 3 1 1 0 5cert02 0 3 0 0 3cert03 0 3 0 0 3cert04 0 3 0 0 3cert05 0 4 0 0 4cert06 0 0 0 0 0CERT07 0 0 0 0 0CERT08 0 0 0 0 0 total 3 14 1 0 18I hope I am able to make it understandable.. somalia
Use formula in EXCELMadhivananFailing to plan is Planning to fail |
 |
|
|
somalia
Starting Member
10 Posts |
Posted - 2009-09-30 : 08:06:35
|
| Thanks a lot madhi for the solution.somalia |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-30 : 08:10:38
|
quote: Originally posted by somalia Thanks a lot madhi for the solution.somalia
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
|