A Developer’s Guide to Dangerous Queries (Video)

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.

Previous Post
What’s a DBA’s Defining Moment?
Next Post
AlwaysOn Availability Groups in AWS Revisited

4 Comments. Leave new

  • Thanks for sharing Jeremiah. Great stuff!

    By the way, I think I’ve seen similar queries before from a product that will remain unnamed 😉

  • Donald Robichaud
    February 19, 2013 3:11 pm

    /*******************************
    * 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) ;

    • Jeremiah Peschka
      February 19, 2013 3:16 pm

      Thanks for the suggestion to remove the GROUP BY.

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

Menu
{"cart_token":"","hash":"","cart_data":""}