Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rpc86
Posting Yak Master
200 Posts |
Posted - 2010-03-09 : 23:33:42
|
Hi guys,I need to get the last DatePost and last Balance based on the given CutOff Date as the parameter. Here is my actual table below:TableAcctNo Balance DatePost RowID123 1000 01/13/2010 1123 700 01/31/2010 2 123 300 02/25/2010 3123 200 03/15/2010 4456 2500 02/01/2010 5456 2000 02/14/2010 6456 1200 02/19/2010 7 @CutOff='02/28/2010'Result Needed:AcctNo Balance DatePost RowID123 300 02/25/2010 3456 1200 02/19/2010 7 Please help. Thank you |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-10 : 00:01:43
|
quote: Originally posted by rpc86 Hi guys,I need to get the last DatePost and last Balance based on the given CutOff Date as the parameter. Here is my actual table below:TableAcctNo Balance DatePost RowID123 1000 01/13/2010 1123 700 01/31/2010 2 123 300 02/25/2010 3123 200 03/15/2010 4456 2500 02/01/2010 5456 2000 02/14/2010 6456 1200 02/19/2010 7 @CutOff='02/28/2010'Result Needed:AcctNo Balance DatePost RowID123 300 02/25/2010 3456 1200 02/19/2010 7 Please help. Thank you
Please try TOP 2 and Order By clausedeclare @a table(acctno int,Balance int,DatePost datetime,RowID int )insert @aselect 123,1000,'01/13/2010' ,1 union allselect 123,700, '01/31/2010', 2 union allselect 123 ,300, '02/25/2010', 3 union allselect 123 ,200, '03/15/2010' , 4 union allselect 456 ,2500, '02/01/2010', 5 union allselect 456, 2000, '02/14/2010', 6 union allselect 456 ,1200, '02/19/2010', 7--select * from @aselect top 2 * from @a where '02/28/2010' between DatePost and '02/28/2010' order by DatePost desc outputacctno Balance DatePost RowID----------- ----------- ----------------------- -----------123 300 2010-02-25 00:00:00.000 3456 1200 2010-02-19 00:00:00.000 7(2 row(s) affected) |
 |
|
|
rpc86
Posting Yak Master
200 Posts |
Posted - 2010-03-10 : 00:16:29
|
| I don't need 2 records only, data above are only partial. I believe I need max here in relation with RowID, but I don't know how to do it. |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-10 : 00:26:50
|
quote: Originally posted by rpc86 I believe I need max here in relation with RowID, but I don't know how to do it.
Sorry, I really not getting You.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2010-03-10 : 01:53:38
|
| Is this you are looking for..declare @a table(acctno int,Balance int,DatePost datetime,RowID int )insert @aselect 123,1000,'01/13/2010' ,1 union allselect 123,700, '01/31/2010', 2 union allselect 123 ,300, '02/25/2010', 3 union allselect 123 ,200, '03/15/2010' , 4 union allselect 456 ,2500, '02/01/2010', 5 union allselect 456, 2000, '02/14/2010', 6 union allselect 456 ,1200, '02/19/2010', 7 --select * from @aSELECT * FROM @A as a where DATEPOST = (SELECT MAX(DATEPOST) FROM @A as bWHERE DATEPOST < '02/28/2010' AND B.ACCTNO = A.ACCTNO GROUP BY ACCTNO )Karthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-10 : 12:02:41
|
SQL 2005 solution using cross applySELECT t.AcctNo ,t.Balance,t.DatePost,t.RowIDFROM yourtable tCROSS APPLY(SELECT TOP 1 DatePost FROM YourTable WHERE AcctNo=t.AcctNo AND DatePost< @CutOffDate ORDER BY DatePost DESC)t1WHERE t.DatePost = t1.DatePost ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
RobertKaucher
Posting Yak Master
169 Posts |
Posted - 2010-03-10 : 18:57:44
|
quote: Originally posted by karthik_padbanaban Is this you are looking for..declare @a table(acctno int,Balance int,DatePost datetime,RowID int )insert @aselect 123,1000,'01/13/2010' ,1 union allselect 123,700, '01/31/2010', 2 union allselect 123 ,300, '02/25/2010', 3 union allselect 123 ,200, '03/15/2010' , 4 union allselect 456 ,2500, '02/01/2010', 5 union allselect 456, 2000, '02/14/2010', 6 union allselect 456 ,1200, '02/19/2010', 7 --select * from @aSELECT * FROM @A as a where DATEPOST = (SELECT MAX(DATEPOST) FROM @A as bWHERE DATEPOST < '02/28/2010' AND B.ACCTNO = A.ACCTNO GROUP BY ACCTNO )Karthikhttp://karthik4identity.blogspot.com/
I am not sure if that is what the original poster was looking for but you halped me greatly. I was looking for a way to perform exactly this type of sub-query. Thank you! |
 |
|
|
|
|
|
|
|