Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sql server sorting issues


Sql server sorting issues

Author
Message
moeessack123
moeessack123
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 55
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?
Brandie Tarvin
Brandie Tarvin
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7806 Visits: 8745
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.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
moeessack123
moeessack123
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 55
Let me give that a try.thanks
Any easier way?
Brandie Tarvin
Brandie Tarvin
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7806 Visits: 8745
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.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
moeessack123
moeessack123
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 55
Thanks
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10761 Visits: 12019
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

Jeff Atherton
Jeff Atherton
Right there with Babe
Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)Right there with Babe (721 reputation)

Group: General Forum Members
Points: 721 Visits: 240
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
mickyT
mickyT
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1253 Visits: 3309
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
Brandie Tarvin
Brandie Tarvin
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7806 Visits: 8745
NTILE?

This is the first time I've heard of that one. Thanks, Micky.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Brandie Tarvin
Brandie Tarvin
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7806 Visits: 8745
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.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
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