0

我有以下查询:

 SELECT sum(p.price) as "totalPurchase", sum(s.price) as "totalSale", sum(er.price) as          
 "totalRet", v.name as "vendor"
 FROM "Esns" e
 JOIN "EsnsPurchaseOrderItems" ep on ep."EsnId" = e.id
 JOIN "PurchaseOrderItems" p on ep."PurchaseOrderItemId"= p.id
 JOIN "PurchaseOrders" po on p."PurchaseOrderId" = po.id
 JOIN "Vendors" v on po."VendorId" = v.id
 LEFT OUTER JOIN "EsnsSalesOrderItems" es on es."EsnId" = e.id
 LEFT OUTER JOIN "SalesOrderItems" s on es."SalesOrderItemId" = s.id
 LEFT OUTER JOIN "EsnsRmas" er on er."EsnId" = e.id
 GROUP BY v.id

但是,我还想计算由上述连接组织的所有 e.id 以及字段 e."inStock" = true。

4

1 回答 1

1
SELECT  sum(p.price) as "totalPurchase", 
        sum(s.price) as "totalSale", 
        sum(er.price) as "totalRet", 
        v.name as "vendor",
        sum(CASE WHEN e."inStock" = true THEN 1 ELSE 0 END) as "totalInStock"
FROM    .....
GROUP   BY ....

或者

COUNT(CASE WHEN e."inStock" = true THEN 1 END) as "totalInStock"
于 2013-02-26T16:22:12.677 回答