Mytable with Id as autoincrement Customer,Date and Amount 我想汇总一下,每天都会计算每个客户的金额 我的意思是汇总按日期和客户分组 这应该是我的结果:
+---------+--------------+--------------+------------+
| Id | Date | Customer | Amount |
+---------+--------------+--------------+------------+
| 1 | 2017-09-19 | B | 10 |
| 4 | 2017-09-19 | B | 15 |
| 8 | 2017-09-19 | B | 02 |
| 6 | 2017-09-19 | B | 18 |
| 5 | 2017-09-19 | B | 05 |
| Total | NULL | B | 50 |
| 9 | 2017-09-19 | C | 11 |
|14 | 2017-09-19 | C | 10 |
| 12 | 2017-09-19 | C | 09 |
| Total | NULL | C | 30 |
| 11 | 2017-09-18 | B | 20 |
| 15 | 2017-09-18 | B | 40 |
| Total | NULL | B | 60 |
| 10 | 2017-09-18 | A | 1 |
| 13 | 2017-09-18 | A | 1 |
| 16 | 2017-09-18 | A | 1 |
| 7 | 2017-09-18 | A | 1 |
| 3 | 2017-09-18 | A | 1 |
| Total | NULL | A | 5 |
| 2 | 2017-09-18 | C | 90 |
| Total | NULL | C | 90 |
我试过了:
Select
Mytable.Id,
Mytable. Date,
Mytable.Customer,
Mytable.Amount
From Mytable
GROUP BY Mytable. Date, Mytable.Customer WITH ROLLUP
但是这不能按预期工作。请帮助
答案 0 :(得分:1)
您需要汇总。这样做你想要的吗?
UICollectionViewDelegateFlowLayout
如果你确实想要select t.Date, t.Customer,
sum(t.Amount) as Amount
from Mytable t
group by t.Date, t.Customer with rollup;
,那么:
id
答案 1 :(得分:0)
我认为以下查询可以解决您的问题。
library(stringr)
regex <- "([0-9]+)|([a-zA-Z]+)|([[:punct:]]+)"
s <- "123d4ss" # digit -> alpha -> digit -> alpha
str_count(s, regex) # gets 4
s <- ",!" # punct only
str_count(s, regex) # gets 1
s <- ",1!Aa9" # punct -> digit -> punct -> alpha -> digit
str_count(s, regex) # gets 5
答案 2 :(得分:0)
您需要在id
上添加group by
列
对于版本<= 5.7
查询:
select
coalesce(id, 'Total') as id,
date,
customer,
sum(amount) as amount
from
mytable
group by date, customer, id with ROLLUP
having customer is not null;
结果:
id | date | customer | amount :---- | :--------- | :------- | -----: 3 | 2017-09-18 | A | 1 7 | 2017-09-18 | A | 1 10 | 2017-09-18 | A | 1 13 | 2017-09-18 | A | 1 16 | 2017-09-18 | A | 1 Total | 2017-09-18 | A | 5 11 | 2017-09-18 | B | 20 15 | 2017-09-18 | B | 40 Total | 2017-09-18 | B | 60 2 | 2017-09-18 | C | 90 Total | 2017-09-18 | C | 90 1 | 2017-09-19 | B | 10 4 | 2017-09-19 | B | 15 5 | 2017-09-19 | B | 5 6 | 2017-09-19 | B | 18 8 | 2017-09-19 | B | 2 Total | 2017-09-19 | B | 50 9 | 2017-09-19 | C | 11 12 | 2017-09-19 | C | 9 14 | 2017-09-19 | C | 10 Total | 2017-09-19 | C | 30
对于版本> 5.7
查询:
select
coalesce(id, 'Total') as id,
date,
customer,
sum(amount) as amount
from
mytable
group by date, customer, id with ROLLUP
having grouping(customer) = 0;
结果:
id | date | customer | amount :---- | :--------- | :------- | -----: 3 | 2017-09-18 | A | 1 7 | 2017-09-18 | A | 1 10 | 2017-09-18 | A | 1 13 | 2017-09-18 | A | 1 16 | 2017-09-18 | A | 1 Total | 2017-09-18 | A | 5 11 | 2017-09-18 | B | 20 15 | 2017-09-18 | B | 40 Total | 2017-09-18 | B | 60 2 | 2017-09-18 | C | 90 Total | 2017-09-18 | C | 90 1 | 2017-09-19 | B | 10 4 | 2017-09-19 | B | 15 5 | 2017-09-19 | B | 5 6 | 2017-09-19 | B | 18 8 | 2017-09-19 | B | 2 Total | 2017-09-19 | B | 50 9 | 2017-09-19 | C | 11 12 | 2017-09-19 | C | 9 14 | 2017-09-19 | C | 10 Total | 2017-09-19 | C | 30
小提琴:
db <>提琴here
注意:当使用group by
而没有 ERROR 1055 时,您需要设置为
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));