sql存储过程
分配表
Id name
1 张三
2 李四
------解决思路----------------------
沒有數據,只能說一下邏輯
如你所說,100條數據。
1)將查詢的結果標號。自增列或row_number() 作為一個新字段ID
2)分配
ID%3等於0 分配給第一個人
ID%3等於1 分配給第二個人
ID%3等於2 分配給第三個人
------解决思路----------------------
IF OBJECT_ID('test_num') IS NOT NULL
DROP TABLE test_num
GO
CREATE TABLE test_num
(
id INT IDENTITY(1,1) PRIMARY KEY,
[name] NVARCHAR(50),
groupId INT,
)
GO
INSERT INTO test_num(name,groupId) VALUES('张三1',1)
INSERT INTO test_num(name,groupId) VALUES('李四1',1)
INSERT INTO test_num(name,groupId) VALUES('王五2',2)
INSERT INTO test_num(name,groupId) VALUES('赵六2',2)
INSERT INTO test_num(name,groupId) VALUES('钱七2',2)
--SELECT * FROM test_num tn
DECLARE @num INT
SET @num=100
;WITH f AS (
SELECT RANK() OVER ( PARTITION BY groupId ORDER BY id) rankId, * FROM test_num
),
fMax AS (
SELECT *
,(SELECT MAX(rankId) FROM f AS f2 WHERE f1.groupId=f2.groupId) AS [gCount]
,(SELECT @num/MAX(rankId) FROM f AS f2 WHERE f1.groupId=f2.groupId) AS [avg]
,(SELECT @num%MAX(rankId) FROM f AS f2 WHERE f1.groupId=f2.groupId) AS [remainder]
FROM f AS f1
)
SELECT *, CASE WHEN rankId!=gCount THEN [avg] ELSE [avg]+[remainder] END AS result FROM fMax
完整代码都上了, 结贴给分吧
------解决思路----------------------
DECLARE @dCount int -- 数据条数,实际应从 data 计数。
DECLARE @uCount int -- 用户数,实际应从 users 计数。
SET @dCount = 100
SET @uCount = 3
;WITH data AS ( -- 模拟100条数据
SELECT TOP 100 name,number
FROM master..spt_values
)
,users (id,name) AS ( -- 模拟3个用户
SELECT 1,'张三' UNION ALL
SELECT 2,'李四' UNION ALL
SELECT 4,'王五'
)
,d1 AS (
SELECT *,
ROW_NUMBER() OVER(ORDER BY number) AS rn
FROM data
)
,d2 AS ( -- 序列号/33 得到 0~3 共4个分组,再除3求余(等于把3组分配给0组)
SELECT *,
(rn/(@dCount/@uCount) % @uCount)+1 AS u_rn
FROM d1
)
,u1 AS (
SELECT *,
ROW_NUMBER() OVER(ORDER BY id) AS rn
FROM users
)
SELECT u1.id,
u1.name,
d2.*
FROM u1
JOIN d2
ON d2.u_rn = u1.rn
------解决思路----------------------
IF OBJECT_ID('test_num') IS NOT NULL
DROP TABLE test_num
GO
CREATE TABLE test_num
(
id INT IDENTITY(1,1) PRIMARY KEY,
[name] NVARCHAR(50),
groupId INT,
)
GO
INSERT INTO test_num(name,groupId) VALUES('张三1',1)
INSERT INTO test_num(name,groupId) VALUES('李四1',1)
INSERT INTO test_num(name,groupId) VALUES('王五2',2)
INSERT INTO test_num(name,groupId) VALUES('赵六2',2)
INSERT INTO test_num(name,groupId) VALUES('钱七2',2)
select a.id,a.name,case when a.n<=b.count2 then b.counts+1 else b.counts end as counts
from (select *,row_number()over(partition by groupid order by id desc )as n from test_num) as a
join (select groupid,100/count(id) as counts,100%count(id) as count2 from test_num
group by groupid) as b on a.groupId=b.groupId
--结果
id name counts
----------- -------------------------------------------------- -----------
2 李四1 50
1 张三1 50
5 钱七2 34
4 赵六2 33
3 王五2 33
(5 行受影响)