t-sql 2008 nested case statements

  • I have a question about understanding nested case statements in sql server 2008:

    The sql looks like the folloiwng:

    select numberofcases

    from inventory

    where inventory_cnt > 1000

    (when select top 1

    from inventory

    where inventory_cnt > 750

    then numberofcases = 750 * 30

    when select top 2

    from inventory

    where inventory_cnt > 975

    then numberofcases = 975 * 35

    when select top 3

    from inventory

    where inventory_cnt > 1025

    then numberofcases = 1025 / 10

    when select top 4

    from inventory

    where inventory_cnt > 1050

    then numberofcases = 1050 / 5) c * 2

    as casesused, select CustomerNumber from inventory

    I would like you to explain the following:

    1. There are 4 when statements. Will the logic hit each when statemnet or will the logic

    stop once the first when statement is true?

    2. Would you explain what the c* 2 means from the school listed above?

  • wendy elizabeth (2/23/2014)


    I have a question about understanding nested case statements in sql server 2008:

    The sql looks like the folloiwng:

    select numberofcases

    from inventory

    where inventory_cnt > 1000

    (when select top 1

    from inventory

    where inventory_cnt > 750

    then numberofcases = 750 * 30

    when select top 2

    from inventory

    where inventory_cnt > 975

    then numberofcases = 975 * 35

    when select top 3

    from inventory

    where inventory_cnt > 1025

    then numberofcases = 1025 / 10

    when select top 4

    from inventory

    where inventory_cnt > 1050

    then numberofcases = 1050 / 5) c * 2

    as casesused, select CustomerNumber from inventory

    I would like you to explain the following:

    1. There are 4 when statements. Will the logic hit each when statemnet or will the logic

    stop once the first when statement is true?

    2. Would you explain what the c* 2 means from the school listed above?

    Homework is easy with Google. Type in TSQL CASE and choose the first result returned. It will be this: http://technet.microsoft.com/en-us/library/ms181765.aspx which answers your first question. Your second question? Multiply c (whatever that is) by two.

    “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 2 posts - 1 through 2 (of 2 total)

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