August 14, 2003 at 6:39 am
Similar to my previous post (Replace all NULLS with something), is there any way to do a GROUP BY *, instead of list all the column names?
E.G.
SELECT *
FROM table
GROUP BY *
HAVING COUNT(name) > 1
Any help is always appreciated.
Thanks,
Ryan
August 14, 2003 at 6:45 am
So, in fact you are looking for duplicates.
You can use the CHECKSUM(*) function for that.
SELECT *
FROM (SELECT *, CHECKSUM(*) AS cs FROM table)
GROUP BY cs
HAVING COUNT(*) > 1
Don't know about performance of this though.
Edited by - NPeeters on 08/14/2003 06:51:40 AM
August 14, 2003 at 7:20 am
Thanks for your help, NPeeters.
I tried what you suggested, but keep getting an error saying Incorrect syntax near the keyword 'GROUP'.
I have tried changing things around, but still get errors.
August 14, 2003 at 8:27 am
Just add ' As a ' between the closing bracket and the 'group by'
SELECT *
FROM (
SELECT *, CHECKSUM(*) AS cs
FROM table
)as a
GROUP BY csHAVING COUNT(*) > 1
August 14, 2003 at 8:54 am
Thanks for your help AnzioBake.
I tried your suggestion, but I get an error stating all the columns are not contained in a GROUP BY clause.
August 14, 2003 at 9:19 am
OK. Stupid me... All values in the select clause have to be contained either in an Aggregate or a group by.
So change the query to..
SELECT T.*
FROM (SELECT *, CHECKSUM(*) cs FROM table) T
INNER JOIN
(SELECT CHECKSUM(*) AS cs
FROM table
GROUP BY cs
HAVING COUNT(*) > 1) a
ON T.cs = a.cs
This should return all data you need.
August 15, 2003 at 4:34 am
Nice generic statement, just replace the table name.
August 15, 2003 at 1:38 pm
I hate those SELECT * statements. I will slap my collegues around the head for using it
Well, anyway, luckily I don't think anyone is subscribed to SQLServercentral . They should though
August 17, 2003 at 3:25 am
A great fan of Joe Celko smarties.
Won't use "great solution" in production.
Agree the "Select *" will take few DBA years to be weeded out.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply