T-SQL SELECT with GROUP BY identifier

I have a table with People '

Columns

  • ID
  • Name
  • Age

And the table with Notes ':

Columns

  • ID
  • Text
  • FK_Author

I want to select the number of notes for all authors from the people table, their name and age, but I want to group it by PERSON identifier, not by name. There are many situations where people have the same name, but the identifier is obviously always different.

EXAMPLE (input)

PEOPLE:

╔════╦═══════╦═════╗
β•‘ ID β•‘ NAME  β•‘ AGE β•‘
╠════╬═══════╬═════╣
β•‘  1 β•‘ John  β•‘  12 β•‘
β•‘  2 β•‘ Annie β•‘  29 β•‘
β•‘  3 β•‘ John  β•‘  44 β•‘
β•šβ•β•β•β•β•©β•β•β•β•β•β•β•β•©β•β•β•β•β•β•

NOTES:

╔════╦═══════╦═══════════╗
β•‘ ID β•‘ TEXT  β•‘ FK_AUTHOR β•‘
╠════╬═══════╬═══════════╣
β•‘  1 β•‘ 'aaa' β•‘         1 β•‘
β•‘  2 β•‘ 'aaa' β•‘         1 β•‘
β•‘  3 β•‘ 'aaa' β•‘         2 β•‘
β•‘  4 β•‘ 'aaa' β•‘         2 β•‘
β•‘  5 β•‘ 'aaa' β•‘         3 β•‘
β•šβ•β•β•β•β•©β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•

Expected Result:

╔═══════╦═════╦════════════╗
β•‘ NAME  β•‘ AGE β•‘ TOTALCOUNT β•‘
╠═══════╬═════╬════════════╣
β•‘ John  β•‘  12 β•‘          2 β•‘
β•‘ Annie β•‘  29 β•‘          2 β•‘
β•‘ John  β•‘  44 β•‘          1 β•‘
β•šβ•β•β•β•β•β•β•β•©β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•

When I select data, I also need to group by name if I want to select this column, because if I do not want, I get an error.

+5
source share
4 answers

People, Notes LEFT JOIN, Notes v totalCount .

SELECT  a.ID, a.Name, a.Age,
        COUNT(b.FK_Author) totalCount
FROM    People a
        LEFT JOIN Notes b
            ON a.ID = b.FK_Author
GROUP   BY a.ID, a.Name, a.Age

, :

╔════╦═══════╦═════╦════════════╗
β•‘ ID β•‘ NAME  β•‘ AGE β•‘ TOTALCOUNT β•‘
╠════╬═══════╬═════╬════════════╣
β•‘  1 β•‘ John  β•‘  12 β•‘          2 β•‘
β•‘  2 β•‘ Annie β•‘  29 β•‘          2 β•‘
β•‘  3 β•‘ John  β•‘  44 β•‘          1 β•‘
β•šβ•β•β•β•β•©β•β•β•β•β•β•β•β•©β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•
+5
SELECT P.ID,P.Name,P.Age,COUNT(N.ID)
FROM People P 
INNER JOIN Notes N ON N.FK_Author = P.ID
GROUP BY P.ID,P.Name,P.age
+3

you can also use a subquery to complete this task as -

select people.Name,people.Age,(select count(notes.id) 
                                 from notes 
                                where notes.FK_Author= people.id)
from people;
+2
source

You can also first get an invoice from the Notes table, and then join the People table, as shown below.

Fiddle-demo (thanks to JW for violin)

select name, age, Notate_Count
from People p left join (
        select fk_author, count(*) Notate_Count
        from Notes 
        group by fk_author ) x
     on p.Id = x.fk_author
order by name --Ordering by name here, change as required

|  NAME | AGE | NOTATE_COUNT |
------------------------------
| Annie |  29 |            2 |
|  John |  44 |            1 |
|  John |  12 |            2 |
+2
source

All Articles