September 19, 2013 at 7:33 am
Hi Guys.I need some help
I'm doing a sellers report for my boss.
I have done a top sellers and it worked fine.I have even done a worst sellers and it worked
I need to do something that is neither top nor worst.
It has to be a middle sellers report
Does sql have any syntax for sorting that out
If i sort by asc it gives me the lowest figures
If i sort by desc it gives me highest figures.
Is there any way to sort the middle figures out?
September 19, 2013 at 7:37 am
Yes, you can sort out the middle. The easiest way to do it would be to create 2 temp tables. One for worst sellers, one for top sellers. Then join the sellers table to both of these tables and select everyone who does not exist in one of the other two tables.
Common Table Expressions would also work for this too, but I find temp tables the easiest to do for those who haven't learned CTEs yet.
September 19, 2013 at 7:39 am
Let me give that a try.thanks
Any easier way?
September 19, 2013 at 7:44 am
I know half a dozen ways to do what you've requested. But any easier than the easiest way I posted above?
If you find one, let me know.
September 19, 2013 at 7:51 am
Thanks
September 19, 2013 at 1:22 pm
Brandie Tarvin (9/19/2013)
I know half a dozen ways to do what you've requested. But any easier than the easiest way I posted above?If you find one, let me know.
Rather than writing the query you described as the final step of your method, wouldn't it be easier to write
SELECT * FROM #ALLSELLERS
EXCEPT SELECT * FROM #WORSTSELLERS
EXCEPT SELECT * FROM #TOPSELLERS
or even
SELECT * FROM #ALLSELLERS EXCEPT (
SELECT * FROM #WORSTSELLERS UNION ALL SELECT * FROM #TOPSELLERS)
if you think using brackets makes it less confusing.
Tom
September 19, 2013 at 2:52 pm
As a side note, this does get easier in SQL 2012 with OFFSET-FETCH:
http://www.dbadiaries.com/new-t-sql-features-in-sql-server-2012-offset-and-fetch
September 19, 2013 at 3:30 pm
Hi you could also use NTILE.
As an example
WITH sampledata AS (
SELECT *, NTILE(5) OVER (ORDER BY Numbers DESC) N
FROM (VALUES
('A',134)
,('B',122)
,('C',88)
,('D',82)
,('E',33)
,('F',22)
,('G',18)
,('H',4)
,('I',1)
,('J',1)
) SD(Book,Numbers)
)
SELECT Book, Numbers,
CASE WHEN N = 1 THEN 'TOP 20%'
WHEN N = 5 THEN 'BOTTOM 20%'
WHEN N = 3 THEN 'MIDDLE 20%'
ELSE ''
END
FROM sampledata
--WHERE N = 3
Edit: cleaned up query a bit to make it clearer
September 20, 2013 at 5:25 am
NTILE?
This is the first time I've heard of that one. Thanks, Micky.
September 20, 2013 at 5:26 am
L' Eomot Inversé (9/19/2013)
Brandie Tarvin (9/19/2013)
I know half a dozen ways to do what you've requested. But any easier than the easiest way I posted above?If you find one, let me know.
Rather than writing the query you described as the final step of your method, wouldn't it be easier to write
SELECT * FROM #ALLSELLERS
EXCEPT SELECT * FROM #WORSTSELLERS
EXCEPT SELECT * FROM #TOPSELLERS
or even
SELECT * FROM #ALLSELLERS EXCEPT (
SELECT * FROM #WORSTSELLERS UNION ALL SELECT * FROM #TOPSELLERS)
if you think using brackets makes it less confusing.
Given the question, I wasn't sure if the OP knew about the EXCEPT operator, so I opted to keep it simple with the very basics I thought (s)he would know.
September 20, 2013 at 6:09 am
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.
September 20, 2013 at 4:22 pm
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:w00t:) I can't envisage.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply