Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Group by exact match on string
Message
 
 
À
28/07/2011 11:30:33
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Divers
Thread ID:
01519453
Message ID:
01519464
Vues:
24
>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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform