简体   繁体   English

如何在SQL查询中创建一列的SUM?

[英]How can I create a SUM of one column within a query in SQL?

I have one table with multiple columns. 我有一个带有多列的表。 I am needing to create an SQL query to display certain columns based off of the user input from a datepicker and a select box. 我需要创建一个SQL查询以根据日期选择器和选择框的用户输入显示某些列。 I have done that just fine. 我做的很好。 However, I am needing one of the result columns to be totaled at the bottom of the display table. 但是,我需要将结果列之一总计在显示表的底部。 I cannot figure out how to create a result within my current query to display the totaled column. 我无法弄清楚如何在当前查询中创建结果以显示总计列。 Essentially, I would like the WTTotal column to be totaled into a separate cell. 本质上,我希望将WTTotal列总计到一个单独的单元格中。 My current query is below. 我当前的查询如下。 I really think this is something simple that I just can't seem to see. 我真的认为这很简单,我似乎看不到。

SELECT REPLACE(Client,',',' ') AS Client,WorkTicketNum,Lease,Date,OrderedBy,WTTotal 
FROM WorkTicket 
WHERE Date BETWEEN '2014-10-01' AND '2014-10-31' AND Invoiced IS NULL 
ORDER BY Client 
SELECT REPLACE(Client,',',' ') AS Client,WorkTicketNum,Lease,Date,OrderedBy,WTTotal 
FROM WorkTicket 
WHERE Date BETWEEN '2014-10-01' AND '2014-10-31' AND Invoiced IS NULL 
ORDER BY Client 
UNION ALL
SELECT 'Total', NULL, NULL, NULL, NULL, SUM(WTTotal)
FROM WorkTicket
WHERE Date BETWEEN '2014-10-01' AND '2014-10-31' AND Invoiced IS NULL;

You can't have it with a single query. 您无法通过单个查询获得它。 Putting a SUM() into your query would require a GROUP BY clause and collapse all of your original result rows down into a single result row - meaning your table would disappear. SUM()放入查询将需要GROUP BY子句,并将所有原始结果行折叠为单个结果行-这意味着表将消失。 What you want a is a client-side solution. 您想要的是客户端解决方案。 eg Start a counter in your client code and manually add up the results. 例如,在客户代码中启动一个计数器,然后手动将结果相加。 In pseudoish-code: 用伪代码:

$sum = 0;
while( $row = fetch_from_db() ) {
   $sum += $row['field_to_sum'];
   display_row();
}
echo "Total: $sum";

If you want to have it in a separate column, not in a separate row, either you calculate it in a subquery and join it to your result: 如果要在单独的列而不是单独的行中使用它,请在子查询中对其进行计算,然后将其加入结果中:

SELECT REPLACE(Client,',',' ') AS Client,WorkTicketNum,Lease,Date,OrderedBy, my_calculated_sum AS WTTotal 
FROM WorkTicket
JOIN (SELECT SUM(whatever_you_want_to_sum) as my_calculated_sum FROM WorkTicket) subquery_alias
WHERE Date BETWEEN '2014-10-01' AND '2014-10-31' AND Invoiced IS NULL 
ORDER BY Client 

Or you just calculate it with variables, like a running total. 或者,您仅使用变量来计算它,例如运行总计。 Your result is in the last row of the column. 您的结果在该列的最后一行。

SELECT REPLACE(Client,',',' ') AS Client,WorkTicketNum,Lease,Date,OrderedBy, @sum_variable := @sum_variable + whatever_you_want_to_sum AS WTTotal 
FROM WorkTicket 
, (SELECT @sum_variable := 0) var_init_subquery
WHERE Date BETWEEN '2014-10-01' AND '2014-10-31' AND Invoiced IS NULL 
ORDER BY Client 

But actually Kevin's solution is very good. 但实际上Kevin的解决方案非常好。

I also have similar solution as Kevin's : 我也有和Kevin相似的解决方案:

;with cte_result
as 
(
    SELECT REPLACE(Client,',',' ') AS Client,WorkTicketNum,Lease,Date,OrderedBy,WTTotal 
    FROM   WorkTicket 
    WHERE  Date BETWEEN '2014-10-01' AND '2014-10-31' AND Invoiced IS NULL 
)
select * 
from   cte_result
union
select 'TotalAmount',NULL,NULL,NULL,NULL,sum(WTTotal)
from   cte_result
order  by client

but this solution will work only if your DB Engine supports CTE. 但是仅当您的数据库引擎支持CTE时,此解决方案才有效。 with a CTE, we can use same resultset to get total's row. 使用CTE,我们可以使用相同的结果集来获取合计的行。

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

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