sqlserver数据库"子连接"count数量查询

2024-11-25 07:36:43
推荐回答(2个)
回答1:

select a.plate_number 车牌,isnull(b.超速,0)超速,isnull(c.超载,0)超载 
from car_report a left join
(select plate_number,COUNT(*) as 超速 from exceed_report group by plate_number) b 
on a.plate_number=b.plate_number left join 
(select plate_number,COUNT(*) as 超载 from associat_report group by plate_number) c 
on a.plate_number=c.plate_number

回答2:

select a.*,b.超载 from

(select plate_number,COUNT(*) as 超速 from exceed_report group by plate_number) a left join
(select plate_number,isnull(COUNT(*),0) as 超载 from associat_report group by plate_number) b on a.plate_number=b.plate_number