我需要帮助从使用sql Server的简单实体中提取数据。
广告资源实体包含(pk,fk)product_id, (pk,fk)warehouse_id, inventory_amount, inventory_max_stock, inventory_reorder_point
列。
我需要将库存记录作为产品ID列表和以下列的总和:库存量,库存最大库存和库存再订货点。仅显示inventory_amount > 900
的结果。
SELECT PRODUCT_ID, SUM(INVEN_AMOUNT), SUM(INVEN_MAX_STOCK),
SUM(INVEN_REORDER_POINT) FROM INVENTORY
GROUP BY PRODUCT_ID HAVING INVEN_AMOUNT > 900;
这是我尝试过的代码。但我不断得到错误,它不是表达式的组。有关为何我收到错误的任何帮助?
答案 0 :(得分:0)
HAVING INVEN_AMOUNT > 900
分组,因此不允许 INVEN_AMOUNT
。改为使用WHERE:
SELECT PRODUCT_ID, SUM(INVEN_AMOUNT), SUM(INVEN_MAX_STOCK), SUM(INVEN_REORDER_POINT)
FROM INVENTORY
WHERE INVEN_AMOUNT > 900
GROUP BY PRODUCT_ID;
如果您想要使用总和>限制这些组900,然后使用SUM(INVEN_AMOUNT)
:
SELECT PRODUCT_ID, SUM(INVEN_AMOUNT), SUM(INVEN_MAX_STOCK), SUM(INVEN_REORDER_POINT)
FROM INVENTORY
GROUP BY PRODUCT_ID
HAVING SUM(INVEN_AMOUNT) > 900;
答案 1 :(得分:0)
INVEN_AMOUNT
无法与having
一起使用,但SUM(INVEN_AMOUNT)
有效。 Having
用于汇总函数,例如sum(),avg()和max()等,否则使用where
会更好
SELECT PRODUCT_ID, SUM(INVEN_AMOUNT) as INVEN_AMOUNT,
SUM(INVEN_MAX_STOCK), SUM(INVEN_REORDER_POINT) FROM INVENTORY
GROUP BY PRODUCT_ID Having SUM(INVEN_AMOUNT) > 900;