Monday, July 19, 2010

In sql what query would you use to determine the most popular (the column name that appears the most frequentl

In sql what query would you use to determine the most popular (the column name that appears the most frequently) column name in the tables of your database?

In sql what query would you use to determine the most popular (the column name that appears the most frequentl
I believe you're more interested in the actual column names rather than the data that appears in the database. You'll need to look at the syscolumns table. This query will show you what you're interested in:





select top 1 c.name, count(*)


from syscolumns c


join sysobjects t on t.id = c.id


join systypes y on y.xtype = c.xtype


where t.xtype = 'U'


group by c.name


order by 2 desc





and then if you want to see the tables that have that column:





select t.name


from sysobjects t


where t.xtype = 'U'


and exists ( select 1 from syscolumns where id = t.id and name = 'popular_column_name' )





(of course you'll need to replace 'popular_column_name' with the name that is returned in the first query).





Hope that helps!
Reply:If I understand correctly, something like this should do it


SELECT COL1 FROM TABLE1 GROUP BY COL1 HAVING COUNT(*) %26gt;= ALL(SELECT COUNT(*) FROM TABLE1 GROUP BY COL1);


No comments:

Post a Comment