ivias ivias - 2 months ago 8
SQL Question

Selecting Values from one table based on another table for stored procedute

I have the following stored procedure:

select assn as Association, count(id) as Total

from Farmers
where assn is not null
group by assn
order by assn asc


Which generates this table:enter image description here

Can anyone assist me in populating the Association column above with values in association column from this second table:enter image description here

Answer

I think you are looking for this

SELECT f.assn AS AssociationId,
       a.Association,
       COUNT(f.id) AS Total
FROM Farmers f
     INNER JOIN YourSecondTable a ON f.assn = a.id
WHERE assn IS NOT NULL
GROUP BY assn,
         a.Association
ORDER BY assn ASC;

Replace YourSecondTable with the name of your second table