简体   繁体   English

底行的 SQL 查询总和

[英]SQL query sum at bottom row

I am trying to get the sum of a column at the bottom row.我试图在底行获得一列的总和。 I have tried a few examples by using SUM() and COUNT(), but they have all failed with syntax errors.我已经使用 SUM() 和 COUNT() 尝试了一些示例,但它们都因语法错误而失败。

Here is my current code without any sum or anything:这是我当前的代码,没有任何总和或任何东西:

:XML ON
USE MYTABLE
SELECT sbc.PolicyC.PolicyName as namn,COUNT(*) as cnt
FROM sbc.AgentC, sbc.PolicyC
WHERE sbc.AgentC.PolicyGuid = sbc.PolicyC.PolicyGuid
GROUP BY sbc.AgentC.PolicyGuid, sbc.PolicyC.PolicyName ORDER BY namn ASC
FOR XML PATH ('celler'), ROOT('root')
GO

The XML output is reformatted to become a regular HTML table. XML 输出被重新格式化为常规 HTML 表。

EDIT:编辑:

Here is the latest code, but it generates a "sum" (same number as the row above) on every other row:这是最新的代码,但它每隔一行生成一个“总和”(与上面的行相同):

:XML ON
USE MYTABLE
SELECT sbc.PolicyC.PolicyName as namn,COUNT(*) as cnt
FROM sbc.AgentC, sbc.PolicyC
WHERE sbc.AgentC.PolicyGuid = sbc.PolicyC.PolicyGuid
GROUP BY sbc.AgentC.PolicyGuid, sbc.PolicyC.PolicyName with rollup
FOR XML PATH ('celler'), ROOT('root')
GO

The XML output looks like this: XML 输出如下所示:

<root>
<celler>
<namn>example name one</namn>
<cnt>23</cnt>
</celler>
<celler>
<cnt>23</cnt>
</celler>
<celler>
<namn>example name two</namn>
<cnt>1</cnt>
</celler>
<celler>
<cnt>1</cnt>
</celler>
</root>

Try试试

SELECT sbc.PolicyC.PolicyName as namn,COUNT(*) as cnt
FROM sbc.AgentC, sbc.PolicyC
WHERE sbc.AgentC.PolicyGuid = sbc.PolicyC.PolicyGuid
GROUP BY sbc.AgentC.PolicyGuid, sbc.PolicyC.PolicyName 
UNION
SELECT 'TOTAL' as nawn,COUNT(*) as cnt
FROM
FROM sbc.AgentC, sbc.PolicyC
WHERE sbc.AgentC.PolicyGuid = sbc.PolicyC.PolicyGuid
ORDER BY namn ASC

This will compute the total in a separate query.这将在单独的查询中计算总数。 However, you might need to either add some non-printing, high-ASCII character to force the total to the bottom, or add some numeric ordering key... mySQL may also have an operator (similar to WITH ROLLUP in Microsoft SQL) which would be more efficient than the above code... So while this would work, there are probably more efficient options available to you...但是,您可能需要添加一些非打印的高位 ASCII 字符以强制总数到底部,或者添加一些数字排序键……mySQL 可能还有一个运算符(类似于 Microsoft SQL 中的 WITH ROLLUP)会比上面的代码更有效......所以虽然这会起作用,但可能有更有效的选项可供您使用......

MySQL supports a rollup extension to group by. MySQL 支持 汇总扩展以分组。

select * from parts;
+-----------+--------+
| part_name | amount |
+-----------+--------+
| upper     |    100 |
| lower     |    100 |
| left      |     50 |
| right     |     50 |
+-----------+--------+

select part_name
      ,sum(amount)
  from parts
 group 
    by part_name with rollup;

+-----------+-------------+
| part_name | sum(amount) |
+-----------+-------------+
| left      |          50 |
| lower     |         100 |
| right     |          50 |
| upper     |         100 |
| NULL      |         300 |
+-----------+-------------+

Updated to answer comments:更新以回答评论:

The following items list some behaviors specific to the MySQL implementation of ROLLUP:以下各项列出了特定于 MySQL 实现 ROLLUP 的一些行为:

When you use ROLLUP, you cannot also use an ORDER BY clause to sort the results.使用 ROLLUP 时,也不能使用 ORDER BY 子句对结果进行排序。 In other words, ROLLUP and ORDER BY are mutually exclusive.换句话说,ROLLUP 和 ORDER BY 是互斥的。 However, you still have some control over sort order.但是,您仍然可以控制排序顺序。 GROUP BY in MySQL sorts results, and you can use explicit ASC and DESC keywords with columns named in the GROUP BY list to specify sort order for individual columns. MySQL 中的 GROUP BY 对结果进行排序,您可以对 GROUP BY 列表中命名的列使用显式 ASC 和 DESC 关键字来指定各个列的排序顺序。 ( The higher-level summary rows added by ROLLUP still appear after the rows from which they are calculated, regardless of the sort order .) 不管排序顺序如何,ROLLUP 添加的更高级别的汇总行仍然出现在计算它们的行之后。)

My code became somthing like:我的代码变成了这样:

SELECT * FROM (...old code here... UNION ...'Total:' ... COUNT() ...)* z
ORDER BY CASE WHEN z.Namn = 'Total:' THEN '2' ELSE '1' END , z.Antal DESC

I have one column named Namn and one named Antal.我有一个名为 Namn 的列和一个名为 Antal 的列。 If there is the value 'Total:' in the column Namn it will order that as a '2' and if not as a '1', that makes the 'Total:' move to the botton when I have decendent ordering on the Antal column.如果在列 Namn 中有值 'Total:',它会将其排序为 '2',如果不是作为 '1',则当我在 Antal 上有适当的排序时,这会使 'Total:' 移动到底部列。

The magic hapens because the 'Total:' is UNION with the table, and then the CASE statement at the end puts it at the end.神奇的事情发生了,因为“总计:”是与表的联合,然后最后的 CASE 语句将它放在最后。

My complete code that works for me that is a loot moore messy, it unions 2 tables and stuff as well:我的完整代码对我有用,它是一个混乱的战利品,它也结合了 2 个表和其他东西:

SELECT * FROM (
SELECT acrclient.Client_Name AS 'Namn', COUNT(x.client) AS 'Antal'  
FROM 
(SELECT 'B' tab,t.client
FROM asutrans t
where t.voucher_type!='IP' AND t.last_update >= {ts '2019-01-01 00:00:00'}  
UNION ALL SELECT 
'C' tab,t.client
FROM asuhistr t
WHERE t.voucher_type!='IP' AND t.last_update >= {ts '2019-01-01 00:00:00'} )  x
LEFT JOIN acrclient ON x.client = acrclient.client 
GROUP BY x.client, acrclient.Client_Name
UNION ALL
SELECT 'Total:', COUNT(client) FROM (SELECT 'B' tab,t.client
FROM asutrans t
where t.voucher_type!='IP' AND t.last_update >= {ts '2019-01-01 00:00:00'}  
UNION ALL SELECT 
'C' tab,t.client
FROM asuhistr t
WHERE t.voucher_type!='IP' AND t.last_update >= {ts '2019-01-01 00:00:00'} ) y
) z
ORDER BY CASE WHEN z.Namn = 'Total:' THEN '2' ELSE '1' END , z.Antal DESC

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

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