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

Count() total of specified value in a column then display that count on the relevant row

SELECT Table1.Col1, Table1.Col2, Table1.Col3, **Table2.Col1** 
FROM Table1
JOIN Table2 ON Table1.ID = Table2.ID
WHERE condition1
AND condition2;

This returns the list as expected.

Here’s where I’m stuck: Table2.Col1 has 8-15 records for each Table1.ID. I want to display the count for each Table2.ID aligned with the row for Table1.ID.

I can get the total count of a Table2.Col1 condition but I don’t know how to align it to the relevant row- Table1.ID.

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

>Solution :

SELECT Table1.Col1, Table1.Col2, Table1.Col3, COUNT(Table2.Col1) 
FROM Table1
JOIN Table2 ON Table1.ID = Table2.ID
WHERE condition1
AND condition2
GROUP BY Table1.Col1, Table1.Col2, Table1.Col3
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