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 |
|
marianux
Starting Member
3 Posts |
Posted - 2011-12-27 : 18:11:28
|
| Hi everyone,id like to know how could i get some rows that some sum of some columns is equal or less than a number and ordered by datetime ASC.I know i could do it with a cursor but id prefer to do it with a select statement if its possibleLet's see.I got this table:ID STAR DATETIME DURATION (HOURS)627 2011-08-03 17:00:00.000 2628 2011-08-05 17:00:00.000 2629 2011-08-08 17:00:00.000 2630 2011-08-10 17:00:00.000 2631 2011-08-12 17:00:00.000 21º) Well, imagine we are at 2011-08-01, and id like to get all the rows from table 'till sum(duration) <= 4.Result must get rows with ID 627 and 6282º) If we were at 2011-08-07 and id like to get all the rows from table 'till sum(duration) <= 4. then rows would be with ID 629, 6303º) If we were at 2011-08-11 and id like to get all the rows from table 'till sum(duration) <= 4. then rows would be with ID 6314º) If we were at 2011-08-27 and id like to get all the rows from table 'till sum(duration) <= 4. then no rows would be retrievedKey factor is that value that is compared with sum(duration) (<= 4) is dinamyc, is not always the same.Anyone could take a look at this.Thanks in advance. |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-12-27 : 20:10:11
|
Probably easiest approach (from the perspective of programming efforts) would be to use a recursive CTE, like this:DECLARE @x INT; SET @x = 4;DECLARE @startDate DATETIME; SET @startDate = '20110827';;WITH cte AS( SELECT TOP 1 id, [DURATION (HOURS)] AS cumulativeHours FROM YourTable WHERE [star datetime] >= @startDate ORDER BY [star datetime] UNION all SELECT y.id, c.cumulativeHours+y.[DURATION (HOURS)] FROM YourTable y INNER JOIN cte c ON c.id+1 = y.ID WHERE c.cumulativeHours+y.[DURATION (HOURS)] <= @x)SELECT id FROM cte; The performance may not be great - would be a problem if you have lot of data in the table and each calculation requires the summation of a large number of rows. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-28 : 00:47:45
|
| [code]DECLARE @startdate datetime,@target intSELECT @startdate = '2011-08-07', --put your date value here@target=4SELECT t.IDFROM table tCROSS APPLY (SELECT SUM(Duration) AS RunTime FROM table WHERE DATETIME <= t.DATETIME )t1WHERE t.DATETIME >= @startdateAND RunTime <=@target[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
marianux
Starting Member
3 Posts |
Posted - 2011-12-28 : 05:38:38
|
quote: Originally posted by sunitabeck Probably easiest approach (from the perspective of programming efforts) would be to use a recursive CTE, like this:DECLARE @x INT; SET @x = 4;DECLARE @startDate DATETIME; SET @startDate = '20110827';;WITH cte AS( SELECT TOP 1 id, [DURATION (HOURS)] AS cumulativeHours FROM YourTable WHERE [star datetime] >= @startDate ORDER BY [star datetime] UNION all SELECT y.id, c.cumulativeHours+y.[DURATION (HOURS)] FROM YourTable y INNER JOIN cte c ON c.id+1 = y.ID WHERE c.cumulativeHours+y.[DURATION (HOURS)] <= @x)SELECT id FROM cte; The performance may not be great - would be a problem if you have lot of data in the table and each calculation requires the summation of a large number of rows.
Hi, thanks for your reply.It seems work fine, althought i never have worked with recursive CTE so i got some doubts about what it's possible to do or to use.Id like to get, if it's possible, next step:I add another column to table:clientid intso table data is now in this wayID CLIENTEID STAR DATETIME DURATION (HOURS)627 1 2011-08-03 17:00:00.000 2628 2 2011-08-05 17:00:00.000 2629 1 2011-08-08 17:00:00.000 2630 3 2011-08-10 17:00:00.000 2631 2 2011-08-12 17:00:00.000 2So now, id like to get results grouped by clientid so f.i. if:1º) We are at 2011-08-01, and id like to get all the rows from table grouped by clientid 'till sum(duration) <= 4.Result must get rows:For Client 1: ID 627 and 629For Client 2: ID 628 and 631For Client 3: ID 6302º) If we were at 2011-08-04 and id like to get all the rows from table 'till sum(duration) <= 4.For Client 1: ID 629For Client 2: ID 628 and 631For Client 3: ID 6303º) If we were at 2011-08-11 and id like to get all the rows from table 'till sum(duration) <= 4. then rows would be For Client 2: ID 628 and 6314º) If we were at 2011-08-27 and id like to get all the rows from table 'till sum(duration) <= 4. then no rows would be retrieved Performance is not a problem because it's not gonna work with a lot of rows, even in the beginning it only woul work with the same client id, but i'd like to know how much can i get with CTE.Anyway im gonna investigate by myself too.Thanks in advance for your help. |
 |
|
|
marianux
Starting Member
3 Posts |
Posted - 2011-12-28 : 06:35:16
|
quote: Originally posted by visakh16
DECLARE @startdate datetime,@target intSELECT @startdate = '2011-08-07', --put your date value here@target=4SELECT t.IDFROM table tCROSS APPLY (SELECT SUM(Duration) AS RunTime FROM table WHERE DATETIME <= t.DATETIME )t1WHERE t.DATETIME >= @startdateAND RunTime <=@target ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks for your replay.I have been trying with your statement and in the end it worked fine when i added to cross apply where statement:DATETIME >= @startdate, so Statement is now:
DECLARE @startdate datetime,@target intSELECT @startdate = '2011-08-07', --put your date value here@target=4SELECT t.IDFROM table tCROSS APPLY (SELECT SUM(Duration) AS RunTime FROM table WHERE DATETIME <= t.DATETIME and DATETIME >= @startdate )t1WHERE t.DATETIME >= @startdateAND RunTime <=@target It works fine now, at least with all the test i've made.I have applied the change that i have explained in my last post and it works fine with new changes.Thanks so much for your help!. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-28 : 07:20:18
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|