I have a Personal table with a LastName column and a MaybeUniqueID.
I want to put in output a table with a LastName column where the counter set on to the column MaybeUniqueID gives more than 1 row.
- I would like to do everything in one unique run, avoiding mid-step outputs.
- I prefere not using temporary table or table variables, otherwise I would like to use at most one table variable (not temporary tables), but I think this should not be necessary.
- I am using Microsoft SQL Server 2005.
I tried different scenarios with different SQL statements like HAVING or GROUP BY, but I failed to get the outcome I am looking for.
Please have a look at the following not-working summary test:
SELECT LastName
FROM Personal
JOIN
(SELECT MaybeUniqueID AS ID2,
COUNT(*) AS CNT
FROM Personal
--WHERE CNT > 1
GROUP BY MaybeUniqueID
HAVING cnt > 1
) AS MultiMaybeUniqueID
ON Personal.MaybeUniqueID = MultiMaybeUniqueID.ID2
HAVING cnt > 1should beHAVING COUNT(*) > 1.Column aliases can only be referenced in the
ORDER BYclause not theHAVINGclause.Though you could also use