SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to write Update query for this scenario


How to write Update query for this scenario

Author
Message
kak.mca
kak.mca
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 21
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?
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26113 Visits: 17538
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 Modens 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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16555 Visits: 19098
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search