0

I have the following query:

 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

However, i want to also count all e.id that are organized by the above join along with the field e."inStock" = true.

1
  • if i simply do this (select count(e.*) where e."inStock" ) as "inStock"-- it gives me the correct info but for some reason splits up the results incorrectly, meaning where there was one line items with 50,000 in sales there are now two with 25,000 Commented Feb 26, 2013 at 16:25

1 Answer 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 ....

OR

COUNT(CASE WHEN e."inStock" = true THEN 1 END) as "totalInStock"
Sign up to request clarification or add additional context in comments.

2 Comments

this has the same problem i mention in my above comment. Incorrectly splitting up the rows
sorry i had forgotten to include that bit of info in my original comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.