我有2代数据表我需要从查询数据SQL计数多列基于
Machine_ID Machine_name
101 Computer1
202 Computer2
303 Computer3
404 Computer4
第二表:
Machine_ID Event_ID Event_date
101 1 03/15/2016
101 1 03/14/2016
101 2 03/15/2016
101 1 03/13/2016
102 1 03/15/2016
102 2 03/15/2016
102 1 03/14/2016
103 2 03/15/2016
103 2 03/14/2016
104 1 03/15/2016
我需要的查询的输出是在一个线路和含有信息:
Machine_ID,
Machine_Name,
Count(Machine_ID) where Event_ID='1'
Count(Machine_ID) where Event_ID='2'
我已经尝试了许多方法来编写查询,但我在正确的语法损失。
这并不工作,但会给什么我要去为我的想法:
Select M.Machine_ID, M.Machine_Name,
count(Machine_ID) where E.Event_ID='1' and Machine_ID='101',
count(Machine_ID) where E.Event_ID='2' and Machine_ID='101'
from Machines M
join Events E on M.Machine_ID = E.Machine_ID
从这个基于给定数据的输出应该是:
101 Computer1 3 1
[如何使用一个SQL查询获取多个计数?]可能的重复?(http://stackoverflow.com/questions/12789396/how-to-get-get-multiple-counts-with-one-sql-query) –