Solved

SortBy question

Posted on 2011-03-19
3
322 Views
Last Modified: 2012-08-14
I have a table storing topics. Each topic contains 2 smalldatetime fields: "Created" and "LastReply". If the topic has no replies the "LastReply" field is NULL.

I need to make a SELECT statement that get the last activity. This means: I need to get the 5 last records that is created OR replied.

The question is how to do this? Is the solution to compare "Created" and "LastReply" for each record, and make a new field (called "LastActivity") with the "newest" date? Then, I need to sort the records by "LastActivity"?

The table is like this:

ID (int)
Title (varchar)
Created (SmallDateTime)
LastReply (SmallDateTime)


How to solve this? Hope someone please can help me!
0
Comment
Question by:webressurs
3 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35173373
Try this query
SELECT * FROM YourTable 
ORDER BY ISNULL(LastReply, Created) DESC

Open in new window


Raj
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35174018
you need this.
SELECT   * 
FROM     (SELECT *, 
                 CASE 
                   WHEN Created > LastReply THEN Created 
                   ELSE ISNULL(LastReply,Created) 
                 END AS LastActivity 
          FROM   your_table) AS t1 
ORDER BY LastActivity DESC

Open in new window

0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 35179201
Thanks :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now