So I have a table that looks like this:
ProjectID TaskID Date 000 001 10/28/09 000 002 12/1/09 000 003 02/24/10 001 001 04/14/10 001 002 07/5/10 001 003 02/24/11 002 001 06/07/10 002 002 07/7/10 002 003 09/13/12
Assume there are many projects and many tasks, but each project contains the same tasks. I want to copy dates in the following manner:
One project (000) has the master dates. I want to copy those dates for each task to several other projects (001, 002 in the example data).
So, if task 001 in project 000 has a date of 10/28/09, I want task 001 in other projects (I know their IDs) to have the same date.
If task 002 in project 001 has date 12/1/09, I want task 002 to have this date in each of the other projects as well.
Is there one query that can do this? I think there should be, but I can't figure it out right now.
BTW I am using SqlServer 2005.
This should work just fine:
Perhaps something like this:
I'm not sure which database engine you are using, and some may allow for a more natural syntax than the above.
The above also obviously assumes that the timeslots haven't already been set for the other projects and that you're trying to define new timeslots instead of updating existing ones.
This is what you want, just replace "..." with the CSV list of projects.
Would that do it for you?
So for TaskID = 001, this statement will
and then update each of the projects (P01-P04) and TaskID=001 to this date. That's what you're looking for, no??
You can also use an OUTPUT clause to PROVE to you what is being updated:
This will cause each row being updated to output the new values (e.g. after the update) so you can check and see what gets updated, and to what values.
Just ran the query and this is the results I get:
The dates for Projects 1 and 2 and Tasks 1,2,3 have been set to the values of the "master" project 0.