tags:

views:

149

answers:

2

I'll be the first one to admit my SQL skills are lacking. Now...

Let's say we've got a database table called Posts with three columns: Id, RootId, and DateTime.

What I'd like to do is get the last (by DateTime) 20 distinct RootId values.

Here's the caveat though: RootId may be NULL, in which case we should consider that record as distinct, and instead of NULL, Id should be returned.

What would the query be?

+2  A: 

select distinct top 20 isnull(rootid,id) from posts order by datetime desc

Assuming this is for MS SQL Server

YogoZuno
isnull(rootid,id) seems a bit odd. Why not isnull(rootid,0)?
JohnFx
Can you offer any ideas how I can take my request and translate it, mentally, into a query? You're correct in your answer.
Chris
JohnFx - read the original post. I've answered what was asked for...
YogoZuno
A: 

Hello, I did a article about this, you can acess

http://alinebossi.wordpress.com/2010/06/24/146/