初学者SQL开发人员,需要从查询中排除一些结果
我有一个sql查询来显示stage_时间表中所有stage_时间的总和。但我只需要为所有3个阶段输入值的车辆的阶段_时间之和,因此在本例中,3号车辆不在列表中 下面是一个SQLFIDLE示例代码初学者SQL开发人员,需要从查询中排除一些结果,sql,Sql,我有一个sql查询来显示stage_时间表中所有stage_时间的总和。但我只需要为所有3个阶段输入值的车辆的阶段_时间之和,因此在本例中,3号车辆不在列表中 下面是一个SQLFIDLE示例代码 选择d.*,求和(阶段时间)作为总计 来自驾驶员d,阶段\时间st 其中,st.Driver\u Id=d.Driver\u Id和st.Event\u Id=1 和st.Stage_Id您可以添加Having子句 having count(distinct st.Stage_id) = 3 见更新
选择d.*,求和(阶段时间)作为总计
来自驾驶员d,阶段\时间st
其中,st.Driver\u Id=d.Driver\u Id和st.Event\u Id=1
和st.Stage_Id您可以添加Having子句
having count(distinct st.Stage_id) = 3
见更新
(顺便说一句,我将您的查询改为使用join,这是一种更好的方法)
having count(distinct st.Stage_id) = 3
select
d.*
, tt.TotalTime
from
(
select
d.Driver_Id
, SUM( st.Stage_Time ) TotalTime
from
Driver d
inner join Stage_Times st
on st.Driver_Id = d.Driver_Id
where
st.Event_Id = 1
group by
d.Driver_Id
having
count(1) = 3
) tt
inner join Driver d
on tt.Driver_Id = d.Driver_id