简体   繁体   English

联合+ COUNT()中的SQL查询变量

[英]SQL query variable in union + COUNT()

I need help with an SQL query where i need to 我需要有关SQL查询的帮助

 SELECT MachineID from TABLE1

and then 接着

Count(Passfail) from TABLE2 WHERE MachineID= *MACHINEID

from the SELECT statement previously into a single table from the SELECT先前from the SELECT语句到单个表

You've almost written it right there. 您几乎已经在那儿写了。 You need to read up a bit on joins and sql group by : 您需要通过以下内容阅读有关joins和sql group by

select T1.MachineId, count(T2.Passfail) from
Table1 T1 inner join Table2 T2 on T1.MachineId = T2.Machine Id
group by T1.MachineId

EDIT: For a separate count depending on values of PassFail, I'd use: 编辑:根据PassFail的值的单独计数,我将使用:

select T1.MachineId, 
sum(case when T2.PassFail='pass' then 1 else 0 end) as 'pass',
sum(case when T2.PassFail='fail' then 1 else 0 end) as 'fail'
from Table1 T1 inner join 
Table2 T2 on T1.MachineId = T2.machineId 
group by T1.machineId 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM