计算不同调查的投票

时间:2017-08-21 05:08:27

标签: sql sql-server sql-server-2008

我有以下方式调查包含数据和列的调查表。数据通过人们的反馈收集。每项调查最多包含20个选项。

form   Survey1  survery2  survey3 survey4 survey5 survey6  
1         16       1         2       5        11      9
2         9        6         16      5        11      9
3         8        3         2       5        11      9
4         16       1         2       5        11      9
5         16       6         4       5        11      9
6         15       1         2       5        11      9
7         16       1         2       5        11      9
8         15       1         16      5        11      9
.
.
.

等等

每项调查都有选项1至16
我需要像

这样的外出

调查1

forms      options
 4           16
 2           15
 1            8
 1            9

调查2

forms      options
 5            1
 2            6
 1            3
.
.
.
.
直到6日调查。我一次需要所有六份调查记录。我正在使用Sql server。

1 个答案:

答案 0 :(得分:0)

好吧......我不确定我是否理解你真正想要的东西,但根据你所描述的内容,我将采取刺痛措施......

(select 'survey1' as survey, survey1 as forms, count(*) as options from some_table
group by survey, survey1 order by options desc)

union all

(select 'survey2' as survey, survey2 as forms, count(*) as options from some_table
group by survey, survey2 order by options desc)

union all

(select 'survey3' as survey, survey3 as forms, count(*) as options from some_table
group by survey, survey3 order by options desc)

union all

(select 'survey4' as survey, survey4 as forms, count(*) as options from some_table
group by survey, survey4 order by options desc)

union all

(select 'survey5' as survey, survey5 as forms, count(*) as options from some_table
group by survey, survey5 order by options desc)

union all

(select 'survey6' as survey, survey6 as forms, count(*) as options from some_table
group by survey, survey6 order by options desc)

这将返回包含该选项的一组结果,以及它在特定调查的列中出现的次数。我添加了一个专栏,以便能够判断哪些结果来自哪个调查。