I’m trying to combine the following 2 queries. table_A stores a list of available appointments and number_open is how many open slots there are for a specific appointment, in this case 2020-06-30 AM which has 5 open. table_B contains any dates waiting to be accepted and has a different record for each date request, so in this case, 2 rows sitting there, 2 dates waiting to be accepted. What I’m trying to do is combine both queries to get a list of how many appointments there are left for each slot, so expected results would be table_A.numberopen (5) minus table_B.Count() (2), result = 3 left to allocate. Everything I try results in duplicate records being returned, I can’t seem to narrow it down to just a single total number for that appointment. table_A will always have a record, but table_B may not have any matching records. Thanks in advance for any help you can provide.
SELECT whendate, slot, initials, numberopen FROM table_A WHERE whendate = '2020-06-30' AND slot = 'AM' AND numberopen > 0
SELECT initials FROM table_B WHERE date_requested = "2020-06-30" AND ampm_requested = 'AM'
Advertisement
Answer
Your question is quite unclear about the join conditions between the two tables, however I suspect that a correlated subquery would do what you want:
select a.whendate, a.slot, a.initials, a.numberopen - ( select count(*) from table_b b where b.date_requested = a.whendate and b.ampm_requested = a.slot ) remaining_open from table_a a where a.whendate = '2020-06-30' and a.slot = 'AM'