1

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?

0

1 Answer 1

2

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

Assuming this is for MS SQL Server

Sign up to request clarification or add additional context in comments.

2 Comments

isnull(rootid,id) seems a bit odd. Why not isnull(rootid,0)?
Can you offer any ideas how I can take my request and translate it, mentally, into a query? You're correct in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.