0

I have 2 tables

Company & products

I need to get 2 counts. One is the total count of products and the secondly count of products for sale_flg=1

This SQL does not seem to work. Tried several other ways. Not able to get the expected results

SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS, 
       (CASE WHEN B.SALEFLG =1 THEN 1 END) AS COUNT_OF_SALES
FROM COMPANY A LEFT JOIN
     PRODUCT B
     ON B.COMPANY_ID = A.COMPANY_ID
GROUP BY A.COMPANY_NAME 
2
  • SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS, (CASE WHEN B.SALEFLG =1 THEN 1 END) AS COUNT_OF_SALES FROM COMPANY A LEFT JOIN PRODUCT B ON B.COMPANY_ID=A.COMPANY_ID GROUP BY A.COMPANY_NAME Commented Apr 14, 2017 at 0:47
  • Edit your question and provide sample data and desired results. Commented Apr 14, 2017 at 0:53

4 Answers 4

1

I think you just need a sum for the case:

SELECT C.COMPANY_NAME, COUNT(P.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS, 
       SUM(CASE WHEN P.SALEFLG = 1 THEN 1 ELSE 0 END) AS COUNT_OF_SALES
FROM COMPANY C LEFT JOIN
     PRODUCT P
     ON P.COMPANY_ID = C.COMPANY_ID
GROUP BY C.COMPANY_NAME ;
Sign up to request clarification or add additional context in comments.

7 Comments

The sql works with no errors but the sum is not giving me the right result as we only have a handful of records that have sale flag =1
Is there any other option
@SQLUser . . . You need to provide sample data and desired results, as I already requested in a comment.
Company name Total_Product_Count Total_Products_for_Sale (where Sale_flg=1) XYZ 25 10 ABC 10 5
There are many possibilities which are hard to second guess without seeing table structure (including primary keys). One possibility: there could be another field which indicates that a product is discontinued and the query isn't handling that.
|
0

If you have B.SALEFLG = 1 or 0 for you may try

Sum(B.SALEFLG) AS COUNT_OF_SALES 

Or use UNION

Comments

0

If you use count then in else you should consider null because null is not consider in count aggregation and if you have B.SALEFLG =1 or 0 then use sum aggregation.

You can try below code:

SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS, 
       count(CASE WHEN B.SALEFLG =1 THEN 1 else null END) AS COUNT_OF_SALES
FROM COMPANY A LEFT JOIN
     PRODUCT B
     ON B.COMPANY_ID = A.COMPANY_ID
GROUP BY A.COMPANY_NAME

OR try this:

SELECT A.COMPANY_NAME, COUNT(B.PRODUCT_ID) AS TOTAL_COUNT_OF_PRODUCTS, 
       sum(B.SALEFLG) AS COUNT_OF_SALES
FROM COMPANY A LEFT JOIN
     PRODUCT B
     ON B.COMPANY_ID = A.COMPANY_ID
GROUP BY A.COMPANY_NAME

Comments

0

This SQL server query is working. Company Table and Product Table Company Table CompanyID is join with Product table and sales_flg add in product table .

sales_flg = 1 record display in CntSalesflg

select Comp.CompID as CompID, COUNT(Pro.ProductID) as CntProdustID, 
SUM(CASE WHEN Pro.SalesflagID = 1 THEN 1 ELSE 0 END) as CntSalesflg
from Product as Pro
inner join Company as Comp on Pro.CompID = Comp.CompID
GROUP by Comp.CompID

Comments

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.