开发者

SQL query to get field value distribution

I have a table of over 1 million test score records that basically have a unique score_ID, a subject_ID and a score given by a person. The score range for most subjects is 0-3, but some have a range of 0-4. There are about 25 possible subjects.

I need to produce a score distribution report which looks like:

subject_ID     0    1    2    3    4
----------    ---  ---  ---  ---  ---
1             967  576  856  2开发者_如何学编程34  
2             576  947  847  987  324
.
.

So it groups the data by subject_ID, then shows how many times a specific score value was given within that subject.

Any SQL pointers to generate this would be greatly appreciated.


Select subject_id
    , Sum( Case When Score = 0 Then 1 Else 0 End ) As [0]
    , Sum( Case When Score = 1 Then 1 Else 0 End ) As [1]
    , Sum( Case When Score = 2 Then 1 Else 0 End ) As [2]
    , Sum( Case When Score = 3 Then 1 Else 0 End ) As [3]
    , Sum( Case When Score = 4 Then 1 Else 0 End ) As [4]
From Table
Group By subject_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜