如果我有以下数据
Cust No. | Action | Val
----------| --------------| ----
10 | Checked out | 1.0
10 | PAID | 40.0
10 | Checked In | 1.0
15 | Flew Away | 2.0
15 | PAID | 100.00
15 | Came back | 1.0
20 | PAID | 150.00
30 | Checked In | 1.0
30 | PAID | 50.00
30 | PAID | 10.00
如何通过SUM
条目获得每位客户的PAID
值Checked In
即。
Cust No. | Total Paid
----------| --------------
30 | 60.00
10 | 40.00
答案 0 :(得分:0)
这应该有效
select customer_number, sum(value) as total_paid
from table_name where action = 'PAID' group by customer_number;
答案 1 :(得分:0)
使用CASE
条件和SUM
一样
sum(case when Action = 'paid' then val end)
如果使用外部查询(例如
)稍微调整查询,该怎么办?select customer_number, PaymentDone
from (
select customer_number,
sum(case when Action = 'paid' then val end) as PaymentDone,
group_concat(Action ORDER BY Action asc) as Action_List
from tbl1
group by customer_number) xxx
where Action_List like 'Checked In%';
答案 2 :(得分:0)
我没有Spark可以测试,但是有一种方法是使用HAVING
来确定是否有“检查过”'在组中排;
SELECT `Cust No`, SUM(CASE WHEN Action='PAID' THEN val END) paid
FROM mytable
GROUP BY `Cust No`
HAVING MAX(CASE WHEN Action='Checked In' THEN 1 ELSE 0 END) > 0
+---------+------+
| Cust No | paid |
+---------+------+
| 10 | 40 |
| 30 | 60 |
+---------+------+
另一种方法是使用WHERE
进行选择,先找到这些组,然后只对它们求和;
SELECT `Cust No`, SUM(val) paid
FROM mytable
WHERE action='PAID' AND `Cust No` IN (
SELECT `Cust No` FROM mytable WHERE action='Checked In'
)
GROUP BY `Cust No`
答案 3 :(得分:0)
这应该有效:
select customer.customer_number, sum(value) as total_paid from customers
left join
(
select customer_number from users where action = 'Checked In' group by customer_number
)checkins on checkins.customer_number = customers.customer_number
where checkins.customer_number is not null and customers.action = 'PAID'
group by customers.customer_number