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

Issue

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.

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

Answered By – symcbean

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published