| Author |
Topic |
|
viallos
Starting Member
3 Posts |
Posted - 2012-02-22 : 18:55:22
|
I have to analyse massive amount of data and would like to remove unnecessery rows from my search results.It is periodical table that contain interest value for contract at the end of each month. In many examples rates are not changing for couple of months and this is the content I want to filtered out. I want to select only month when value have changed compared to last month. I can't do distinct as value can come back to previous value after number of month and I want to capture this change.My table is as follows:acc_no int_rate marg_rate cust_rate period_end111 3 1 2 31/01/2010111 3 1 2 28/02/2010111 3,5 1,5 2 31/03/2010111 3,5 2 1,5 30/04/2010111 3,5 2 1,5 31/05/2010111 3,5 2 1,5 30/06/2010111 3 1 2 31/07/2010111 3 1 2 31/08/2010222 4 2 2 31/01/2010222 4 2 2 28/02/2010222 4 2 2 31/03/2010222 5 3 2 30/04/2010222 5 3 2 31/05/2010222 5 3 2 30/06/2010222 4 2 2 31/07/2010222 4 2 2 31/08/2010333 3 1 2 31/01/2010333 3 1 2 28/02/2010333 4 2 2 31/03/2010333 4 2 2 30/04/2010333 5 3 2 31/05/2010333 5 3 2 30/06/2010333 3,5 2 1,5 31/07/2010333 3,5 2 1,5 31/08/2010 Expected results as followsacc_no int_rate marg_rate cust_rate period_end111 3 1 2 31/01/2010111 3,5 1,5 2 31/03/2010111 3,5 2 1,5 30/04/2010111 3 1 2 31/07/2010222 4 2 2 31/01/2010222 5 3 2 30/04/2010222 4 2 2 31/07/2010333 3 1 2 31/01/2010333 4 2 2 31/03/2010333 5 3 2 31/05/2010333 3,5 2 1,5 31/07/2010 Thanks for help in advance |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2012-02-23 : 02:43:56
|
| Try this..Select * from (Select row_number() over(partition by acc_no,int_rate,marg_rate,cust_rate order by period_end) as s_no,acc_no,int_rate,marg_rate,cust_rate,period_end)a where s_no=1Senthil Kumar C------------------------------------------------------MCITP - Database Administration SQL SERVER 2008MCTS - Database Development SQL SERVER 2008 |
 |
|
|
viallos
Starting Member
3 Posts |
Posted - 2012-02-23 : 04:33:22
|
| This is working perfectly.Thanks a million - this query reduced numbers of rows I have to review by 80%Really appreciated |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-23 : 06:35:41
|
NO, IT DOESN'T WORK!This line is removed by the flawed query!111 3 1 2 31/07/2010 N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-23 : 06:53:42
|
[code]DECLARE @Sample TABLE ( acc_no INT, int_rate MONEY, marg_rate MONEY, cust_rate MONEY, period_end DATE )SET DATEFORMAT DMYINSERT @SampleVALUES (111, 3 , 1 , 2 , '31/01/2010'), (111, 3 , 1 , 2 , '28/02/2010'), (111, 3.5, 1.5, 2 , '31/03/2010'), (111, 3.5, 2 , 1.5, '30/04/2010'), (111, 3.5, 2 , 1.5, '31/05/2010'), (111, 3.5, 2 , 1.5, '30/06/2010'), (111, 3 , 1 , 2 , '31/07/2010'), (111, 3 , 1 , 2 , '31/08/2010'), (222, 4 , 2 , 2 , '31/01/2010'), (222, 4 , 2 , 2 , '28/02/2010'), (222, 4 , 2 , 2 , '31/03/2010'), (222, 5 , 3 , 2 , '30/04/2010'), (222, 5 , 3 , 2 , '31/05/2010'), (222, 5 , 3 , 2 , '30/06/2010'), (222, 4 , 2 , 2 , '31/07/2010'), (222, 4 , 2 , 2 , '31/08/2010'), (333, 3 , 1 , 2 , '31/01/2010'), (333, 3 , 1 , 2 , '28/02/2010'), (333, 4 , 2 , 2 , '31/03/2010'), (333, 4 , 2 , 2 , '30/04/2010'), (333, 5 , 3 , 2 , '31/05/2010'), (333, 5 , 3 , 2 , '30/06/2010'), (333, 3.5, 2 , 1.5, '31/07/2010'), (333, 3.5, 2 , 1.5, '31/08/2010')-- Senthil_Nagore (9 rows returned) FAILSelect * from (Select row_number() over(partition by acc_no,int_rate,marg_rate,cust_rate order by period_end) as s_no,acc_no,int_rate,marg_rate,cust_rate,period_end from @sample)a where s_no=1-- SwePeso (11 rows returned)SELECT ACC_NO, INT_RATE, MARG_RATE, CUST_RATE, MIN(PERIOD_END) AS PERIOD_ENDFROM ( SELECT ACC_NO, INT_RATE, MARG_RATE, CUST_RATE, PERIOD_END, ROW_NUMBER() OVER (PARTITION BY ACC_NO ORDER BY PERIOD_END) - ROW_NUMBER() OVER (PARTITION BY ACC_NO, INT_RATE, MARG_RATE, CUST_RATE ORDER BY PERIOD_END) AS Block FROM @Sample ) AS dGROUP BY ACC_NO, INT_RATE, MARG_RATE, CUST_RATE, BLockORDER BY ACC_NO, PERIOD_END[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|