Discussion Locked

Many to one database question

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Can someone pls help me!!
I have 2 tables in a database for Project/Workflow tracking. One table (Actions table) contains a detailed list of actions with a status column.
The other table contains each project (Project table).

I need a query to update the Project table record for a particular project when all actions for that project are Complete (as indicated in the Actions Table).

Thanks for your help!!!

4 replies

by stress junkie

Each record in each database table should have a key field on which to join the tables. In the project table this key field would have unique values. In the action table the key would be allowed to have duplicates. Then you put the unique project key into all of the records of its associated actions. Then when you want to display the database you simply join the tables based on taht key.

by jdmercha

First you need to decide at what point the Project Table will get updated. Do you want it done automatically or do you want to have a button that triggers the update?

by eppk

Add two fields into the Project Table with Int field to keep the goal and actual value.
Add a score Int field within the Action table to indicate the workflow's performance.
Write a trigger to update the Project table's actual field when Action table's score field changed

Then you can write a smiple query to compare the goal VS actual for update or for reporting

Hope it helps!

by tony hopkinson

There are several options.
The one where you won't potentially have to change your table structures and add extra data or use inefficient methods like updating through a subquery or a view, is a cursor.
Unfortunately you didn't mention what dbms you are using and the syntax is very specific. on MSSQL it would be something like

Declare @ProjectID int
Declare CURSOR csrIncompete AS
Select ProjectID
From Projects
Where Status = 'Incomplete'

Open csrIncomplete
Fetch Next csrIncomplete Into @ProjectID
While @@Fetch_Status = 0
Begin
If Not Exists(Select * From Actions Where ProjectID = @ProjectID and Status <> 'Complete')
Update Projects Set Status = 'Complete' Where ProjectID = @ProjectID
Fetch Next csrIncomplete Into @ProjectID
End
Close crsIncomplete
Deallocate csrIncomplete

When to run the above is of course another question entirely.

The absolute simplest solution is not to store Project Status in the Projects table at all and to derive it from the contents of actions.

For instance if the ProjectStatus is that of the least complete action and action status's are integers. Selecting the min of actionstatus for each project ID in a join and then joining that to a status description table would give you the desrired answer, you'd just have to have an extra code (0 ?) for those projects with no actions and outer join Projects and Actions using the IfNull function to pick up 0 = 'No actions yet Assigned' or some such.

Some DBMS's will let you do this but it's horribly inefficient
Update Projects Set ProjectStatus = 'Complete'
Where ProjectStatus <> 'Complete ' and Not Exists(Select * From Actions Where Actions.ProjectID = Projects.ProjectID and Action.Status <> 'Complete')

This discussion is closed to new replies.