September 2, 2010 at 4:18 pm
I have a table with customerID,caegory,cnt.
I want select the top 3 cnt and category names (order by cnt desc) for each customerID.
And then convert rows to columns for each customer
Now the table looks like this:
customerID, Cat, Cnt
c1, cat1, 10
c1, cat2, 5
c1, cat3, 8
c1, cat4, 3
c1, cat5, 1
c2, cat9, 7
c2, cat10, 5
c3, cat1, 1
The result table:
customerID, CatName1,Cnt1,CatName2,Cnt2,CatName3,Cnt3
c1,cat1,10, cat3,8, cat2,5
c2,cat9,7,cat10,5,,
c3,cat1,1,,,,
Now, I put the the result to a temp table, for each patient I update the temp table, the performance was really ugly.
Any idea?
September 2, 2010 at 8:01 pm
declare @test-2 TABLE (customerID char(2), Cat varchar(5), Cnt int);
-- See how you start off by actually creating a table and then
-- inserting the data into it? Your doing this makes it a lot easier
-- for all of us volunteers to help you. So, help us help you.
-- See http://www.sqlservercentral.com/articles/Best+Practices/61537/
-- for more details on how to do all of this.
INSERT INTO @test-2
SELECT 'c1', 'cat1', 10 UNION ALL
SELECT 'c1', 'cat2', 5 UNION ALL
SELECT 'c1', 'cat3', 8 UNION ALL
SELECT 'c1', 'cat4', 3 UNION ALL
SELECT 'c1', 'cat5', 1 UNION ALL
SELECT 'c2', 'cat9', 7 UNION ALL
SELECT 'c2', 'cat10', 5 UNION ALL
SELECT 'c3', 'cat1', 1;
;WITH CTE AS
(
SELECT *,
RN = ROW_NUMBER() OVER (PARTITION BY customerID ORDER BY Cnt DESC)
FROM @test-2
)
SELECT customerID,
CatName1 = MAX(CASE WHEN RN = 1 THEN Cat ELSE NULL END),
CatQty1 = MAX(CASE WHEN RN = 1 THEN Cnt ELSE NULL END),
CatName2 = MAX(CASE WHEN RN = 2 THEN Cat ELSE NULL END),
CatQty2 = MAX(CASE WHEN RN = 2 THEN Cnt ELSE NULL END),
CatName3 = MAX(CASE WHEN RN = 3 THEN Cat ELSE NULL END),
CatQty3 = MAX(CASE WHEN RN = 3 THEN Cnt ELSE NULL END)
FROM CTE
WHERE RN <= 3
GROUP BY customerID;
Please see the CrossTabs and Pivots, Parts 1 and 2 links in my signature.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 3, 2010 at 9:51 am
Thank you so so so much. That's exactly what I want.:-P
I really appreciate your help.
September 3, 2010 at 1:31 pm
Nicely done, Wayne.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 3, 2010 at 4:28 pm
Thanks Jeff. You're a good teacher!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply