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 2005 Forums
 Transact-SQL (2005)
 Order by clause for 2 fileds

Author  Topic 

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-05 : 18:22:50
Hi,

I have a query with isProduct set to (1 bit, null). I want the result set to be ordered by isProduct1 or isProduct2 values equal to true or 1 (it does not matter which one) to be on top. How can i do that? The way I have it currently. It does the first column correctly but the 2nd column is dependent on the first one and so all the true records are not on the top for the isProduct2 column. Please advice.

select a.isProduct1, a.isProduct2, a.col1, a.col2, b.col1
from table1 a
join table 2 b on b.id= a.id
Join table 3 c on c.name = a.name
where a.col1 = '1245'
order by a.isProduct1, a.isproduct2

SA

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-11-05 : 18:58:27
Do you have nulls in a column of bit data type?

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-05 : 19:00:23
yes

SA
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-05 : 19:47:54
I would be curious to know if this is even possible?

SA
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-11-05 : 20:20:25
Maybe something like this will help?

order by ISNULL(a.isProduct1,0) DESC, ISNULL(a.isproduct2,0) DESC

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

agarwasa2008
Posting Yak Master

109 Posts

Posted - 2009-11-05 : 20:42:42
It works correctly. I have never seen that syntax before can u explain it to me please. Thanks this is a life saver for me.

Thanks again!!

SA
Go to Top of Page
   

- Advertisement -