sql-server – 如何将数据分组到Microsoft SQL中的存储区

前端之家收集整理的这篇文章主要介绍了sql-server – 如何将数据分组到Microsoft SQL中的存储区前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前我的sql语句如下
SELECT NAME,ROUND([DR# BASE]/DAYS_WORKED,0) AS 'BASE/DAY'
FROM MYTABLE

输出数据如下所示

NAME    BASE/DAY
James   300
Jane    310
Jim 313
John    325
Jonah   400

是否有一个sql语句使输出看起来像下面这样?

NAME    BUCKET  BASE/DAY
James   300 <= 325  300
Jane    300 <= 325  310
Jim 300 <= 325  313
John    300 <= 325  325
Johnny  325 <= 350  329
Jonah   350 <= 400  400

解决方法

SELECT NAME,CASE WHEN [BASE/DAY] <= 325 THEN '300 <= 325'
     WHEN [BASE/DAY] <= 350 THEN '325 <= 350'
     WHEN [BASE/DAY] <= 400 THEN '350 <= 400'
END AS BUCKET,[BASE/DAY]
FROM
(
    SELECT NAME,0) AS 'BASE/DAY' FROM MYTABLE
) T
ORDER BY 1,2,3

猜你在找的MsSQL相关文章