tags:

views:

24

answers:

2

Hello friend, I want to make a query that will give the result on order by using two columns. I made query like this.

select 
    el.*,
    lb.LabName,lb.LabType,
    lb.LabDescription
from encounterlab el
INNER JOIN
    labs lb
    ON 
    lb.LabType=el.LabType
where 
    PatientAcctNo=4 ORDER BY el.DataOfService,lb.LabName DESC

It gives the results.But My aim is IF in the DataOfService contain the same date I want to make the Order depending upon the LabName and this should be ASC

+3  A: 

you can optionally specify the order for each single field in the order by statement

ORDER BY field1 DESC, field2 ASC
zolex
Thanks zolex.Working Fine.One More doubt like this we can Order n number of columns right?
vinothkumar
yes you can do that with as many cols as you want
zolex
A: 

If I understand you correctly, your current query orders in the opposite direction that you want (for both columns!). Ordering ascending (ASC) is implicit, and you always have a direction per column you're ordering by. So your example:

ORDER BY el.DataOfService,lb.LabName DESC

Is the same as:

ORDER BY el.DataOfService ASC, lb.LabName DESC

So what you want is this:

ORDER BY el.DataOfService DESC, lb.LabName
Blixt