目前我的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语句使Output如下所示?
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, ROUND([DR# BASE]/DAYS_WORKED,0) AS 'BASE/DAY' FROM MYTABLE
) T
ORDER BY 1, 2, 3

关于sql-server - 如何在Microsoft SQL中将数据分组到存储桶中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1296785/

10-09 02:03