2013-08-29 56 views
4

我有這樣的一個表:發現大多數出現在多個列中SQL

Answer1,Answer2,Answer3,Answer4 
A,B,C,C 
B,D,D,D 
C,C,A,C 
B,B,D,D 

我想找到所有四個答案中最OCCURENCES,如果出現的次數是相同的,我需要的只是第一價值。所以理想情況下,我應該有這樣的輸出表:

Answer1,Answer2,Answer3,Answer4,MostAnswers,Occurrences 
A,B,C,C,C,2 
B,D,D,D,D,3 
C,C,A,C,C,3 
B,B,D,D,B,2 

我怎樣才能做到這一點在sql服務器?我可以通過逐行進行組羣嗎?

+0

您正在尋找每個答案的字母序列或最常見的字母? –

+0

你的表是否有主鍵,或只是Answer1-4列? – GolfWolf

+0

我可以有這個主鍵,但我認爲pivoting是必要的。 – Seen

回答

5

另一種方法,仍然假定一個Id柱的存在:

select 
    a.Id, 
    a.Answer1, 
    a.Answer2, 
    a.Answer3, 
    a.Answer4, 
    TopAnswers.* 
from AnswerTable a 
outer apply (
    select top 1 Answer, count(*) as cnt 
    from (
    select Answer1 as Answer from AnswerTable where Id = a.Id 
    union all 
    select Answer2 from AnswerTable where Id = a.Id 
    union all 
    select Answer3 from AnswerTable where Id = a.Id 
    union all 
    select Answer4 from AnswerTable where Id = a.Id 
) x 
    group by Answer 
    order by count(*) desc, Answer asc 
) TopAnswers 

這裏的SQLFiddle它:http://sqlfiddle.com/#!3/b1dfd/8

+0

謝謝。我將看看如何在sql server中使用apply。 – Seen

1

一種方法是取消數據。訣竅是你需要一個id來識別每一行。這是在以下查詢中使用row_number()獲得的。然後得到你想要的值,需要明智地使用的聚集和窗口功能:

with t as (
     select (case when n.n = 1 then answer1 
        when n.n = 2 then answer2 
        when n.n = 3 then answer3 
        when n.n = 4 then answer4 
       end) as answer, a.* 
     from (select row_number() over (order by (select NULL)) as recnum, a.* 
      from answers a 
      ) cross join 
      (select 1 as n union all select 2 union all select 3 union all select 4) n 
    ) 
select answer1, answer2, answer3, answer4, answer, cnt 
from (select recnum, answer1, answer2, answer3, answer4, answer, count(*) as cnt, 
      row_number() over (partition by recnum order by count(*) desc 
           ) as seqnum_cnt 
     from t 
     group by recnum, answer1, answer2, answer3, answer4, answer 
    ) a 
where seqnum_cnt = 1;