使用SUM的MYSQL查询

时间:2010-04-27 18:02:28

标签: mysql

如何从ON_HAND VALUE列中选择7500或更高版本?

“列出零件编号部分描述和每个零件的现有价值,其现有价值至少为7,500美元。将ON_HAND_VALUES指定为计算列。”

这是我到目前为止所做的,我试过subquerys IN语句,但我无法弄明白。

SELECT PART_NUM, DESCRIPTION, SUM(ON_HAND * PRICE) AS ON_HAND_VALUE
FROM PART
WHERE SUM(ON_HAND * PRICE)'7500'
GROUP BY PART_NUM, DESCRIPTION;

部分表

PART_NUM    DESCRIPTION     ON_HAND     CLASS   WAREHOUSE   PRICE

AT94       Iron               50         HW         3       24.95
BV06       Home Gym           45         SG         2       794.95
CD52       Microwave Oven     32        AP          1       165.00
DL71       Cordless Drill     21        HW          3       129.95
DR93       Gas Range           8        AP          2       495.00
DW11       Washer             12        AP          3       399.99
FD21       Stand Mixer        22        HW          3       159.95
KL62       Dryer              12        AP          1       349.95
KT03       Dishwasher          8        AP          3       595.00
KV29       Treadmill           9        SG          2       1390.00

3 个答案:

答案 0 :(得分:2)

你不能使用where,你必须使用having聚合函数:

SELECT PART_NUM, DESCRIPTION, SUM(ON_HAND * PRICE) AS ON_HAND_VALUE
FROM PART
GROUP BY PART_NUM, DESCRIPTION;
HAVING SUM(ON_HAND * PRICE)'7500'

wheregroupsum ming发生之前进行评估,having在对记录进行过滤和分组后对结果集进行评估

答案 1 :(得分:0)

这就是我用的。

SELECT PART_NUM, DESCRIPTION, SUM(ON_HAND * PRICE) AS ON_HAND_VALUE
FROM PART
GROUP BY PART_NUM
HAVING SUM(ON_HAND * PRICE)  > '7500'
ORDER BY PART_NUM;

答案 2 :(得分:0)

前两个人错了。我知道这是正确的:

SELECT PART_NUM, DESCRIPTION, (ON_HAND * PRICE) AS ON_HAND_VALUE
FROM PART
WHERE (ON_HAND * PRICE) >= 7500