I have 3 tables such as
IndentHeader:
IndentID StatusID
-------- ------
1 5
2 5
IndentDetail:
IndentID ItemID ItemStatusID
-------- ------ ------------
1 22 4
1 23 4
2 11 4
2 12 3
2 13 3
POIndent:
POID IndentID ItemID ItemStatusID
-------- ------ ------ ------------
1 1 22 4
1 1 23 4
1 2 11 4
I want to Update IndentHeader table StatusID = 4 when all the Items in the IndentDetail table (based on the IndentID) ItemstatusID becomes 4 otherwise I want to Update IndentHeader StatusID = 3. In the Condition I need to give POID. Based on the POID, the corresponding Indent is considered for both the IndentHeader and IndentDetail table. My desired Result should be like this:
IndentHeader:
IndentID StatusID
-------- ------
1 4
2 3
How to achieve this? Please help me.
Hi all, this is my update command. But it update both the StatusID in IndentHeader as 4.
UPDATE STR_IndentHeader
SET StatusID = IID
FROM
(SELECT
STR_IndentDetail.IndentID, MIN(ItemStatusID) AS 'IID'
FROM
STR_IndentDetail INNER JOIN PUR_POIndent PP
ON PP.IndentID = STR_IndentDetail.IndentID
AND PP.ItemID = STR_IndentDetail.ItemID
WHERE ItemStatusID = 4 AND PP.POID = 1
GROUP BY STR_IndentDetail.IndentID) ID
WHERE ID.IndentID = STR_IndentHeader.IndentID
I need all your valuable contributions. please help me…
My [revised] solution use one ALL subquery to check ItemStatusID condition: