This is a discussion on Sum Accumulating Data within the SQL Server forums, part of the Microsoft SQL Server category; --> So, does my code meet the requirement? -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| So, does my code meet the requirement? -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Toronto, ON Canada .. <sucaba.r@gmail.com> wrote in message news:1145448102.311646.16480@g10g2000cwb.googlegro ups.com... Hi Tom, >From looking at the table, the system ran for three days before it was reset (rows 1-3). It then ran for 4 days (rows 4-7) for a total of 7 (not 5 as I indicated above). So I'd need to capture 7 as the total. Thanks |
| |||
| > Here is a query based on Tom's table, but it returns 6 and not 7, because the first row is not counted If there are gaps in PK, it gets slightly more exciting: select count(*) from #t t1 join #t t2 on t1.PK < t2.PK and t1. Amount < t2.amount where not exists( select 1 from #t t3 where (t1.PK < t3.PK) and (t3.PK < t2.PK) ) |
| |||
| Now, I am even more confused. What's wrong with entry #8? What disqualifies it? You say that it ran from 4 - 7, though 8 is also part of the same progression. -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Toronto, ON Canada .. <sucaba.r@gmail.com> wrote in message news:1145541923.733297.75130@t31g2000cwb.googlegro ups.com... Thanks for the help (and lecture). Tom, forgive my ignorance, but your code is producing 9 instead of 7. |
| |||
| I too am confused. :-) I'll try and explain what I mean. Here's the table data again, notated on the right with asterisks that count as legitimate data points: 1 - 239.21 * 2 - 239.55 * 3 - 240.30 * 4 - 0.35 5 - 0.44 * 6 - 0.53 * 7 - 1.20 * 8 - 2.40 * 9 - 0.25 So the uptime query should return 7, as the machine was reset at location 4 and 9. Sorry for the confusion. |
| |||
| On 21 Apr 2006 06:12:15 -0700, sucaba.r@gmail.com wrote: >I too am confused. :-) I'll try and explain what I mean. Here's the >table data again, notated on the right with asterisks that count as >legitimate data points: > >1 - 239.21 * >2 - 239.55 * >3 - 240.30 * >4 - 0.35 >5 - 0.44 * >6 - 0.53 * >7 - 1.20 * >8 - 2.40 * >9 - 0.25 > >So the uptime query should return 7, as the machine was reset at >location 4 and 9. > >Sorry for the confusion. Hi sucaba, Slight modification of Erland's suggestion (it was thus far unclear if the "first" data point had to be included, since there's no "previous" value to compare to - Erland apparently chose to exclude it): SELECT COUNT(*) FROM @t AS a LEFT OUTER JOIN @t AS b ON a.PK = b.PK + 1 WHERE a.Amount > b.Amount OR b.Amount IS NULL -- Hugo Kornelis, SQL Server MVP |
| |||
| And what is wrong with entry 9? -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Toronto, ON Canada .. <sucaba.r@gmail.com> wrote in message news:1145625135.604955.307690@i40g2000cwc.googlegr oups.com... I too am confused. :-) I'll try and explain what I mean. Here's the table data again, notated on the right with asterisks that count as legitimate data points: 1 - 239.21 * 2 - 239.55 * 3 - 240.30 * 4 - 0.35 5 - 0.44 * 6 - 0.53 * 7 - 1.20 * 8 - 2.40 * 9 - 0.25 So the uptime query should return 7, as the machine was reset at location 4 and 9. Sorry for the confusion. |
| |||
| Tom Moreau (tom@dont.spam.me.cips.ca) writes: > And what is wrong with entry 9? 0.25 < 2.40 or so I guess. But it's difficult when sucaba.r does not really seem to have a grip on it him/herself. Maybe because it's some class assignment, and not a real-world case, who knows. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |
| |||
| The specs are confusing - and fluid. My guess is if he wants 7 so bad, then this is the solution: SELECT 7 -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Toronto, ON Canada .. "Erland Sommarskog" <esquel@sommarskog.se> wrote in message news:Xns97AD85A38BDF9Yazorman@127.0.0.1... Tom Moreau (tom@dont.spam.me.cips.ca) writes: > And what is wrong with entry 9? 0.25 < 2.40 or so I guess. But it's difficult when sucaba.r does not really seem to have a grip on it him/herself. Maybe because it's some class assignment, and not a real-world case, who knows. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |