目前我的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
答案 0 :(得分:9)
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
答案 1 :(得分:5)
SELECT NAME,
[BASE/DAY],
CAST( ([BASE/DAY]-1) / 25) * 25 AS varchar(20)) + ' <= ' +
CAST( ([BASE/DAY]-1) / 25 + 1) * 25 AS varchar(20)) As Bucket,
FROM
(SELECT Name, ROUND([DR# BASE]/DAYS_WORKED,0) AS [BASE/DAY]
FROM MYTABLE) T
编辑:修改边界值以显示在下部存储桶中。