Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update Column Value in one table based on count entries in another table

I have two tables named board_votes and req_form. When a form_id in the board_votes table is equal to 2 (i.e two board member votes means approved) I want to change the value of the column Board_approval in the req_form table to ‘yes’.

Each Board_approval in the req_form table is set at ‘pending’ and only on atleast two form_id entries into the board_votes can the Board_approval value be changed to ‘yes’.

board_votes table:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

+---------+------------+----------+
| vote_id |  form_id   | board_id |
+---------+------------+----------+
|       1 |         22 |        1 |
|       3 |         22 |        3 |
|       4 |         26 |        1 |
|       5 |          7 |        1 |
|       6 |         19 |        1 |
+---------+------------+----------+

req_form:

+-------------------------+
| form_id  Board_approval |
+-------------------------+
| 7          pending      |
| 19         pending      |
| 22         pending      |
| 23         pending      |
+-------------------------+

Here’s the sql query I’m using also:

"Update req_form 
 SET Board_approval = 'yes' 
 HAVING COUNT(board_votes.form_id = '22') >1";

>Solution :

UPDATE rf
SET rf.BoardApproval = 'Yes'
FROM req_form rf
JOIN 
(
SELECT form_id FROM board_votes bv 
GROUP BY bv.form_id 
HAVING COUNT(bv.vote_id) >= 2
) TwoVotes ON TwoVotes.form_id = rf.form_id
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading