如何根据A列中的数据将B列的SQL搜索结果汇总为总和?

时间:2018-05-30 09:41:02

标签: mysql sql sql-server

我创建了一个SQL公式来搜索特定计算机的信号状态所花费的时间:

SELECT 'Core' as Core, Z.EventName, ISNULL(DIFF,0) AS DIFF 
FROM (select distinct MacID, EventName from dbo.tblMachine a  
join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur) 
b on b.EventName <> '' and MacID in ('A06','A07', 'A08', 'A09', 'B01','B05','B09', 'B10', 'B11', 'C04', 'C08', 'C09') ) 
z LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) 
as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) 
as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A 
LEFT JOIN ( SELECT macid, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff 
FROM  dbo.tblEvtDur  WHERE DayID between '20180401' and '20180430'  
GROUP BY MacID ) B ON  A.MacID = B.MacID 
WHERE DayID between '20180401' and '20180430'  
AND A.MacID in
 ('A06','A07', 'A08', 'A09', 'B01','B05','B09', 'B10', 'B11', 'C04', 'C08', 'C09') 
group by A.MacID, A.EventName) a 
ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName

我得到的结果如下所示:

enter image description here

现在我想让SQL显示这些特定机器的EventName BLK,MDL,MTL,OFF,RPR和RUN的总和。这可能吗?

编辑:

如果我尝试使用group by子句添加一个sum,它对当前的SQL不起作用。

所以我不得不重建它:

select 'Core' as Core, EventName, sum(DATEDIFF(second, startdt, EndDT)) as DIFF 
from dbo.tblEvtDur

where MacID in ('A06','A07','A08','A09','B01','B05','B09','B10','B11','C04','C08','C09')

and DayID between '20180531' and '20180531'

group by eventname

但根据当天,某些信号不会出现,因此未在结果表中列出。

enter image description here

我应该添加什么来告诉SQL向我显示EventName BLK为0?

编辑2:

到目前为止,Dan尝试使用正确加入的建议:

select 'Core' as [Core], a.EventName, isnull((sum(DATEDIFF(second, startdt, EndDT))),0) as DIFF 
from dbo.tblEvtDur a
right join (select distinct eventname from dbo.tblEvtDur where EventName in     
('BLK', 'MDL', 'MTL', 'OFF', 'RPR', 'RUN')) b 
on a.EventName = b.EventName
where MacID in ('A06','A07', 'A08', 'A09', 'B01','B05',
'B09', 'B10', 'B11', 'C04', 'C08', 'C09')
and DayID between '20180531' and '20180531'
group by a.eventname

同样,结果相同,但所花费的时间要长得多。

4 个答案:

答案 0 :(得分:0)

将聚合函数(sum,count,avg ...)与group by一起使用。

 select count(*) from (output of your query) group by EventName;

答案 1 :(得分:0)

由于格式化,您的代码有点难以阅读,但正如@Piyush Ghediya所说,您可以使用sum by group by子句。

{{1}}

答案 2 :(得分:0)

你需要这样的东西,因为你需要在单个查询中使用sum和condition所需的所有值。

DECLARE @tbl TABLE(name VARCHAR(50), quantity INT, saledate DATE)
INSERT INTO @tbl(name, quantity, saledate)
VALUES('A', 20, '2018-05-31'), ('B', 50, '2018-05-25'), ('c', 40, '2018-05-20'), ('a', 40, '2018-05-10'), ('d', 20, '2018-05-15'), ('c', 20, '2018-05-20')
SELECT a.name, SUM(CASE WHEN a.saledate > '2018-05-15'
  THEN a.quantity ELSE 0 END)
FROM @tbl a
WHERE(a.saledate > '2018-05-15'
  OR a.name in ('a', 'b', 'c', 'd')) GROUP BY a.name

答案 3 :(得分:0)

最后找到了解决问题的方法,我所要做的就是从select函数中删除MacID并声明EventName:

SELECT 'Core' as Core, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM 
(select distinct EventName from dbo.tblMachine a 

join 
(SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur) b 
on b.EventName <> '' and eventname in ('BLK', 'MDL', 'MTL', 'OFF', 'RPR', 'RUN') 
and MacID in ('A06','A07', 'A08', 'A09', 'B01','B05','B09', 'B10', 'B11', 'C04', 'C08', 'C09')) Z 

LEFT JOIN (SELECT A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff, 
round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) as decimal(30,8)),4) * 100 AS PER 
FROM dbo.tblEvtDur  A 

LEFT JOIN ( SELECT EVENTNAME, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff 
FROM dbo.tblEvtDur  WHERE DayID between '20180401' and '20180430' GROUP BY EVENTNAME ) B 
ON  A.EVENTNAME = B.EVENTNAME 

WHERE DayID between '20180401' and '20180430' 
AND A.MacID in ('A06','A07', 'A08', 'A09', 'B01','B05','B09', 'B10', 'B11', 'C04', 'C08', 'C09') 
and A.eventname in ('BLK', 'MDL', 'MTL', 'OFF', 'RPR', 'RUN') 
group by A.EventName) a ON A.EVENTNAME = Z.EVENTNAME and Z.EVENTNAME  = a.EVENTNAME order by  Z.EventName