How to write Update query for this scenario

  • RowIdENameProjectTime partHours

    Worked[14 Jan][15 Jan][16 Jan]

    1Emp 1Project 114/1/20107.0

    2Emp 2Project 215/1/20108.0

    3Emp 1Project 116/1/20108.0

    From the above table:

    The above table is showing like

    Emp 1 has worked for 7.0 hours on Project 1 on 14/1/2010.

    Emp 2 has worked for 8.0 hours on Project 2 on 15/1/2010.

    Emp 1 has worked for 8.0 hours on Project 1 on 16/1/2010.

    I want to update the above table like below:

    I want to update the column [14 Jan] column first row with 7.0 for Emp 1

    I want to update the column [15 Jan] column second row with 8.0 for Emp 2

    I want to update the column [16 Jan] column third row with 8.0 for Emp 1

    Hence the result should be:

    RowIdENameProjectTime partHours

    Worked[14 Jan][15 Jan][16 Jan]

    1Emp 1Project 114/1/20107.07.0

    2Emp 2Project 215/1/20108.08.0

    3Emp 1Project 116/1/20108.08.0

    Can anybody please suggest me how to write the query for the same in SQL Server 2005?

  • kak.mca (11/8/2012)


    RowIdENameProjectTime partHours

    Worked[14 Jan][15 Jan][16 Jan]

    1Emp 1Project 114/1/20107.0

    2Emp 2Project 215/1/20108.0

    3Emp 1Project 116/1/20108.0

    From the above table:

    The above table is showing like

    Emp 1 has worked for 7.0 hours on Project 1 on 14/1/2010.

    Emp 2 has worked for 8.0 hours on Project 2 on 15/1/2010.

    Emp 1 has worked for 8.0 hours on Project 1 on 16/1/2010.

    I want to update the above table like below:

    I want to update the column [14 Jan] column first row with 7.0 for Emp 1

    I want to update the column [15 Jan] column second row with 8.0 for Emp 2

    I want to update the column [16 Jan] column third row with 8.0 for Emp 1

    Hence the result should be:

    RowIdENameProjectTime partHours

    Worked[14 Jan][15 Jan][16 Jan]

    1Emp 1Project 114/1/20107.07.0

    2Emp 2Project 215/1/20108.08.0

    3Emp 1Project 116/1/20108.08.0

    Can anybody please suggest me how to write the query for the same in SQL Server 2005?

    From what you posted it is nearly impossible to determine what you are trying to do or how to write the code. We would need ddl (create table statements), sample data (insert statements) and desired output based on the sample data. 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/

  • I can help you to write the update statement. But I would like to know why are you doing this? This will help you with 3 days but what happens with a whole month? or a whole year? Why do you have this table design?

    You shouldn't store values that way, to show the results in a report would be ok (even if some people say it should be done in the front end).

    Please, review the requirements and then confirm you need to do this.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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