Author |
Topic |
scabral7
Yak Posting Veteran
57 Posts |
Posted - 2011-01-25 : 19:54:10
|
Hi,i have the follwing table:select '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdinto #tempUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdAcct_No svc_Dtime Qty cpt_cd12345 10-01-2010 1 800012345 10-01-2010 1 800012345 10-01-2010 1 8000 --remove(offset with -1)12345 10-01-2010 1 8000 --remove(offset with -1)12345 10-01-2010 -1 8000 --remove(offset with 1)12345 10-01-2010 -1 8000 --remove(offset with 1)67890 10-01-2010 1 800067890 10-01-2010 1 800067890 10-01-2010 1 8000 --remove(offset with -1)67890 10-01-2010 1 8000 --remove(offset with -1)67890 10-01-2010 -1 8000 --remove(offset with 1)67890 10-01-2010 -1 8000 --remove(offset with 1)i want to remove any offsetting records based on the Qty field for the same acct_no, svc_dtime and cpt_cd. For example in the above table, i would want to remove 2 negative 1 records and 2 positive 1 records for each acct and return the remaining fields:Acct_No svc_Dtime Qty cpt_cd12345 10-01-2010 1 800012345 10-01-2010 1 800067890 10-01-2010 1 800067890 10-01-2010 1 8000Is there a way to do this without using cursors?thanksscott |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-15 : 15:22:52
|
Select acct_no,svc_dtime,cpt_cd,qty from (Select *,ROW_NUMBER() over (partition by acct_no,svc_dtime,cpt_cd order by acct_no,svc_dtime,cpt_cd,qty desc)rowNo from (select '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdUNION ALLselect '12345' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cdUNION ALLselect '67890' as acct_no,'10-01-2010' as svc_dtime,-1 as qty,'8000' as cpt_cd)a)Final Where rowNo<=2 |
|
|
|
|
|