SQl case statement

  • I have a table something like below:

    DROP table #step2
    Create Table #steps2(
    Loannumber Varchar(10)
    ,Step1 varchar(10)
    ,Step2 varchar(10)
    ,Step3 varchar(10)
    ,Step4 varchar(10)
    ,Step5 varchar(10)
    ,Step6 varchar(10)
    ,Step7 varchar(10)
    ,Step8 varchar(10)
    ,Step9 varchar(10)
    ,Step10 varchar(10)
    ,Numberofsteps int
    ,maxstep varchar(10)
    )

    Insert into #steps2 (loannumber , step1) Values ('12345','1/2/2016')
    Insert into #steps2 (loannumber , step1,step2) Values ('12345','1/3/2016','2/12/2017')

    Insert into #steps2 (loannumber , step1,step2,step3,step4,step5) Values ('12345','1/2/2018','1/3/2018','1/4/2018','1/5/2018','1/6/2018')

    Insert into #steps2 (loannumber , step1,step2) Values ('12345','1/2/2/2019','1/2/2/2020')

    Insert into #steps2 (loannumber ) Values ('12345')

    LoannumberStep1 Step2Step3Step4Step5Step6Step7Step8Step9Step10Numberof stepsmax step
    123451/2/20161Step1
    123451/3/20162/12/20172Step2
    123451/2/20181/3/20181/4/20181/5/20181/6/20185Step5
    123451/2/2/20191/2/2/20202step2 
    1234500

    I need to get the Number of steps for each loannumber ( though for example i took same loannumber ) and also maxstep which tells the loan in which step?

  • My first suggestion, normalize the table.  Right now, if you add another step you have to add another column.  If you normalize the data each step becomes a row in a child table and if you need to add another step or two, it is just adding an additional row of data.

  • Lynn Pettis - Friday, June 29, 2018 12:35 PM

    My first suggestion, normalize the table.  Right now, if you add another step you have to add another column.  If you normalize the data each step becomes a row in a child table and if you need to add another step or two, it is just adding an additional row of data.

    +1.

    If you can't do that, create a view which presents the data in normalised form and query that.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Lynn Pettis - Friday, June 29, 2018 12:35 PM

    My first suggestion, normalize the table.  Right now, if you add another step you have to add another column.  If you normalize the data each step becomes a row in a child table and if you need to add another step or two, it is just adding an additional row of data.

    This is just an example , i created for explanation. Actual table is different.

  • komal145 - Friday, June 29, 2018 1:24 PM

    Lynn Pettis - Friday, June 29, 2018 12:35 PM

    My first suggestion, normalize the table.  Right now, if you add another step you have to add another column.  If you normalize the data each step becomes a row in a child table and if you need to add another step or two, it is just adding an additional row of data.

    This is just an example , i created for explanation. Actual table is different.

    So what did you expect?
    We can offer advice based only on what you tell us.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Phil Parkin - Friday, June 29, 2018 1:30 PM

    komal145 - Friday, June 29, 2018 1:24 PM

    Lynn Pettis - Friday, June 29, 2018 12:35 PM

    My first suggestion, normalize the table.  Right now, if you add another step you have to add another column.  If you normalize the data each step becomes a row in a child table and if you need to add another step or two, it is just adding an additional row of data.

    This is just an example , i created for explanation. Actual table is different.

    So what did you expect?
    We can offer advice based only on what you tell us.

    I am trying to calculate two fields 1) NumofSteps for each loan 2) Maxstep  for each loan. Can you please see above example and suggest how do i do calculation in sql query?

  • komal145 - Friday, June 29, 2018 1:46 PM

    Phil Parkin - Friday, June 29, 2018 1:30 PM

    komal145 - Friday, June 29, 2018 1:24 PM

    Lynn Pettis - Friday, June 29, 2018 12:35 PM

    My first suggestion, normalize the table.  Right now, if you add another step you have to add another column.  If you normalize the data each step becomes a row in a child table and if you need to add another step or two, it is just adding an additional row of data.

    This is just an example , i created for explanation. Actual table is different.

    So what did you expect?
    We can offer advice based only on what you tell us.

    I am trying to calculate two fields 1) NumofSteps for each loan 2) Maxstep  for each loan. Can you please see above example and suggest how do i do calculation in sql query?

    And why would we bother doing that, if 'actual table is different'? Our solution would not work on 'actual table'.

    Show us the real table, or something close, and we can tell you how to normalise it and then how to query it.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • You could try doing a CROSS APPLY such as in this article to operate on the data in a set based fassion:
    http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/
    Then you can do aggregates on it:
    SELECT Loannumber, x.NumOfSteps, x.MaxStep
    FROM #steps2
    CROSS APPLY (SELECT COUNT(*) NumOfSteps, MAX(num) AS MaxStep FROM (VALUES (1,Step1), (2,Step2), (3,Step3), (4,Step4), (5,Step5), (6,Step6), (7,Step7), (8,Step8), (9,Step9), (10,Step10)) AS VALUE(num, val) WHERE val IS NOT NULL) x

    I suppose if you have multiple rows with the same LoanNumber as in your sample data, you'd have to do a GROUP BY LoanNumber in your outer query

  • Chris Harshman - Friday, June 29, 2018 2:08 PM

    You could try doing a CROSS APPLY such as in this article to operate on the data in a set based fassion:
    http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/
    Then you can do aggregates on it:
    SELECT Loannumber, x.NumOfSteps, x.MaxStep
    FROM #steps2
    CROSS APPLY (SELECT COUNT(*) NumOfSteps, MAX(num) AS MaxStep FROM (VALUES (1,Step1), (2,Step2), (3,Step3), (4,Step4), (5,Step5), (6,Step6), (7,Step7), (8,Step8), (9,Step9), (10,Step10)) AS VALUE(num, val) WHERE val IS NOT NULL) x

    I suppose if you have multiple rows with the same LoanNumber as in your sample data, you'd have to do a GROUP BY LoanNumber in your outer query

    Works fine with the sample data returning what is needed.  Here is a slight variation on the code Chris posted:

    SELECT
      [Loannumber]
      , Step1
      , Step2
      , Step3
      , Step4
      , Step5
      , Step6
      , Step7
      , Step8
      , Step9
      , Step10
      , [x].[NumOfSteps]
      , [MaxStep] = ISNULL([x].[MaxStep],0)
    FROM
      [dbo].[steps2]
      CROSS APPLY
      (
        SELECT
          COUNT(*)     [NumOfSteps]
          , MAX([num]) AS [MaxStep]
        FROM
          (
            VALUES
              (1, [Step1])
              , (2, [Step2])
              , (3, [Step3])
              , (4, [Step4])
              , (5, [Step5])
              , (6, [Step6])
              , (7, [Step7])
              , (8, [Step8])
              , (9, [Step9])
              , (10, [Step10])
          ) AS [VALUE] ([num], [val])
        WHERE
          [val] IS NOT NULL
      ) [x];
    go

  • Chris Harshman - Friday, June 29, 2018 2:08 PM

    You could try doing a CROSS APPLY such as in this article to operate on the data in a set based fassion:
    http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/
    Then you can do aggregates on it:
    SELECT Loannumber, x.NumOfSteps, x.MaxStep
    FROM #steps2
    CROSS APPLY (SELECT COUNT(*) NumOfSteps, MAX(num) AS MaxStep FROM (VALUES (1,Step1), (2,Step2), (3,Step3), (4,Step4), (5,Step5), (6,Step6), (7,Step7), (8,Step8), (9,Step9), (10,Step10)) AS VALUE(num, val) WHERE val IS NOT NULL) x

    I suppose if you have multiple rows with the same LoanNumber as in your sample data, you'd have to do a GROUP BY LoanNumber in your outer query

    Thank you , it works.

Viewing 10 posts - 1 through 9 (of 9 total)

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