简体   繁体   English

sql 子和总计

[英]sql sub and grand total

Is there a nice consise way to get grand totals here-- this query shows sub totals for each day, but how can I get the grand total of each sub total without a sub query or another query是否有一种很好的简洁方式来获取总计 - 此查询显示每天的小计,但是如何在没有子查询或其他查询的情况下获得每个小计的总计

also is there something better to use than compute sum?还有比计算总和更好的东西吗?

SELECT loguser,logdate,logaction,logtime
FROM log WHERE loguser IN
('scott')   AND logdate  
BETWEEN '2011-06-01' AND '2011-06-15'   
ORDER BY logdate DESC    
COMPUTE SUM(logtime) BY logdate

SQL Server 2008 R2 SQL 服务器 2008 R2

I'm not familiar with COMPUTE as you've used it, but this (or a variation on it) should work in most SQL dialects...我对 COMPUTE 不熟悉,因为您使用过它,但是这个(或它的变体)应该适用于大多数 SQL 方言......

SELECT loguser,logdate,logaction,SUM(logtime)
FROM log WHERE loguser IN
('scott')   AND logdate  
BETWEEN '2011-06-01' AND '2011-06-15'
GROUP BY loguser,logdate,logaction WITH ROLLUP
ORDER BY logdate DESC    

Better way to get grand total is to use front end..instead of query.获得总计的更好方法是使用前端..而不是查询。

I think you should look at using ROLLUP over COMPUTE as Dems suggests, but if what you want is 3 things returned- 1) the daily logtime data, 2) the daily subtotal;我认为您应该按照 Dems 的建议考虑在 COMPUTE 上使用 ROLLUP,但是如果您想要返回 3 件事 - 1)每日日志时间数据,2)每日小计; and, 3) the grand total (total of subtotals) then I think you will need to UNION a query that finds the first two like the one you have with a query that finds the grand total.并且,3)总计(小计总计)然后我认为您将需要 UNION 一个查询,该查询可以找到前两个,就像您使用找到总计的查询一样。

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

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