loop thru the list of months using t-sql statement

  • can this be done in a t-sql statement:

    table 1:

    operator

    name1

    name2

    name3

    table 2:

    previous_month

    name2

    table 3:

    current_month

    name1

    name2

    i need to return the list of operators from table 1.  i need to join it with the other two tables. 

    table2 will tell me the value for the previous month.  if its null in table2 then the value of item will be 0. 

    if it has a matching record in table 2 then its 1. 

    if it has a matching record in table 3 then the value of the month is the current month and item is 1.  otherwise

    the value of the month is the current month and item is 0.

    my problem is how can i iterate through values January to (CurrentMonth-1) so that it will return the following:

    operator       month        item

    name1          January       0

    name1          February      0

    name1          March          0

    name1          April            0

    name1          May            0

    name1          June           0

    name1          July            0

    name1          August         0

    name1          September    0

    name1          October        1

    name2          January        1

    name2          February       1

    name2          March           1

    name2          April             1

    name2          May             1

    name2          June            1

    name2          July             1

    name2          August         1

    name2          September    1

    name2          October        1

    name3          January        0

    name3          February       0

    name3          March          0

    name3          April             0

    name3          May             0

    name3          June            0

    name3          July             0

    name3          August         0

    name3          September    0

    name3          October        0

    thanks

    ann

  • It sounds like a CASE statment would work.  But it would help if you could give us some sample data, your current output, and what you hope to achieve...   

     

     

    I wasn't born stupid - I had to study.

Viewing 2 posts - 1 through 1 (of 1 total)

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