| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-06-17 : 08:42:39
|
William writes "Maybe I'm overlooking something, and this is really brain-dead simple. It should be.How can I, in a single SQL query (well, more if we must, I'm sure I could write it as a stored procedure if I had to, but a friend of mine and I... ah, enough backstory), get the most common element in each of several several columns that are grouped by a different column?Example:"Name", "Type", "A", "B", "C", "D"Jackson, 1, 1, 2, 3, 4Houston, 1, 1, 2, 3, 4New York, 1, 2, 3, 4, 1Oklahoma, 2, 1, 2, 3, 4Kansas, 2, 2, 1, 3, 2Oregon, 2, 2, 2, 3, 4France, 3, 1, 3, 3, 4Italy, 3, 3, 1, 2, 4 etc, etc.I would like, one type per row, the most common element in each column A, B, C, and D for each type 1, 2, 3, without, of course, knowing what types I have to begin with. Also, because of a poor design decision made in the depths of time, the type can be null. (I came up with a union that would get it if I knew the types ahead of time. But when I tried to make that into a subquery, the cross joins I was using didn't seem to pass the right data back.)SQL Server 2000, Windows 2000 SP 4." |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2005-06-20 : 12:18:43
|
| How do you want to deal with ties, with no clear "common element?" Examine mode A for type 3 in your example, do you want both values returned or neither?And since NULLs are present, should these be ignored or included in the mode evaluation? Can NULL be considered a possible mode value? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-06-20 : 16:33:53
|
Here is one ugly way:------------------------------------------------------DDL, DMLset nocount ondeclare @tb table (Name varchar(15) ,Type int ,a int ,b int ,c int ,d int)insert @tbselect 'Jackson', 1, 1, 2, 3, 4 union allselect 'Houston', 1, 1, 2, 3, 4 union all select 'New York', 1, 2, 3, 4, 1 union allselect 'Oklahoma', 2, 1, 2, 3, 4 union allselect 'Kansas', 2, 2, 1, 3, 2 union allselect 'Oregon', 2, 2, 2, 3, 4 union allselect 'France', 3, 1, 3, 3, 4 union allselect 'Italy', 3, 3, 1, 2, 4----------------------------------------------------select type ,max(elem) mostCommonElement --min,max doesn't matter because of top 1 in subqueryfrom ( select type, a elem from @tb union all select type, b from @tb union all select type, c from @tb union all select type, d from @tb ) awhere elem in (select top 1 elem from ( select type, a elem from @tb union all select type, b from @tb union all select type, c from @tb union all select type, d from @tb ) b where type = a.type group by elem order by count(*) desc --ties will return max(element) , elem desc ) group by type Be One with the OptimizerTG |
 |
|
|
|
|
|