Search code examples
sqlsql-serverselectsql-order-bysql-server-2017

How to avoid the extra column used for ordering in the SELECT statement to the final output?


I have a table TestTable with columns ID, Workitem_Type, [STATUS], Workitem, CompletedDate, WORK, Log_createdDate

The data looks like:

+----+---------------+--------+-----------+-------------------------+-------+-------------------------+
| ID | Workitem_Type | STATUS | Workitem  |     CompletedDate       | WORK  |     Log_createdDate     |
+----+---------------+--------+-----------+-------------------------+-------+-------------------------+
|  1 | Remainder     | Pass   | Workitem1 | 2019-12-19 01:20:35.060 | Work1 | 2014-12-17 00:36:38.557 |
|  2 | Remainder     | Pass   | Workitem2 | 2019-12-19 01:20:35.060 | Work2 | 2015-12-17 00:36:38.557 |
|  3 | Remainder     | Pass   | Workitem3 | 2019-12-17 01:20:35.060 | Work3 | 2018-12-17 00:36:38.557 |
|  4 | Request       | Pass   | Workitem4 | 2019-12-18 01:20:35.060 | Work4 | 2017-12-17 00:36:38.557 |
|  5 | Remainder     | Pass   | Workitem5 | 2019-12-17 01:20:35.060 | Work5 | 2016-12-17 00:36:38.557 |
+----+---------------+--------+-----------+-------------------------+-------+-------------------------+

I have to give priority based on Workitem_Type, CompletedDate and should order by the same priority and Log_createdDate in single query. So the output should look like the below:

+----+---------------+--------+-----------+-------------------------+-------+-------------------------+
| ID | Workitem_Type | STATUS | Workitem  |      CompletedDate      | WORK  |     Log_createdDate     |
+----+---------------+--------+-----------+-------------------------+-------+-------------------------+
|  1 | Remainder     | Pass   | Workitem1 | 2019-12-19 01:31:12.620 | Work1 | 2014-12-17 00:36:38.557 |
|  2 | Remainder     | Pass   | Workitem2 | 2019-12-19 01:31:12.637 | Work2 | 2015-12-17 00:36:38.557 |
|  5 | Remainder     | Pass   | Workitem5 | 2019-12-17 01:31:12.637 | Work5 | 2016-12-17 00:36:38.557 |
|  3 | Remainder     | Pass   | Workitem3 | 2019-12-17 01:31:12.637 | Work3 | 2018-12-17 00:36:38.557 |
|  4 | Request       | Pass   | Workitem4 | 2019-12-18 01:31:12.637 | Work4 | 2017-12-17 00:36:38.557 |
+----+---------------+--------+-----------+-------------------------+-------+-------------------------+

I tried with the below query:

SELECT  *, CASE WHEN Workitem_Type = 'Remainder' THEN 
               CASE WHEN CompletedDate > GETDATE() THEN 1 ELSE 2 END 
           ELSE 3 END AS [Priority] 
FROM TestTable
ORDER BY [Priority], Log_createdDate;

But I don't want the Priority column to get appear in my output. So Is there anyway to get the above output without priority column in single query?

Please find the db<>fiddle for the same.


Solution

  • Just use the expression directly in the ORDER BY clause.

    SELECT * 
           FROM testtable
           ORDER BY CASE
                      WHEN workitem_type = 'Remainder' THEN 
                        CASE
                          WHEN completeddate > getdate() THEN
                            1
                          ELSE
                            2
                        END 
                      ELSE
                        3
                    END,
                    log_createddate;
    

    EDIT: (by gordon)

    You don't need to nest the CASE expressions:

       ORDER BY (CASE WHEN workitem_type = 'Remainder' AND completeddate > getdate()
                      THEN 1
                      WHEN workitem_type = 'Remainder' 
                      THEN 2
                      ELSE 3
                 END),
                log_createddate;