Group by returns wrong count

Hi Experts,

I have the following 2 queries:
SELECT pickup_date, DATE_FORMAT(pickup_time, '%h:%i') AS pu_time, pickup_location AS pu_loc_id, locs.name AS pu_loc_name, driver_id, CONCAT(drivers.fName, ' ', LEFT(drivers.lName, 1), '.') AS driver_name, COUNT(res.id) AS res_count, (SUM(total_pax) + SUM(infant_qty)) AS pax, CONCAT('T', pickup_time, 'L', pickup_location, 'D', driver_id) AS group_id FROM reservations AS res INNER JOIN locations AS locs ON res.pickup_location = locs.id INNER JOIN drivers ON res.driver_id = drivers.id WHERE trip_type != 'M' /*VM Exclusion*/ AND (pickup_date = '2014-06-17') AND (pickup_time BETWEEN '00:00:00' AND '23:59:00') AND res.archived = 0 AND ( (pickup_status = 'O') OR (pickup_status = 'C') OR (pickup_status = 'X')) GROUP BY pickup_location, driver_id ORDER BY pickup_time, locs.name, drivers.fName, drivers.lName

Open in new window


SELECT user_initials, res.id, products.display_as, group_name, room, (total_pax + infant_qty) AS total_qty, adult_qty, senior_qty, student_qty, child_qty, infant_qty, deposit, trans_due AS owing, reserv_comment, CONCAT('T', pickup_time, 'L', pickup_location, 'D', driver_id) AS group_id FROM reservations AS res INNER JOIN locations AS locs ON res.pickup_location = locs.id INNER JOIN drivers ON res.driver_id = drivers.id INNER JOIN products ON res.product = products.id WHERE trip_type != 'M' /*VM Exclusion*/ AND (pickup_date = '2014-06-17') AND (pickup_time BETWEEN '00:00:00' AND '23:59:00') AND res.archived = 0 AND ( (pickup_status = 'O') OR (pickup_status = 'C') OR (pickup_status = 'X')) ORDER BY pickup_time, locs.name, drivers.fName, drivers.lName

Open in new window


As you will see I am using the exact same criteria in both queries, but res_count in SOME cases is return a different count then what I physically count in the results from the second query. I would like to stress that this is not happening for every group, but for 2 as I saw.

Can anyone see why?
APD TorontoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Hi APD. Good to see you again.

Although you're using the same criteria,  you've JOIN'd an additional table in your second query that is not in your first, namely, products.

Chances are there may be multiple products for a particular group, giving you duplicate records, which skews the res_count.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.