简体   繁体   中英

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.

I've using Where "balance" <> 0 and variations this, but keep getting error aggregation cannot be used in where clause. 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. After the GROUP BY add:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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