在表格中的行下方显示行总和

时间:2018-09-10 11:00:55

标签: mysql aggregate rollup

我有表tblservicesummary

enter image description here

我从以下查询获得了结果集

select ss.Quantity as Qty ,ss.Item , ss.ES_ServiceStart as Time, fr.functionRoom from tblServiceSummary ss 
join servdesc sd on ss.Service_time =sd.ServDescID  
join funct fn on fn.FunctID = ss.FunctionID 
join tbl_functionRoom fr on fr.id=fn.EvtID
where fn.StartDate between '2018-09-19' and '2018-09-19' 
and ss.Item != ''  
order by ss.Item 

像这样

我想显示为

表示行数据和行总和。

只想知道在MySQL中这可能吗?我无法按功能分组。

我发现的唯一相关内容是汇总功能。

我尝试了

查询

SELECT Quantity, Item, count(Quantity) AS sumoption
FROM tblServiceSummary 
GROUP BY Quantity WITH ROLLUP

但这给了我类似的结果

enter image description here

也尝试过

SELECT Quantity, Item, count(Quantity) AS sumoption
FROM tblServiceSummary 
GROUP BY Quantity WITH ROLLUP

请提出建议

1 个答案:

答案 0 :(得分:1)

您可以使用“求和-按项目分组”来进行并集,并选择相应的顺序。

select '' as Kind, ss.Quantity as Qty, ss.Item as Item,
    ss.ES_ServiceStart as Time, fr.functionRoom
from tblServiceSummary ss 
join servdesc sd on ss.Service_time =sd.ServDescID  
join funct fn on fn.FunctID = ss.FunctionID 
join tbl_functionRoom fr on fr.id=fn.EvtID
where fn.StartDate between '2018-09-19' and '2018-09-19' 
and ss.Item != ''  

union

select 'SUBTOTAL', sum(ss.Quantity), ss.Item, '', ''
from tblServiceSummary ss 
join funct fn on fn.FunctID = ss.FunctionID 
where fn.StartDate between '2018-09-19' and '2018-09-19' 
and ss.Item != ''  
group by ss.Item

order by Item, Kind