Complex sql

Discussion in 'General MSSQL Discussion' started by mssql, Dec 2, 2016.

  1. mssql

    mssql

    Joined:
    Dec 2, 2016
    Messages:
    1
    Likes Received:
    0
    Here is data in my table

    SiteId Wk1A Wk1B Wk2A Wk2B Wk3A Wk3B Wk4A Wk4B Wk5A Wk5B
    101 0 8 7 9 0 6 0 9 0 9
    288 8 8 0 7 0 7 0 8 6 6
    468 1 8 6 9 8 6 0 9 0 9
    768 0 8 0 7 0 7 6 8 0 6

    I want a query that will look at all weeks starting with a so Wk1a, Wk2a, Wk3a, Wk4a, Wk5a and so forth then figure out which is the last column which has value greater than 0 for example SiteId 101 It is Wk2A. SiteId 288 it is Wk5A. SiteId 468 Wk3A.
    Once the last column is figured out find which all weeks b are remaining after that and sum up the value in weeks b. So for example siteid101 last column is Wk2a so i want a sum of values in Wk3b, Wk4b and Wk5b.
     
    mssql, Dec 2, 2016
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.