3 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
select t.range as [score range], count(*) as [number of occurences]from ( select case when score between 0 and 9 then ' 0- 9' when score between 10 and 19 then '10-19' else '20-99' end as range from scores) tgroup by t.range
select t.range as [score range], count(*) as [number of occurences]from ( select user_id, case when score >= 0 and score< 10 then '0-9' when score >= 10 and score< 20 then '10-19' else '20-99' end as range from scores) tgroup by t.range

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊
另一種方法是將范圍存儲(chǔ)在表中,而不是將它們嵌入查詢中。最后你會(huì)有一張桌子,叫它恒河,看起來是這樣的:
LowerLimit UpperLimit Range
0 9 '0-9'
10 19 '10-19'
20 29 '20-29'
30 39 '30-39'
一個(gè)類似于這樣的查詢:
Select
Range as [Score Range],
Count(*) as [Number of Occurences]
from
Ranges r inner join Scores s on s.Score between r.LowerLimit and r.UpperLimit
group by Range
這確實(shí)意味著設(shè)置一個(gè)表,但是當(dāng)所需的范圍發(fā)生變化時(shí),它將很容易維護(hù)。沒有代碼更改的必要!
- 3 回答
- 0 關(guān)注
- 874 瀏覽
添加回答
舉報(bào)