简体   繁体   English

基于两个聚合函数返回计算字段的结果

[英]Return results on a calculated field based on two aggregate functions

The SQL query returns the results i'm looking for however, i would like to exclude the results where balance (aliased field) equals zero, to reduce the set of results that appear. SQL 查询返回我正在寻找的结果,但是,我想排除余额(别名字段)等于零的结果,以减少出现的结果集。

I've using Where "balance" <> 0 and variations this, but keep getting error aggregation cannot be used in where clause.我已经使用 Where "balance" <> 0 和变体,但不断收到错误聚合不能在 where 子句中使用。 I tried nested selects as well, same error我也尝试了嵌套选择,同样的错误

SELECT "Item"."ItemCode"
,"ItmWhs"."WhsCode"
,"ItmWhs"."AvgPrice"
,"ItmWhs"."OnHand" AS "On Hand"
,"ItemGrp"."ItmsGrpNam"
,"Item"."ItmsGrpCod"
,"Item"."U_StyleCode"
,"Item"."U_StyleName"
,"Item"."U_ColourCode"
,"Item"."U_ColourName"
,"Item"."U_Size"
,"Item"."U_Gender"
,"Item"."U_LCC"
,SUM("Document"."InQty")
,SUM("Document"."OutQty")
,(SUM("Document"."InQty") - SUM("Document"."OutQty")) AS "Balance"
FROM "SBK_UA"."OINM" AS "Document"
,"SBK_UA"."OITW" AS "ItmWhs"
,"SBK_UA"."OITM" AS "Item"
INNER JOIN "SBK_UA"."OITB" AS "ItemGrp" ON "Item"."ItmsGrpCod" = "ItemGrp"."ItmsGrpCod"
WHERE "Document"."ItemCode" = "ItmWhs"."ItemCode"
AND "Document"."Warehouse" = "ItmWhs"."WhsCode"
AND "Item"."ItemCode" = "ItmWhs"."ItemCode"
AND "Document"."CreateDate" <= ?
GROUP BY "Item"."ItemCode"
,"ItmWhs"."WhsCode"
,"ItmWhs"."AvgPrice"
,"ItmWhs"."OnHand"
,"ItemGrp"."ItmsGrpNam"
,"Item"."ItmsGrpCod"
,"Item"."U_StyleCode"
,"Item"."U_StyleName"
,"Item"."U_Size"
,"Item"."U_ColourCode"
,"Item"."U_ColourName"
,"Item"."U_Gender"
,"Item"."U_LCC"

To return all the results except where the balance is zero.返回除余额为零以外的所有结果。

You want a HAVING clause.你想要一个HAVING子句。 After the GROUP BY add:GROUP BY之后添加:

HAVING SUM("Document"."InQty") - SUM("Document"."OutQty") <> 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM