Holiday prices end in 28d 20h 43m — See the sale

A Developer’s Guide to Dangerous Queries (Video)

http://youtu.be/2c33WStd94s

Want to help us shape our training? Take 30 seconds and answer 5 questions.

Are you looking for sample code from the presentation? Download the samples from A Developers Guide to Dangerous Queries.

Free, 3× a week

Get my new posts by email

Three posts a week, plus a Monday roundup of the best database news from around the web.

4 comments

  1. /*******************************
    * Correlated Subqueries – fixed
    * Removed DISTINCT
    * Added alias in GROUP BY
    ******************************/

    USE ContosoRetailDW;
    GO

    SELECT
    dd.DateDescription ,
    SUM(fs.SalesAmount) AS Sales,
    SUM(COALESCE(fs.DiscountAmount, 0)) AS Discount

    FROM dbo.FactSales AS fs
    JOIN dbo.DimDate AS dd
    ON fs.DateKey = dd.Datekey
    GROUP BY dd.DateDescription
    HAVING SUM(fs.SalesAmount) > AVG(fs.SalesAmount) ;

    1. Thanks for the suggestion to remove the GROUP BY.

      I edited your comment to make it clear to the reader which changes you made.

Leave a comment

Email me: