K
K
kolomat2021-01-14 23:31:59
SQL
kolomat, 2021-01-14 23:31:59

How to do SUM and COUNT on LEFT JOIN query?

Good evening, please tell me about this issue. There are 2 tables in the database, in the first one there is roughly an identifier, in the second there is also a column with this id. The bottom line is, is it possible to make a selection of identifiers from the first table and then get from the second table by these id get SUM and COUNT from the second grouped by id?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail E, 2021-01-15
@Mikhail_E

SELECT 
    t1.id, COUNT(t2.field2) AS cnt, SUM(t2.field2) AS sum_field2
FROM tbl1 as t1
left join tbl2 as t2 
ON t1.id = t2.id
group by  t1.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question