Trying to find an elegant way to split data into groups in SQL Server 2005 / SSRS
I'm trying to find the best way to split data into groups, for example Quintiles (groups of 5). I'll be using this SQL is to bring back data for an SSRS report.
What i'm currently doing is selecting 3 columns into a temporary table called #quintiles (customer ID, customer spend and adding a rownumber). The data is ordered by customer spend, highest to lowest.
-Edit-
Not too sure if I made it clear, but by ordering the temporary table by customer spend, when I break it down into groups, Q1 are开发者_运维技巧 the highest spenders, and so on, with Q5 being my bottom spenders.
Then I am using the following case statement to group them into 5. [tmp] is the alias for my temporary table #quintiles, as this case section is in the second SELECT statement
CASE
WHEN tmp.Row_number <= ((SELECT COUNT (*) FROM #quintiles)/5)
THEN 'Q1'
WHEN tmp.Row_number BETWEEN ((SELECT COUNT (*) FROM #quintiles)/5) AND ((SELECT COUNT (*) FROM #quintiles)*2/5)
THEN 'Q2'
WHEN tmp.Row_number BETWEEN ((SELECT COUNT (*) FROM #quintiles)*2/5) AND ((SELECT COUNT (*) FROM #quintiles)*3/5)
THEN 'Q3'
WHEN tmp.Row_number BETWEEN ((SELECT COUNT (*) FROM #quintiles)*3/5) AND ((SELECT COUNT (*) FROM #quintiles)*4/5)
THEN 'Q4'
WHEN tmp.Row_number > ((SELECT COUNT (*) FROM #quintiles)*4/5)
THEN 'Q5'
END
This works at the moment as it is, and does bring back the customers grouped into groups of 5. I couldn't just divide the total by 5 as if there is an odd number of customers I would end up with 0.5 customers in each group!
I just don't feel that this is the best way to have tackled the problem.
I hope it makes sense.
Use NTILE
SELECT 'Q' + CAST(NTILE(5) OVER (ORDER BY customer_spend) AS VARCHAR(1)) AS Quintile
Have you tried NTILE (ranking function) in the query? See here for details.
精彩评论