We have a vehicles table that has a VehicleName column. I’m looking into a bug where a user has multiple vehicles of the same VehicleName. I have this query to help return which VehicleNames have been used multiple times:
SELECT VehicleName,
(
SELECT count(VehicleName)
FROM Vehicles as V2
WHERE V1.VehicleName = V2.VehicleName
)
FROM Vehicles as V1;
For one thing, it’s slow. That’s not too bad because this isn’t going into production; it’s just to aid in a bug fix. Second, this will return every VehicleName, even the ones that have a count of one, those are VehicleNames I’m not interested in for this application. I can’t remember how to name the subquery, so I can’t add a where to limit it.
I’m interested not just in how to name the subquery, but also are their faster solutions to this?
Are you trying to do this in some ugly way ?