Author |
Topic |
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-07-03 : 18:22:17
|
Hi friendsI have the following tableCustomers with following fieldsID,Counter,Nameregular ,8,JohnWeekly,10,John,Monthly,9,JohnI want to write a query for getting the resukts as followsName,regularcount,Weeklycount,MonthlycountJohn,8,10,9How can I achieve this..Thank you in advance... |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-03 : 20:28:43
|
You can use the PIVOT operator if you know the ID values in advance.SELECT *FROM YourTablePIVOT( SUM(counter) FOR ID IN ([regular],[Weekly],[Monthly]))p If you don't know the ID's in advance, you will need to more work - for example, using dynamic pivot. See Madhivanan's blog for code and examples: http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-07-04 : 17:31:08
|
thank you sunita beck...it works the way i want..but i need one more help my id has one more value called dailyID,Counter,Nameregular ,8,JohnWeekly,10,John,Monthly,9,JohnDaily,2,Johnso my query now shud be liek thisName,regularcount,Weeklycount,MonthlycountJohn,11(regular+daily),10,9Can i achieve this..thank you |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-04 : 18:51:22
|
[code]SELECT Name,[regular] + [Daily] AS regularcount,[Weekly] as weeklycount,[Monthly] as monthlycountFROM YourTablePIVOT( SUM(counter) FOR ID IN ([regular],[Weekly],[Monthly],[Daily]))p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|