Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to write Update query for this scenario Expand / Collapse
Author
Message
Posted Thursday, November 8, 2012 2:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, November 24, 2013 9:35 AM
Points: 5, Visits: 19
RowId EName Project Time part Hours
Worked [14 Jan] [15 Jan] [16 Jan]
1 Emp 1 Project 1 14/1/2010 7.0
2 Emp 2 Project 2 15/1/2010 8.0
3 Emp 1 Project 1 16/1/2010 8.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:
RowId EName Project Time part Hours
Worked [14 Jan] [15 Jan] [16 Jan]
1 Emp 1 Project 1 14/1/2010 7.0 7.0
2 Emp 2 Project 2 15/1/2010 8.0 8.0
3 Emp 1 Project 1 16/1/2010 8.0 8.0


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

Post #1382344
Posted Thursday, November 8, 2012 7:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:59 PM
Points: 13,007, Visits: 12,426
kak.mca (11/8/2012)
RowId EName Project Time part Hours
Worked [14 Jan] [15 Jan] [16 Jan]
1 Emp 1 Project 1 14/1/2010 7.0
2 Emp 2 Project 2 15/1/2010 8.0
3 Emp 1 Project 1 16/1/2010 8.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:
RowId EName Project Time part Hours
Worked [14 Jan] [15 Jan] [16 Jan]
1 Emp 1 Project 1 14/1/2010 7.0 7.0
2 Emp 2 Project 2 15/1/2010 8.0 8.0
3 Emp 1 Project 1 16/1/2010 8.0 8.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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1382505
Posted Thursday, November 8, 2012 8:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 9:29 AM
Points: 3,642, Visits: 8,157
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1382512
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse