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
 Transact-SQL (2000)
 Help needed in SQL Query!

Author  Topic 

padsp
Starting Member

21 Posts

Posted - 2005-09-19 : 13:24:33
Hi,

I'm using Sql Server 2000, don't know how to order result set.
I have table called TAB1 has the following structure with sample data:
Table TAB1:
EMP_ID EST_ID MTG_ID DTUPD DTNH
164 12 219 2003-01-18 1900-01-01
164 12 340 2003-04-22 1900-01-01
164 24 800 1900-01-01 2004-06-06
164 24 807 2005-06-12 1900-01-01

I would like to show the result set as follows:
Table TAB1:
EMP_ID EST_ID MTG_ID DTUPD DTNH
164 24 807 2005-06-12 1900-01-01
164 24 800 1900-01-01 2004-06-06
164 12 340 2003-04-22 1900-01-01
164 12 219 2003-01-18 1900-01-01

Basically i would like to sort by latest date(DTUPD,DTNH).
I'm looking for your help.
I've tried query like this,But no luck
SELECT * FROM TAB1 WHERE EMP_ID=164 ORDER BY DTUPD,DTNH desc

Thanks
Bob

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-09-19 : 13:35:17
try changing
ORDER BY DTUPD,DTNH desc
to ORDER BY DTUPD desc, DTNH desc

you have to put desc to every column name in the order by list
if you want to order them all in the descending way.



Go with the flow & have fun! Else fight the flow
Go to Top of Page

padsp
Starting Member

21 Posts

Posted - 2005-09-19 : 13:50:35
Hi spirit1,
I have tried as you have suggested, this is what i'm getting,
Table TAB1:
EMP_ID EST_ID MTG_ID DTUPD DTNH
164 24 807 2005-06-12 1900-01-01
164 12 340 2003-04-22 1900-01-01
164 12 219 2003-01-18 1900-01-01
164 24 800 1900-01-01 2004-06-06

The query i've used,
SELECT * FROM TAB1 WHERE EMP_ID=164 ORDER BY DTUPD desc,DTNH desc
The problem is here order works independently. but i would like order both cols together.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-09-19 : 14:32:20
try:

Order By (case when DTUPD > DTNH then DTUPD else DTNH end) desc

Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page
   

- Advertisement -