我有一个查询,它使用连接的三个表中的“Case When Then End”返回结果。它看起来像这样:
select f.filename,
sum(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser,
sum(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,
sum(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather
from VMdata v
inner join files f on v.id = f.id
inner join DMdata d on f.id = d.id
where f.filename in (X,Y,Z)
group by f.filename
这很好用,每个结果行都正确给出 1 或 0。这里要注意的是,每个表只有一个特定“文件名”的条目(行)。现在,当我尝试使用每个“文件名”可以有多个条目(行)的表添加另一个内部联接时,结果变得错误,只有最后一个“总和”显示正确的值,而其他“总和”给出错误价值观。第二个查询是:
select f.filename,
sum(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser,
sum(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,
sum(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather,
sum(case when m.extras like '%hat%' then 1 else 0 end) as hatExtras
from VMdata v
inner join files f on v.id = f.id
inner join DMdata d on f.id = d.id
inner join MultiFiledata m on f.id = m.id
where f.filename in (X,Y,Z)
group by f.filename
任何想法为所有列获得正确的数字?