1
SELECT 
    count(A.gross_c) AS cnt, 
    SUM(A.gross_c) AS Gross_c, 
    B.store_name
FROM hr_location_c_verified A 
JOIN hr_stores B ON A.c_location = B.id
WHERE A.c_ref_fkid IS NULL
GROUP BY A.c_location

I am executing this query, the output result coming correct, but count showing wrong.

Please help me.

4
  • What are you trying to count? Commented Mar 7, 2013 at 12:15
  • Please show some sample data and what's wrong count is comming? Commented Mar 7, 2013 at 12:15
  • I am trying to count the number of rows, Gross_c and store_name coming correct but cnt output wrong Commented Mar 7, 2013 at 12:16
  • group by expression should always contain non aggregated colums of the select (here store_name, and add c_location on the select), else strange things may happen. other databases would reject your query as invalid. Commented Mar 7, 2013 at 12:56

1 Answer 1

2

It's likely because of your JOIN, producing more rows.

One way is to use the DISTINCT keyword.

SELECT 
    count(DISTINCT A.gross_c) AS cnt, 
    SUM(A.gross_c) AS Gross_c, 
    B.store_name FROM hr_location_c_verified A 
JOIN hr_stores B ON A.c_location = B.id
WHERE A.c_ref_fkid IS NULL
GROUP BY A.c_location
Sign up to request clarification or add additional context in comments.

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.