舍入并计算出一个数字出现的次数? SQL

时间:2017-11-08 17:56:12

标签: sql sql-server

我有以下数据:

-1.25
-0.5
   0
   0
1.25
0.75
1.25
   2
   2
   2
   2

我试图巩固这些数字,同时对它们进行舍入,并使任何负数变为0.

这是我到目前为止所做的:

SELECT 
    RawScore,
    Count(*) AS Freq
FROM TestScores
GROUP BY RawScore

返回

RawScore    Freq
-1.25        1
-0.5         1
   0         2
0.75         1
1.25         1
   2         4

有没有办法解决这个问题?

RawScore    Freq
     0        4
     1        2
     2        4

2 个答案:

答案 0 :(得分:1)

你可以这样做:

select (case when rawScore < 0 then 0
             else round(rawScore, 0)
        end) as nolongerrawScore,
       count(*)
from TestScores
group by (case when rawScore < 0 then 0
               else round(rawScore, 0)
          end) 
order by nolongerrawScore;

答案 1 :(得分:0)

SELECT 
    FLOOR(ABS(RawScore)),
    Count(*) AS Freq
FROM TestScores
GROUP BY FLOOR(ABS(RawScore))