• subscribe
April 22, 2009 12:00 AM

Subqueries and Joins for Running Aggregates

Use these solutions for small partitions
SQL Server Pro
InstantDoc ID #101623
Downloads
101623.zip



Listing 7: Set-Based Solution Using Joins, Multiple Aggregates
SELECT S1.empid, S1.qty, S1.val, 
   SUM(S2.qty) AS sumqty,
   AVG(S2.qty) AS avgqty,
   SUM(S2.val) AS sumqty,
   AVG(S2.val) AS avgval
FROM dbo.Sales AS S1
   JOIN dbo.Sales AS S2
       ON S2.empid = S1.empid
       AND S2.dt GROUP BY S1.empid, S1.dt, S1.qty, S1.val;



ARTICLE TOOLS

Comments
  • Marcos
    3 years ago
    May 20, 2009

    Very good explanation about the details os SQL Server.
    I would love if the articles comes with a rate that indicate it's level, something simple like (begginer, intermediate, advanced) would be wonderful.

    Take Care!

You must log on before posting a comment.

Are you a new visitor? Register Here