need help to simplify this script

  • Hey Dwain,

    Could you please explain how this query executes to get the count.

    Thanks in Advance

  • Well, I can try. I saw this technique in the "Replace CASE expression and UNPIVOT operator" section of this article: http://beyondrelational.com/modules/2/blogs/70/posts/10905/interesting-enhancements-to-the-values-clause-in-sql-server-2008.aspx

    The COUNT(Products) construct is counting non-null values across the columns that are specified in the VALUES set.

    I realize that's a pretty brief explanation but I think it conveys the gist of it.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thank you very much

  • dwain.c (6/5/2012)


    Just to show that I'm keeping current on SQL 2008 stuff, and probably for no other reason, I thought I'd post a less verbose alternative.

    SELECT Employee, NEW_LOB, NEW_DEPARTMENT, NEW_COST_CENTRE, NEW_MANAGER, NEW_JOB_CODE, NEW_CYCLE

    FROM #Findings

    WHERE @NbrProducts >

    (SELECT COUNT(Products)

    FROM (VALUES (NEW_LOB), (NEW_DEPARTMENT), (NEW_COST_CENTRE)

    ,(NEW_MANAGER), (NEW_JOB_CODE), (NEW_CYCLE)) t(Products))

    Absolutely perfect. Nice job, Dwain.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (6/8/2012)


    dwain.c (6/5/2012)


    Just to show that I'm keeping current on SQL 2008 stuff, and probably for no other reason, I thought I'd post a less verbose alternative.

    SELECT Employee, NEW_LOB, NEW_DEPARTMENT, NEW_COST_CENTRE, NEW_MANAGER, NEW_JOB_CODE, NEW_CYCLE

    FROM #Findings

    WHERE @NbrProducts >

    (SELECT COUNT(Products)

    FROM (VALUES (NEW_LOB), (NEW_DEPARTMENT), (NEW_COST_CENTRE)

    ,(NEW_MANAGER), (NEW_JOB_CODE), (NEW_CYCLE)) t(Products))

    Absolutely perfect. Nice job, Dwain.

    Thanks Chris!

    I take it you're trolling for low-hanging fruit?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (6/8/2012)


    ChrisM@Work (6/8/2012)


    dwain.c (6/5/2012)


    Just to show that I'm keeping current on SQL 2008 stuff, and probably for no other reason, I thought I'd post a less verbose alternative.

    SELECT Employee, NEW_LOB, NEW_DEPARTMENT, NEW_COST_CENTRE, NEW_MANAGER, NEW_JOB_CODE, NEW_CYCLE

    FROM #Findings

    WHERE @NbrProducts >

    (SELECT COUNT(Products)

    FROM (VALUES (NEW_LOB), (NEW_DEPARTMENT), (NEW_COST_CENTRE)

    ,(NEW_MANAGER), (NEW_JOB_CODE), (NEW_CYCLE)) t(Products))

    Absolutely perfect. Nice job, Dwain.

    Thanks Chris!

    I take it you're trolling for low-hanging fruit?

    Nah mate - wouldn't want to tread on your toes!

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply