SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 07-03-2008, 08:37 AM
Unregistered
 
Posts: n/a
Default ANY dialect question:

Complicated sorting in one sql


Hello,

I am trying to write a query that returns this result set:

Table “Tests”:
Code:
Id    Test
1    B
2    B
3    C
4    A
5    B
Result Set:
Code:
Id    Test
1    B
2    B
5    B
3    C
4    A
The tests are grouped by name, but the order is by id.

I have come up with this:
SELECT Id, Test,
RANK() OVER(PARTITION BY Test ORDER BY Id) Rank1
FROM Tests

The result set groups the tests by name as needed, but the order is
by test, not by id...

any ides?

10x,
Alex
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 07-03-2008, 09:02 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 93
ben has disabled reputation
Default ANY answer. Re: Complicated sorting in one sql

I am not 100% sure I understand what you are after but if you want to order the result set by name as well as by Id:

SELECT Id, Test
FROM Tests
ORDER BY Test, Id
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-03-2008, 09:16 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: Complicated sorting in one sql

no, i cant use ORDER BY because the results are not ordered
by Id or by Test.
They are grouped (for display) by Test but ordered by Id.
i.e.:
Test "B" willl apear first because it has the smallest Id. Since the
results are grouped by Test all the rest of the B's will appear directly after it.
Then the groupd of Test "A", and last the group of Test "C"

10x,
Alex
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-07-2008, 08:39 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: Complicated sorting in one sql

This should be easy (if I understand correctly):
1) select the groups the and the minimum for each group (as table T)
2) order T by id (as table T1)
3) select from T1 join Tests on Tests.test = T1.test
Derived tables in sub-selects.

Do you need more detailed help?
Did it work?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:32 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8