Author |
Topic |
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2012-04-03 : 16:39:51
|
Sample Datadeclare @t table (polnum char(8), eff_date int, trx_code char(2), cov_seq char(2))insert @t select 'L1234567','20120401', 'EA','01'union all select 'L1234567','20120401', 'GS','03'union all select 'L1234567','20120401', 'GS','04'union all select 'L1234567','20120401', 'FA','01'union all select 'L1234567','20120401', 'NA','01'union all select 'L1234567','20120401', 'FA','03'union all select 'L1234567','20120401', 'NA','03'union all select 'L1234567','20120401', 'FA','04'union all select 'L1234567','20120401', 'NA','04'union all select 'L1234567','20120402', 'NA','01'union all select 'L1234567','20120402', 'NA','03'union all select 'L1234567','20120402', 'NA','04'union all select 'L1234567','20120403', 'NA','01'union all select 'L1234567','20120403', 'NA','03'union all select 'L1234567','20120403', 'NA','04'union all select 'L9999999','20120401', 'GS','02'union all select 'L9999999','20120401', 'EA','01'union all select 'L9999999','20120401', 'NA','02'union all select 'L9999999','20120401', 'FA','01'union all select 'L9999999','20120401', 'NA','01'union all select 'L9999999','20120401', 'FA','02' here's what I have so farSELECT RANK() OVER (PARTITION BY polnum ORDER BYeff_date,CASE WHEN trx_code = 'EA' THEN -2 ELSE CASE WHEN trx_code = 'GS' THEN -1 ELSE 0 END END, cov_seq, CASE WHEN trx_code = 'FA' THEN -2 ELSE CASE WHEN trx_code = 'NA' THEN -1 ELSE 0 END END) AS [new_admin_seq], *FROM @t Expected Results1 L1234567 20120401 EA 012 L1234567 20120401 GS 033 L1234567 20120401 GS 044 L1234567 20120401 FA 014 L1234567 20120401 NA 016 L1234567 20120401 FA 036 L1234567 20120401 NA 038 L1234567 20120401 FA 048 L1234567 20120401 NA 0410 L1234567 20120402 NA 0111 L1234567 20120402 NA 0312 L1234567 20120402 NA 0413 L1234567 20120403 NA 0114 L1234567 20120403 NA 0315 L1234567 20120403 NA 041 L9999999 20120401 EA 012 L9999999 20120401 GS 023 L9999999 20120401 FA 013 L9999999 20120401 NA 015 L9999999 20120401 FA 025 L9999999 20120401 NA 02 Logic -The EA record always goes first. Followed by the GS record (ordered by the cov_seq). Followed by a pair of FA and NA records (again ordered based on cov_seq and for the same effdate as EA and GS). Followed by all other NAs (or any other trx_code) for the other effdates (ordered by effdate and cov_seq)My query gives thisnew_admin_seq polnum eff_date trx_code cov_seq-------------------- -------- ----------- -------- -------1 L1234567 20120401 EA 012 L1234567 20120401 GS 033 L1234567 20120401 GS 044 L1234567 20120401 FA 015 L1234567 20120401 NA 016 L1234567 20120401 FA 037 L1234567 20120401 NA 038 L1234567 20120401 FA 049 L1234567 20120401 NA 0410 L1234567 20120402 NA 0111 L1234567 20120402 NA 0312 L1234567 20120402 NA 0413 L1234567 20120403 NA 0114 L1234567 20120403 NA 0315 L1234567 20120403 NA 041 L9999999 20120401 EA 012 L9999999 20120401 GS 023 L9999999 20120401 FA 014 L9999999 20120401 NA 015 L9999999 20120401 FA 026 L9999999 20120401 NA 02 I still need to get the same sequence # for the FA and NA pairs as seen in the expected result.Been a while since I worked on SQL. Any help is appreciated. |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2012-04-03 : 18:48:21
|
Think this might work.Hope someone will come up with a better way to do it.SELECT RANK() OVER (PARTITION BY polnum ORDER BYeff_date,CASE WHEN trx_code = 'EA' THEN -2 ELSE CASE WHEN trx_code = 'GS' THEN -1 ELSE 0 END END, cov_seq, CASE WHEN trx_code IN ('FA','NA') THEN -1 ELSE 0 END) AS [new_admin_seq], *FROM @tORDER BY polnum, [new_admin_seq], trx_code |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-04 : 20:58:37
|
[code]declare @t table (polnum char(8), eff_date int, trx_code char(2), cov_seq char(2))insert @t select 'L1234567','20120401', 'EA','01'union all select 'L1234567','20120401', 'GS','03'union all select 'L1234567','20120401', 'GS','04'union all select 'L1234567','20120401', 'FA','01'union all select 'L1234567','20120401', 'NA','01'union all select 'L1234567','20120401', 'FA','03'union all select 'L1234567','20120401', 'NA','03'union all select 'L1234567','20120401', 'FA','04'union all select 'L1234567','20120401', 'NA','04'union all select 'L1234567','20120402', 'NA','01'union all select 'L1234567','20120402', 'NA','03'union all select 'L1234567','20120402', 'NA','04'union all select 'L1234567','20120403', 'NA','01'union all select 'L1234567','20120403', 'NA','03'union all select 'L1234567','20120403', 'NA','04'union all select 'L9999999','20120401', 'GS','02'union all select 'L9999999','20120401', 'EA','01'union all select 'L9999999','20120401', 'NA','02'union all select 'L9999999','20120401', 'FA','01'union all select 'L9999999','20120401', 'NA','01'union all select 'L9999999','20120401', 'FA','02'select rank() over (partition by polnumorder by case when trx_code in ('FA','NA') then 1else 0 end,eff_date,cov_seq) as rn,* from @toutput------------------------------------rn polnum eff_date trx_code cov_seq1 L1234567 20120401 EA 012 L1234567 20120401 GS 033 L1234567 20120401 GS 044 L1234567 20120401 FA 014 L1234567 20120401 NA 016 L1234567 20120401 FA 036 L1234567 20120401 NA 038 L1234567 20120401 FA 048 L1234567 20120401 NA 0410 L1234567 20120402 NA 0111 L1234567 20120402 NA 0312 L1234567 20120402 NA 0413 L1234567 20120403 NA 0114 L1234567 20120403 NA 0315 L1234567 20120403 NA 041 L9999999 20120401 EA 012 L9999999 20120401 GS 023 L9999999 20120401 FA 013 L9999999 20120401 NA 015 L9999999 20120401 FA 025 L9999999 20120401 NA 02[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|