SELECT X WHERE X使用Y> 1

时间:2018-06-19 20:02:09

标签: sql

我可以计算一次使用折扣代码多次的订单吗?

from functools import reduce

所以我希望它重新带回订单:151和161

Order       Codes
123         mowerwo
151         femorfm
151         geirmgr
151         rtert
161         erger
161         wefww
162         wweff

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

SELECT order_num
FROM orderdiscount
GROUP BY discountcode
HAVING COUNT (*) > 1;

如果仅需要代码,则无需进行不必要的计数

答案 1 :(得分:0)

您需要将所有列包括在selectgroup by 列表(此处为order_num)中,这不是诸如 sum之类的聚合函数,计数,平均

count(*)count(1)count(0)count(order_num)都给出相同的结果。

请使用:

SELECT order_num, count(1) as discount
  FROM ordersdiscount 
 GROUP BY order_num
HAVING count(1)>1;

order_num   discount
   151         3
   161         2

SQL Fiddle Demo