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 |
|
jai2808
Starting Member
27 Posts |
Posted - 2009-06-17 : 09:32:10
|
| Hi,I need to generate a record set based on the logic mentioned below.Date Name Amt1-Jan-09 A 1002-Jan-09 A 2003-Jan-09 A 3004-Jan-09 A 6005-Jan-09 A 506-Jan-09 A 50I need to get only those records where when i add 5 days to date and the sum of amount is more then 500.for eg., for first record, i add 5 days and check the sum of amt then the first record should be the part of resultset.For second record, i add 5 days and the amount is more than 500.But for 5th record, i add 5 days and check the sum of amount then i get only 100.Can some one help how to get the logic.I am looping through each row which is taking lot of time as we have nearly 2 lac records. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-17 : 09:34:14
|
What is the expected output from the sample data above? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-17 : 09:41:20
|
See this blog post http://weblogs.sqlteam.com/peterl/archive/2007/12/10/Cursor-is-really-faster-than-set-based-solution.aspx-- Prepare sample dataDECLARE @Sample TABLE ( Date DATETIME, Name VARCHAR(20), Amt MONEY )INSERT @SampleSELECT '1-Jan-09', 'A', 100 UNION ALLSELECT '2-Jan-09', 'A', 200 UNION ALLSELECT '3-Jan-09', 'A', 300 UNION ALLSELECT '4-Jan-09', 'A', 600 UNION ALLSELECT '5-Jan-09', 'A', 50 UNION ALLSELECT '6-Jan-09', 'A', 50-- Solution by PesoSELECT DATEADD(DAY, -v.Number, s.Date) AS theDate, s.Name AS theName, SUM(s.Amt) AS theSumFROM @Sample AS sINNER JOIN master..spt_values AS v ON v.Type = 'P' AND v.Number < 5GROUP BY DATEADD(DAY, -v.Number, s.Date), s.NameHAVING MIN(v.Number) = 0 AND SUM(s.Amt) > 500ORDER BY DATEADD(DAY, -v.Number, s.Date) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-17 : 10:20:39
|
quote: Originally posted by jai2808 Hi,I need to generate a record set based on the logic mentioned below.Date Name Amt1-Jan-09 A 1002-Jan-09 A 2003-Jan-09 A 3004-Jan-09 A 6005-Jan-09 A 506-Jan-09 A 50I need to get only those records where when i add 5 days to date and the sum of amount is more then 500.for eg., for first record, i add 5 days and check the sum of amt then the first record should be the part of resultset.For second record, i add 5 days and the amount is more than 500.But for 5th record, i add 5 days and check the sum of amount then i get only 100.Can some one help how to get the logic.I am looping through each row which is taking lot of time as we have nearly 2 lac records.
do you mean thisSELECT t.*FROM YourTable tCROSS APPLY (SELECT SUM(Amt) AS Total FROM YourTable WHERE Name = t.Name AND Date >= t.Date AND Date <= DATEADD(dd,5,t.Date) )t1WHERE t1.Total >=500 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-17 : 10:28:14
|
Visakh, why do I get this result from my query2009-01-01 00:00:00.000 A 1250,002009-01-02 00:00:00.000 A 1200,002009-01-03 00:00:00.000 A 1000,002009-01-04 00:00:00.000 A 700,00 and this result from your query2009-01-01 00:00:00.000 A 100,002009-01-02 00:00:00.000 A 200,002009-01-03 00:00:00.000 A 300,002009-01-04 00:00:00.000 A 600,00 Have we interpreted OP's request totally different? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-17 : 10:32:40
|
| sorry i cant test now...i dont have a sql box nearby... |
 |
|
|
jai2808
Starting Member
27 Posts |
Posted - 2009-06-18 : 02:07:35
|
| Thanks Visakh16 your solution worked.Peso your solution also worked.Thanks for both of you for helping me. |
 |
|
|
|
|
|
|
|