How to return a few of Resultset rows at the bottom of the entire resultset?

In the ORDER BY clause we have to use CASE statement to achieve result. We can add more case statement if we have more than one rows to treat differently.

In this following diagram you can notice that there are two rows which are with ProductID 712 and 715. Now our final requirement is that we want row 715 to be the second last row in the resultset and 712 as a very last row in the resultset.

Here is the script…

SELECT [ProductID], COUNT(*) CountofProductID
FROM [Sales].[SalesOrderDetail]
WHERE ProductID BETWEEN 707 AND 716
GROUP BY [ProductID]
ORDER BY CASE WHEN [ProductID] = 715 THEN 1
WHEN [ProductID] = 712 THEN 2 ELSE 0 END

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s