SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sql server sorting issues


Sql server sorting issues

Author
Message
Andreas P. Williams
Andreas P. Williams
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1615 Visits: 5694
Hi, mickyT!

Your method only works if the number of rows is a multiple of 5. Try and remove the ('J',1) and your bottom 20 per cent has only one record.
mickyT
mickyT
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2878 Visits: 3318
Andreas P. Williams (9/20/2013)
Hi, mickyT!

Your method only works if the number of rows is a multiple of 5. Try and remove the ('J',1) and your bottom 20 per cent has only one record.


True .. but with more records in the result sets and a higher NTILE value, this could be refined. The other problem with this is that it will pretty much do only percentages rather than number of records.

RANK could also be used for this with a little effort
WITH cte AS (
SELECT Name, Quantity,
RANK() OVER (ORDER BY Quantity DESC) TopRank,
RANK() OVER (ORDER BY Quantity ASC) BottomRank,
COUNT(*) OVER (ORDER BY Quantity) / 2 Midpoint
FROM Books
)
SELECT Name, Quantity, TopRank,
CASE WHEN TopRank <= 100 THEN 'Best Seller'
WHEN BottomRank <= 100 THEN 'Worst Seller'
ELSE 'Average Seller'
END
FROM cte
WHERE TopRank <= 100 or
BottomRank <= 100 or
TopRank between Midpoint - 50 and Midpoint + 50



Unfortunately I can't test and refine this at the moment, so there may be some quirks (errors:w00tSmile I can't envisage.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search