I've got 2 tables in SQL Server database:
What I need is query that returns table with grouped records by [opt_1] and [opt_2] like this:
[opt_1],
[opt_2],
{count of such records from table_1},
{count of such records from table_2, by [id]},
{average [result] from table_2, by [id]}
What is the best way to get it?
You could use join
and group by
;
select t1.opt_1,t2.opt_2,count(t1.id),AVG(t2.result) from table1 t1
inner join table2 t2 ON t1.id = t2.id
group by t1.opt_1,t2.opt_2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.