[英]SUM multiple columns by different condition from same table and then group by date
I tried to SUM values of multiple columns (CASH+CARD+CHEQUE+REST) and group them to display totals for each day.我尝试对多列(CASH+CARD+CHEQUE+REST)的值求和并将它们分组以显示每天的总数。
Actual table:实际表:
Date![]() |
CASH![]() |
CARD![]() |
CHEQUE![]() |
REST ![]() |
TYPE![]() |
---|---|---|---|---|---|
2022-06-02 ![]() |
150.00 ![]() |
200.00 ![]() |
0.00 ![]() |
12.00 ![]() |
STORE1![]() |
2022-06-02 ![]() |
150.00 ![]() |
240.00 ![]() |
56.00 ![]() |
67.00 ![]() |
STORE2![]() |
2022-06-02 ![]() |
45.00 ![]() |
459.00 ![]() |
150.00 ![]() |
0.00 ![]() |
STORE3![]() |
2022-06-02 ![]() |
45.00 ![]() |
400.00 ![]() |
150.00 ![]() |
34.00 ![]() |
TRAVEL1![]() |
2022-06-03 ![]() |
87.00 ![]() |
59.00 ![]() |
150.00 ![]() |
400.00 ![]() |
STORE1![]() |
2022-06-03 ![]() |
45.00 ![]() |
790.00 ![]() |
450.00 ![]() |
104.00 ![]() |
STORE2![]() |
2022-06-03 ![]() |
70.00 ![]() |
30.00 ![]() |
0.00 ![]() |
241.00 ![]() |
STORE3![]() |
2022-06-03 ![]() |
30.00 ![]() |
120.00 ![]() |
11.00 ![]() |
72.00 ![]() |
TRAVEL1![]() |
I want it to make it as new table as:我希望它成为新表:
Date![]() |
TOTAL_SALE ![]() |
STORE_TOTAL ![]() |
TRAVEL_TOTAL ![]() |
---|---|---|---|
2022-06-02 ![]() |
2158.00 ![]() |
1529.00 ![]() |
629.00 ![]() |
2022-06-03 ![]() |
2659.00 ![]() |
2426.00 ![]() |
233.00 ![]() |
I tried with UNION but that puts the result one below other and not like this in the same row, I also tried the option below but am getting this error我尝试使用 UNION 但将结果放在另一个之下而不是像这样在同一行中,我也尝试了下面的选项但收到此错误
#1111 invalid use of group function
#1111 无效使用组 function
Code I use that produces an error:我使用的代码会产生错误:
SELECT DATE, (SUM(CASH) + SUM(CARD) + SUM(CHEQUE) + SUM(REST)) AS TOTAL_SALE,
SUM(case when TYPE LIKE 'STORE%' then (SUM(CASH) + SUM(CARD) + SUM(CHEQUE) + SUM(REST))
else 0 end) as STORE_TOTAL,
SUM(case when TYPE LIKE 'TRAVEL%' then (SUM(CASH) + SUM(CARD) + SUM(CHEQUE) + SUM(REST))
else 0 end) as TRAVEL_TOTAL
FROM tbl_Payment where DATE BETWEEN '2022-06-02' AND '2022-06-03'
GROUP BY DATE ASC
First, group by
does not take an order.首先,
group by
不接受订单。 No asc
.没有
asc
。
The problem is putting sum
inside a sum
.问题是将
sum
放入sum
中。 Whatever is inside sum
will be summed per row , there's no need to sum it again. sum
中的任何内容都将按行求和,无需再次求和。
Similarly, you don't have to sum each column and then add the sums.同样,您不必对每一列求和,然后再相加。 Add the columns then sum.
添加列然后求和。 This is a bit more succinct.
这更简洁一些。
SELECT
"DATE",
SUM(CASH + CARD + CHEQUE + REST) AS TOTAL_SALE,
SUM(
case when TYPE LIKE 'STORE%' then
CASH + CARD + CHEQUE + REST
else
0
end
) as STORE_TOTAL,
SUM(
case when TYPE LIKE 'TRAVEL%' then
CASH + CARD + CHEQUE + REST
else
0
end
) as TRAVEL_TOTAL
FROM tbl_Payment
where "DATE" BETWEEN '2022-06-02' AND '2022-06-03'
GROUP BY "DATE"
Note: date
is a SQL keyword.注意:
date
是 SQL 关键字。 It can be confused with the type date
.它可能与类型
date
混淆。 Avoid using it as a column name.避免将其用作列名。 Use the
at
and on
conventions for naming timestamp and date columns.使用
at
和on
约定命名时间戳和日期列。 For example, paid_on
.例如,
paid_on
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.