我有一张表,序列号从1到90000 所以我想知道如何自动将值分配给序列号 例如从1到1000说我希望它们落在1000桶以下 从20001年的1001到2000桶 等等多达90000条记录。
答案 0 :(得分:1)
您可以将数字除以1000,将其除以,并将其乘以1000:
SELECT 1000*FLOOR(num/1000) + 1, COUNT(*)
FROM mytable
GROUP BY FLOOR(num/1000)
答案 1 :(得分:1)
Modulo (%)运营商非常适合这样的事情...... 很容易,感觉好像在作弊。
WITH
cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)),
cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),
cte_n3 (n) AS (SELECT 1 FROM cte_n2 a CROSS JOIN cte_n2 b),
Sequense (n) AS (
SELECT TOP 90000
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM
cte_n3 a CROSS JOIN cte_n3 b
)
SELECT
SequenseNumber = s.n,
GroupNumber = s.n - (s.n % 1000)
FROM
Sequense s;
结果...
SequenseNumber GroupNumber
-------------------- --------------------
1 0
2 0
3 0
4 0
5 0
6 0
.........................
997 0
998 0
999 0
1000 1000
1001 1000
1002 1000
1003 1000
1004 1000
1005 1000
1006 1000
1007 1000
1008 1000
1009 1000
1010 1000
.........................
89990 89000
89991 89000
89992 89000
89993 89000
89994 89000
89995 89000
89996 89000
89997 89000
89998 89000
89999 89000
90000 90000
(90000 rows affected)
答案 2 :(得分:0)
下面的代码执行您想要的操作,它使用CEILING,从0到90K的算法字面意思是 num 必须 DECIMAL 你使用int将十进制数设置为0。
测试数据
declare @tbl table(num decimal)
insert into @tbl
select 1 union
select 999 union
select 1000 union
select 1001 union
select 2001 union
select 3001 union
select 9999 union
select 10000 union
select 10001 union
select 15001 union
select 25001 union
select 77006 union
select 80000 union
select 90000
查询
SELECT distinct
num,
CASE WHEN num <= 10000 THEN 1000*CEILING(num/1000)
WHEN num <= 20000 THEN 10000 + 1000*CEILING((num-10000)/1000)
WHEN num <= 30000 THEN 20000 + 1000*CEILING((num-20000)/1000)
WHEN num <= 40000 THEN 30000 + 1000*CEILING((num-30000)/1000)
WHEN num <= 50000 THEN 40000 + 1000*CEILING((num-40000)/1000)
WHEN num <= 60000 THEN 50000 + 1000*CEILING((num-50000)/1000)
WHEN num <= 70000 THEN 60000 + 1000*CEILING((num-60000)/1000)
WHEN num <= 80000 THEN 70000 + 1000*CEILING((num-70000)/1000)
WHEN num <= 90000 THEN 80000 + 1000*CEILING((num-80000)/1000)
ELSE 0
END
FROM @tbl
结果
1 1000
999 1000
1000 1000
1001 2000
2001 3000
3001 4000
9999 10000
10000 10000
10001 11000
15001 16000
25001 26000
77006 78000
80000 80000
90000 90000
答案 3 :(得分:0)
我将假设Jason发布的是OP正在寻找的东西。这是使用getnumsAB的略微变化,https://github.com/meteor/meteor/issues/9126是针对此类事物而开发的。首先,我们将使用它来创建一些示例数据:
示例数据
if object_id('tempdb..#yourdata') is not null drop table #yourdata;
select SequenceNumber = rn
into #yourdata
from dbo.GetNumsAB(1,90000,1,1);
create unique clustered index uq_cl_yourdata on #yourdata(SequenceNumber);
要了解我的解决方案,请先注意此查询:
select rn, n1, n2 from dbo.getnumsAB(0,90000,1000,1);
返回:
rn n1 n2
----- -------- --------
1 0 1000
2 1000 2000
3 2000 3000
4 3000 4000
....
87 86000 87000
88 87000 88000
89 88000 89000
90 89000 90000
<强>解决方案强>
select y.SequenceNumber, GroupNumber = n1
from #yourdata y
join dbo.getnumsAB(0,90000,1000,1) gn
on y.SequenceNumber >= n1 and y.SequenceNumber < n2;
答案 4 :(得分:0)
如果我没有遗漏某些东西你不会只是使用案例陈述吗?
选择案例 当Sequence&lt; = 1000时 那么'1000' 当Sequence&lt; = 2000且&gt; = 1001时 那么'2000'
等等达到9000?