Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    1

    Unanswered: Case statement in sqlserver for thhis logic.

    select id,qty into #lastyrend from table where dt='12/31/2007'--last yr end


    select id, qty into #lastmonthend from table where dt='05/31/2008' --last month end

    select id, qty into #lastqtrend from table where dt='03/31/2008'--last qtr end

    select id,qty into #lastweeked from table where dt='05/27/2008'



    select id,#lastyrend .qty,

    case when #lastmonthend.qty is null then #lastyrend .qty as lastmonthend


    case when #lastmonthend.qty is null then #lastyrend .qty
    when #lastqtrend .qty is null then #lastmonthend.qty .qty else #lastqtrend .qty as lastqtr


    case when #lastmonthend.qty is null then #lastyrend .qty
    when #lastqtrend .qty is null then #lastmonthend.qty
    when #lastweeked .qty is null then #lastqtrend .qty else #lastweeked .qty as lastweekend.

    from table
    left join table.id=#lastyrend .id
    left join table.id=#lastmonthend.id
    left join table.id=#lastqtrend.id
    left join table.id=#lastweeked.id



    IN my query I am taking values from a table for different date series.

    logic is if there is no value for a particular date then take the previous date value.



    Problem
    --------------

    in my query #lastqtrend .qty is null but #lastweeked .qty has the value.

    as per my third case statement I expected I will take the #lastweeked .qty

    but since #lastqtrend .qty is null it shows the value of #lastmonthend.qty
    I think here after the

    case when #lastmonthend.qty is null then #lastyrend .qty
    when #lastqtrend .qty is null then #lastmonthend.qty ---- after executing this line below line is not executing when #lastweeked .qty is null then #lastqtrend .qty else


    Can somebody help me in this.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •