| Author |
Topic |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-11-19 : 22:01:52
|
| Hi,I have a problem with my current stored procedure that is using cursor. It's slow and causing timeout expired for it computes a very large volume of records. Thus, i need to change the query without using cursor.Below is the sample data:CREATE TABLE EmpLevy (EmpCode nvarchar(8), WDate date, Amount decimal, MaxAmt decimal)INSERT INTO EmpLevy VALUES ('001','20070101',10,50)INSERT INTO EmpLevy VALUES ('001','20070102',10,50)INSERT INTO EmpLevy VALUES ('001','20070103',10,50)INSERT INTO EmpLevy VALUES ('001','20070104',10,50)INSERT INTO EmpLevy VALUES ('001','20070105',5,50)INSERT INTO EmpLevy VALUES ('001','20070106',10,50)INSERT INTO EmpLevy VALUES ('001','20070107',5,50)INSERT INTO EmpLevy VALUES ('002','20070101',10,30)INSERT INTO EmpLevy VALUES ('002','20070102',10,30)INSERT INTO EmpLevy VALUES ('002','20070103',5,30)INSERT INTO EmpLevy VALUES ('002','20070104',10,30)Expected result:EmpCode WDate Amount RunningTotal MaxAmount 001 20070101 10 10 50001 20070102 10 20 50001 20070103 10 30 50001 20070104 10 40 50001 20070105 5 45 50001 20070107 5 50 50002 20070101 10 10 30002 20070102 10 20 30002 20070103 5 25 30Thanks for your attention. I hope you guys can help to solve this.Cheers,erwine... sql is fun... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-19 : 22:08:36
|
What happen to this 2 records ?INSERT INTO EmpLevy VALUES ('001','20070106',10,50)INSERT INTO EmpLevy VALUES ('002','20070104',10,30) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-19 : 22:16:53
|
try thisSELECT e.EmpCode, e.WDate, e.Amount, RunningTotal = (SELECT SUM(Amount) FROM EmpLevy x WHERE x.EmpCode = e.EmpCode AND x.WDate <= e.WDate), e.MaxAmtFROM EmpLevy eORDER BY EmpCode, WDate KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-11-19 : 22:26:24
|
| Hi KH,Thanks again.Those two records shouldn't be included because if it will cause the running total more than its maximum amount paid per month.I was able to produce the result of your query too, but I'm kind of thinking if someone can help me to generate the expected result (without those two records) directly rather than occupying another temp table to eliminate the unwanted records.Cheers,erwine... sql is fun... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-19 : 22:36:42
|
quote: Those two records shouldn't be included because if it will cause the running total more than its maximum amount paid per month.
Aiyah, then should say so lar.
SELECT *FROM( SELECT e.EmpCode, e.WDate, e.Amount, RunningTotal = (SELECT SUM(Amount) FROM EmpLevy x WHERE x.EmpCode = e.EmpCode AND x.WDate <= e.WDate), e.MaxAmt FROM EmpLevy e) aWHERE RunningTotal <= MaxAmtORDER BY EmpCode, WDate KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-11-20 : 00:51:49
|
haha..thanks.. ... sql is fun... |
 |
|
|
|
|
|