Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:14:57+00:00 2026-06-06T05:14:57+00:00

Having tried this query a million different ways, I can’t seem to SELECT ‘count_lonely_vehicles’

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:14:59+00:00Added an answer on June 6, 2026 at 5:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble optimizing this query: SELECT a.id FROM a JOIN b ON a.id=b.id
The problem I'm having is removing spaces. I've tried a few different ways and
I tried endlessly now you resolve this utter frustrating situation I have been having.
Forgive me if this is a tried question, but I'm having a little difficulty
Having an issue here that I have tried everything I can think of but
I have tried this query to get the duplicate records.but I am getting this
So right now I'm using this query: select * from items where mydate =
Having some trouble getting this query to work correctly. mysql_query(DELETE FROM `table` WHERE `id`
Having a problem with this query, anyone know the problem? INSERT INTO options (grouptogether)
I'm having trouble figuring out this query. I'm using PHP and postgresql 9.1. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.