Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Group by exact match on string
Message
 
 
To
28/07/2011 11:30:33
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01519453
Message ID:
01519464
Views:
23
>Wow. I'm impressed. Very cool.
>
>I need to study a little to figure out *why* it works and to learn the technique for other applications, but it definitely is the result I want
>
>Thanks so much.
>
In SQL 2005 with the introduction of windowing functions such as row_number(), rank(), dense_rank(), etc. many of the common problems that were a bit hard to solve are now very easy to solve with just applying these functions.

SQL Server Denali made it even much easier and running total problem can now be solved easily. Here is a sample sent to me by Peter Larsson:
DECLARE     @Sample TABLE
            (
                         i INT,
                         j INT
            );
 
INSERT      @Sample
VALUES      (1, 1), (1, 2), (1, 4), (1, 3),
            (1, 6), (1, 5), (1, 8), (1, 7)
 
SELECT      i,
            j,
            SUM(j) OVER (PARTITION BY i ORDER BY j) AS RunningTotal,
            SUM(j) OVER (PARTITION BY i ORDER BY j RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotalFormal,
            SUM(j) OVER (PARTITION BY i ORDER BY j RANGE UNBOUNDED PRECEDING) AS RunningTotalFormal,
            SUM(j) OVER (PARTITION BY i ORDER BY j ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotalFormal,
            SUM(j) OVER (PARTITION BY i ORDER BY j ROWS BETWEEN 2147483647 PRECEDING AND CURRENT ROW) AS RunningTotalOther
FROM        @Sample
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform