Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Order Query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-06-08 : 08:03:56
Eduardo writes "Hello, I Want to Know how I must Writte a query to Order This Table:

Id        After_of_Id

=================
1         1

2         4

3         9

4         7

5         8

6         9

7         4

8         2

9         8

10        1


The Result must to be like This

Id
===
1
10
2
8
5
9
3
6
7
4

Thanks a Lot.

Eduardo"

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-08 : 12:16:27
SELECT Id
FROM Table1
ORDER BY After_of_Id, Id


Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-08 : 12:22:31
That's GOT to be a homework question....



Brett

8-)
Go to Top of Page

drymchaser
Aged Yak Warrior

552 Posts

Posted - 2004-06-08 : 12:31:41
[confused]
I did not see any logical solution to sort the table the way Eduardo suggests.
[/confused]
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-08 : 12:36:21
quote:
Originally posted by drymchaser

[confused]
I did not see any logical solution to sort the table the way Eduardo suggests.
[/confused]



True...but I bet the requested result set is mixed up....




Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-08 : 12:38:04
The data in After_of_Id specifies the order. Then when there are duplicates of After_of_Id, order by Id.

Tara
Go to Top of Page

drymchaser
Aged Yak Warrior

552 Posts

Posted - 2004-06-08 : 12:50:37
quote:
Originally posted by X002548

quote:
Originally posted by drymchaser

[confused]
I did not see any logical solution to sort the table the way Eduardo suggests.
[/confused]



True...but I bet the requested result set is mixed up....




Brett

8-)



Really messed up...
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-08 : 13:02:36
quote:
Originally posted by tduggan

The data in After_of_Id specifies the order. Then when there are duplicates of After_of_Id, order by Id.

Tara



Look closer...



Brett

8-)
Go to Top of Page

joldham
Wiseass Yak Posting Master

300 Posts

Posted - 2004-06-08 : 13:04:16
Tara,

Your result set will return

ID
1
10
8
2
7
4
5
9
3
6

Which is not the result set he wants.

He basically wants to sort by the After_of_ID column for first record and then sort by the After_of_id of the ids returned from the first sort order. I don't have time to look at it right now, but the only wa I can see to return this would be to generate a temp table of some kind. Unless I missed something obvious.

Jeremy W. Oldham
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-08 : 13:05:40
quote:
Originally posted by X002548



Look closer...



Got to stop doing shots in the morning.

Tara
Go to Top of Page

joldham
Wiseass Yak Posting Master

300 Posts

Posted - 2004-06-08 : 13:26:33
There also appears to be a problem with his result set. He has id 2 in third place when the After_of_id column says it should not appear until after id 4 making id 2 the last record in the result set instead of the 3rd record. Or should id 2 have an after_of_id of 10?

Eduardo can you help us out here to make sure we are following your logic? Do you want the result in a single result set or is a stored procedure?

Jeremy W. Oldham
Go to Top of Page
   

- Advertisement -