Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to sort data based on two date columns among which one has to be grouped?

$
0
0


I have the following set of data.
select * from STATEMENT_HISTORY(nolock) order by stmt_dte desc

stmt_key	stmt_dte	stmt_start_dte
12	2013-10-13 00:00:00.000	2013-07-10 00:00:00.000
11	2013-10-12 00:00:00.000	2013-03-10 00:00:00.000
10	2013-10-11 00:00:00.000	2013-07-10 00:00:00.000
9	2013-10-10 00:00:00.000	2013-09-10 00:00:00.000
8	2013-09-10 00:00:00.000	2013-08-10 00:00:00.000
7	2013-08-10 00:00:00.000	2013-07-10 00:00:00.000
6	2013-07-10 00:00:00.000	2013-06-10 00:00:00.000
5	2013-06-10 00:00:00.000	2013-05-10 00:00:00.000
4	2013-05-10 00:00:00.000	2013-04-10 00:00:00.000
3	2013-04-10 00:00:00.000	2013-03-10 00:00:00.000
2	2013-03-10 00:00:00.000	2013-02-10 00:00:00.000
1	2013-02-10 00:00:00.000	2013-01-10 00:00:00.000

My requirement is as follows.
1. The row with latest stmt_dte should be on top
2. All other rows that match with the stmt_start_dte of the top row should take next place sorted by stmt_dte among them and so on.

Expected output should be in the following order.
stmt_key
12
10
7
11
3
9
8
6
5
4
2
1

How can I achieve it? Can some one suggest a better approach?





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>