I have this statement:
UPDATE `tblAllocatedLeads`
SET tblAllocatedLeads.fldAllocatedStatus = CASE
WHEN tblAllocatedLeads.fldAllocatedStatus = 'Allocated'
AND
(SELECT COUNT(*) FROM (SELECT * FROM `tblAllocatedLeads`) AS tblallocate WHERE AND tblallocate.fldAllocatedLeadId = tblAllocatedLeads.fldAllocatedLeadId) > 1
THEN 'Outstanding'
END
What I wanted is that update all rows where fldAllocatedStatus = ‘Allocated’ to ‘Outstanding’ that has one or more same value of column fldAllocatedLeadId to other rows.
my data
ID fldAllocatedStatus fldAllocatedLeadId
1 Accepted 123
2 Rejected 123
3 Allocated 123
4 Allocated 321
In this case ID = 3 should update fldAllocatedStatus to ‘Outstanding’ as it has one or more same fldAllocatedLeadId from ID = 1,2
and ID = 4 should stay ‘Allocated’ as it doesn’t have match of fldAllocatedLeadId with other rows.
right now what it does it updates even the “Accepted,Rejected” which is supposedly to update only rows with “Allocated”
2
Answers
Assuming MySQL, I think you want:
The subquery gets leads that have some value that is not allocated. The outer
WHERE
only updates the allocated row (if there is one).You don’t need any aggregation, just a self join:
See the demo.
Results: