MySQL: Output ID based on SUM data in a column based attributes in 另一列
MySQL: Output ID based on SUM data in one column based attributes in another column
SQL Table:
Customer
Type
Payment
1
Apples
5
1
Apples
5
1
Oranges
1
1
Oranges
2
2
Apples
7
2
Oranges
3
2
Oranges
6
根据以上内容,确定哪些客户为苹果支付的费用比橙子多,作为他们所有付款的总和。
如上table,
客户 1 - 苹果 10 > 橙子 3
客户 2 - 苹果 7 < 橘子 9
因此 SQL 应该输出 Customer 1
我尝试了多个查询,以下是最有希望的查询,但对组函数的使用无效错误代码 1111。
SELECT a.customer
FROM (SELECT customer, SUM(payment) AS orangespaid FROM table
WHERE type ='Oranges'
GROUP BY customer) o
JOIN table AS a ON a.customer = o.customer
WHERE type = 'Apples' and SUM(payment) > orangespaid
GROUP BY customer
ORDER BY customer;
尝试将 SUM 移动到第二个子查询中
SELECT a.customer
FROM (SELECT customer, SUM(payment) AS orangespaid FROM table
WHERE type ='Oranges'
GROUP BY customer) o
JOIN (SELECT customer, SUM(payment) AS applespaid FROM table
WHERE type ='Apples'
GROUP BY customer) AS a ON a.customer = o.customer
WHERE applespaid > orangespaid
ORDER BY customer;
您应该针对您想要的每种类型尝试使用 sum(case when)
,它可能不是最佳解决方案,但它确实有效。
select a.customer
from (select as1.Customer,
sum(case when type = 'Oranges' then payment else 0 end) AS orangespaid,
sum(case when type = 'Apples' then payment else 0 end) AS applespaid
from as1 group by as1.Customer) a
where applespaid > orangespaid
有很多方法可以实现。
下面是没有子查询的方法:
SELECT Customer,
SUM(CASE WHEN Type='Apples' THEN Payment ELSE 0 END) AS Apples,
SUM(CASE WHEN Type='Oranges' THEN Payment ELSE 0 END) AS Oranges
FROM table1
GROUP BY Customer
HAVING Apples > Oranges;
或者像这样:
SELECT Customer,
SUM(IF(Type='Apples',Payment,0)) > SUM(IF(Type='Oranges',Payment,0)) Chk
FROM table1
GROUP BY Customer
HAVING Chk=1
或者稍微修改上面的查询,而不是检查 SELECT
中的值然后从 HAVING
中过滤,为什么不直接在 HAVING
中进行检查:
SELECT Customer
FROM table1
GROUP BY Customer
HAVING SUM(IF(Type='Apples',Payment,0)) > SUM(IF(Type='Oranges',Payment,0)) != 0;
第一个查询也可以用类似的方式完成。
旁注:
至于使用CASE
和IF
的区别,基本上是一样的,看个人喜好吧。我主要选择使用 CASE
因为可读性和更易于编辑(parentheses/brackets 的使用不多)但是几乎每次使用 IF
都更短。
SQL Table:
Customer | Type | Payment |
---|---|---|
1 | Apples | 5 |
1 | Apples | 5 |
1 | Oranges | 1 |
1 | Oranges | 2 |
2 | Apples | 7 |
2 | Oranges | 3 |
2 | Oranges | 6 |
根据以上内容,确定哪些客户为苹果支付的费用比橙子多,作为他们所有付款的总和。
如上table,
客户 1 - 苹果 10 > 橙子 3
客户 2 - 苹果 7 < 橘子 9
因此 SQL 应该输出 Customer 1
我尝试了多个查询,以下是最有希望的查询,但对组函数的使用无效错误代码 1111。
SELECT a.customer
FROM (SELECT customer, SUM(payment) AS orangespaid FROM table
WHERE type ='Oranges'
GROUP BY customer) o
JOIN table AS a ON a.customer = o.customer
WHERE type = 'Apples' and SUM(payment) > orangespaid
GROUP BY customer
ORDER BY customer;
尝试将 SUM 移动到第二个子查询中
SELECT a.customer
FROM (SELECT customer, SUM(payment) AS orangespaid FROM table
WHERE type ='Oranges'
GROUP BY customer) o
JOIN (SELECT customer, SUM(payment) AS applespaid FROM table
WHERE type ='Apples'
GROUP BY customer) AS a ON a.customer = o.customer
WHERE applespaid > orangespaid
ORDER BY customer;
您应该针对您想要的每种类型尝试使用 sum(case when)
,它可能不是最佳解决方案,但它确实有效。
select a.customer
from (select as1.Customer,
sum(case when type = 'Oranges' then payment else 0 end) AS orangespaid,
sum(case when type = 'Apples' then payment else 0 end) AS applespaid
from as1 group by as1.Customer) a
where applespaid > orangespaid
有很多方法可以实现。
下面是没有子查询的方法:
SELECT Customer,
SUM(CASE WHEN Type='Apples' THEN Payment ELSE 0 END) AS Apples,
SUM(CASE WHEN Type='Oranges' THEN Payment ELSE 0 END) AS Oranges
FROM table1
GROUP BY Customer
HAVING Apples > Oranges;
或者像这样:
SELECT Customer,
SUM(IF(Type='Apples',Payment,0)) > SUM(IF(Type='Oranges',Payment,0)) Chk
FROM table1
GROUP BY Customer
HAVING Chk=1
或者稍微修改上面的查询,而不是检查 SELECT
中的值然后从 HAVING
中过滤,为什么不直接在 HAVING
中进行检查:
SELECT Customer
FROM table1
GROUP BY Customer
HAVING SUM(IF(Type='Apples',Payment,0)) > SUM(IF(Type='Oranges',Payment,0)) != 0;
第一个查询也可以用类似的方式完成。
旁注:
至于使用CASE
和IF
的区别,基本上是一样的,看个人喜好吧。我主要选择使用 CASE
因为可读性和更易于编辑(parentheses/brackets 的使用不多)但是几乎每次使用 IF
都更短。