实体数据检索

时间:2012-11-19 18:57:05

标签: sql database

我需要帮助从使用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;

这是我尝试过的代码。但我不断得到错误,它不是表达式的组。有关为何我收到错误的任何帮助?

2 个答案:

答案 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;

Check Out here