Huck
Huck

Reputation: 517

Subselect not obeying GROUP BY

Having tried this query a million different ways, I can't seem to SELECT 'count_lonely_vehicles' properly. The 'count_lonely_vehicles' subselect currently counts the total number of photos with just one vehicle_id association, but is not GROUP BY'd the dealership.id like the other SELECTs are.

dealerships (has many vehicles)
-----------
id | name

vehicles (has many photos and belongs to dealership)
--------
id | dealership_id

photos (belongs to vehicle)
------
id | vehicle_id

SELECT DISTINCT
    dealerships.id,
    dealerships.name,
    COUNT(photos.id) AS 'count_photos',
    FLOOR(COUNT(photos.id) / COUNT(DISTINCT vehicles.id)) AS 'photos_per_vehicle',
    (
        SELECT COUNT(*)
        FROM (
            SELECT COUNT(photos.vehicle_id) AS 'count'
            FROM photos
            GROUP BY photos.vehicle_id
            HAVING COUNT(photos.vehicle_id) = 1
        ) AS tbl
    ) AS 'count_lonely_vehicles'
FROM dealerships
JOIN vehicles ON vehicles.dealership_id = dealerships.id
JOIN photos ON photos.vehicle_id = vehicles.id
GROUP BY dealerships.id
ORDER BY count_photos DESC

So when looping through the dealerships array in a template and spitting out 'count_lonely_vehicles', it's always the same number. The intended behavior is to collect the number of photo records with just one vehicle_id association per dealership.

How can I rewrite this query to accomplish this?

Edit: Answered

This is the query that finally worked after much help from the chosen answer. Also had to fix some field names that were mistaken in the chosen answer.

SELECT
    dealerships.id,
    dealerships.name,
    COUNT(photos.id) AS 'count_photos',
    FLOOR(COUNT(DISTINCT photos.id) / COUNT(DISTINCT vehicles.id)) AS 'photos_per_vehicle',
    l.num_lonely
FROM dealerships
JOIN vehicles ON vehicles.dealership_id = dealerships.id
JOIN photos ON photos.vehicle_id = vehicles.id
LEFT OUTER JOIN (
    SELECT
        v.dealership_id,
        COUNT(*) AS num_lonely
    FROM dealerships d
    JOIN vehicles v ON v.dealership_id = d.id
    JOIN (
        SELECT p.vehicle_id
        FROM photos p
        GROUP BY p.vehicle_id
        HAVING COUNT(*) = 1
    ) p ON p.vehicle_id = v.id
    GROUP BY d.id
) l ON vehicles.dealership_id = l.dealership_id

GROUP BY dealerships.id
ORDER BY count_photos DESC

Upvotes: 2

Views: 97

Answers (2)

Steven Mastandrea
Steven Mastandrea

Reputation: 2772

You need to get the main dealership ID value into your sub-query. Try this:

SELECT DISTINCT
    dealerships.id,
    dealerships.name,
    COUNT(photos.id) AS 'count_photos',
    FLOOR(COUNT(photos.id) / COUNT(DISTINCT vehicles.id)) AS 'photos_per_vehicle',
    (
        SELECT COUNT(*)
        FROM (
            SELECT COUNT(photo2.vehicle_id) AS 'count'
            FROM photos photo2
            JOIN vehicles vehicle2 on photo2.vehicle_id = vehicle2.vehicle_id 
            WHERE vehicle2.dealership_id = dealerships.dealership_id
            GROUP BY photo2.vehicle_id
            HAVING COUNT(photo2.vehicle_id) = 1
        ) AS tbl
    ) AS 'count_lonely_vehicles'
FROM dealerships
JOIN vehicles ON vehicles.dealership_id = dealerships.id
JOIN photos ON photos.vehicle_id = vehicles.id
GROUP BY dealerships.id
ORDER BY count_photos DESC

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269973

You need to join in the dealership information to get what you want. I prefer doing this by moving the subquery into the "FROM" clause:

FROM dealerships JOIN
     vehicles
     ON vehicles.dealership_id = dealerships.id JOIN
     photos ON photos.vehicle_id = vehicles.id left outer join
     (select d.dealership_id, count(*) as num_lonely
      from dealerships d JOIN
           vehicles v
           ON v.dealership_id = d.id JOIN
           (select p.vehicle_id
            from photos p
            group by p.vehicle_id
            having count(*) = 1
           ) p
           ON p.vehicle_id = v.id
     group by d.dealership_id
     ) l
     on d.dealership_id = l.dealership_id

You can also fix it by joining in the tables and doing a correlated subquery.

Upvotes: 4

Related Questions