Adding data

  • Hi,

    I have the following data

    UId COID CLId SID

    1 1 0 9

    1 2 1 0

    1 3 2 0

    1 4 3 9

    2 5 0 2

    2 5 1 3

    2 5 2 9

    2 5 3 3

    2 5 4 0

    2 6 0 0

    2 6 1 0

    2 6 2 0

    2 6 3 0

    2 6 4 0

    2 7 0 0

    2 7 1 0

    2 7 2 0

    2 7 3 1

    2 7 4 0

    I need the following result from the above data

    UnId CoID ClId SID

    1 1 0 9

    1 2 1 9

    1 3 2 0

    1 4 3 9

    2 5 0 2

    2 5 1 5

    2 5 2 9

    2 5 3 3

    2 5 4 0

    2 6 0 0

    2 6 1 0

    2 6 2 0

    2 6 3 0

    2 6 4 0

    2 7 0 0

    2 7 1 0

    2 7 2 0

    2 7 3 1

    2 7 4 0

    The query needs to add Sid value when the CLid =1 it should add the SID value when both CLid=0 and CLid=1

    Thanks.

  • sql_novice_2007 (7/7/2014)


    Hi,

    I have the following data

    UId COID CLId SID

    1 1 0 9

    1 2 1 0

    1 3 2 0

    1 4 3 9

    2 5 0 2

    2 5 1 3

    2 5 2 9

    2 5 3 3

    2 5 4 0

    2 6 0 0

    2 6 1 0

    2 6 2 0

    2 6 3 0

    2 6 4 0

    2 7 0 0

    2 7 1 0

    2 7 2 0

    2 7 3 1

    2 7 4 0

    I need the following result from the above data

    UnId CoID ClId SID

    1 1 0 9

    1 2 1 9

    1 3 2 0

    1 4 3 9

    2 5 0 2

    2 5 1 5

    2 5 2 9

    2 5 3 3

    2 5 4 0

    2 6 0 0

    2 6 1 0

    2 6 2 0

    2 6 3 0

    2 6 4 0

    2 7 0 0

    2 7 1 0

    2 7 2 0

    2 7 3 1

    2 7 4 0

    The query needs to add Sid value when the CLid =1 it should add the SID value when both CLid=0 and CLid=1

    Thanks.

    I am guessing that what you want is the value from the "previous" row? There are a couple of issue with this. First of all you have to have something in your table to use as an order. From you sample data I think you could use Clid. However the column names are so shortened they don't have any meaning so it is hard for us to know.

    Secondly, if you could post this as ddl and insert statements we can work on your problem instead of setting it up. Take a look at the first link in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

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

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