Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query From Heck!

Status
Not open for further replies.

PDL

IS-IT--Management
Dec 11, 2000
36
US
How would one write a query that would order a set of records based upon the either

a)the latest of two dates within each record and given the possibility one datefield may be blank/null or
b) the date of the field that was not null

ordered by date


Record 1: field1 field2 12/12/00 3/3/01
Record 2: field1 field2 11/11/00 2/2/01
Record 3: field1 field2 12/15/00 null

Need Records returned as

Record 3
Record 2
Record 1

 
If I were you I would write a cursor to perform this since you are going to want each row anaylyzed as it is selected.
 
select * from table
order by case
when field1 is null then field2
when field2 is null then field2
when field1 >= field2 then field1
else field2
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top