2017-10-10 73 views
0

我需要一個SQL服務器查詢的幫助:在SQL服務器中的分配

我有一個分配給它們的訂單ID和錯誤的表。

enter image description here

我需要分配這些訂單給團隊成員以平等的方式。

enter image description here

有類似以下

enter image description here

enter image description here

我已經能夠使用由錯誤劃分的NTILE結果,但是這將拆分的訂單團隊成員,但不平等。

預先感謝您。

+0

使用NTILE(countOfTeamMembers) –

+0

是否有你正在尋找做任何特別的方式?例如,它可以用多個離散的語句來完成。你在找什麼? – STLDeveloper

+0

太多的示例數據... – jarlh

回答

1

爲什麼你不應該僅僅因爲他們進來分發錯誤。

; with octe as 
(
select 
    [order #], 
    error, 
    row_number() over(order by error asc) r 
from tblorders 
), 
tcte as 
(
select 
    teammember, 
    row_number() over(order by teammember asc) r 
from tblTeam 
), 
distribution as 
(
select 
    teammember, 
    [order #], 
    error 
from 
octe o join tcte t 
on o.r%(select count(1) from tcte)+1=t.r 
) 

select * from distribution; 
0

你可以試試NTILE功能。

CREATE TABLE [dbo].[orders](
    [OrderID] [int] IDENTITY(1,1) NOT NULL, 
    [ErrorName] [varchar](50) NULL 
) ON [PRIMARY] 
GO 
/****** Object: Table [dbo].[TeamMembers] Script Date: 10/11/2017 12:53:51 AM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[teamMembers](
    [Memberid] [int] IDENTITY(1,1) NOT NULL, 
    [MemberName] [varchar](50) NULL 
) ON [PRIMARY] 
GO 
SET IDENTITY_INSERT [dbo].[Orders] ON 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (1, N'a') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (2, N'a') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (3, N'a') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (4, N'a') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (5, N'a') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (6, N'b') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (7, N'b') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (8, N'b') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (9, N'b') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (10, N'b') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (11, N'c') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (12, N'c') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (13, N'c') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (14, N'c') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (15, N'c') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (16, N'd') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (17, N'd') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (18, N'd') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (19, N'd') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (20, N'd') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (21, N'e') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (22, N'e') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (23, N'e') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (24, N'e') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (25, N'e') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (26, N'e') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (27, N'f') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (28, N'f') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (29, N'g') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (30, N'g') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (31, N'g') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (32, N'h') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (33, N'i') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (34, N'i') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (35, N'i') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (36, N'i') 
GO 
INSERT [dbo].[Orders] ([OrderID], [ErrorName]) VALUES (37, N'i') 
GO 
SET IDENTITY_INSERT [dbo].[Orders] OFF 
GO 
SET IDENTITY_INSERT [dbo].[TeamMembers] ON 
GO 
INSERT [dbo].[TeamMembers] ([Memberid], [MemberName]) VALUES (1, N'M1') 
GO 
INSERT [dbo].[TeamMembers] ([Memberid], [MemberName]) VALUES (2, N'M2') 
GO 
INSERT [dbo].[TeamMembers] ([Memberid], [MemberName]) VALUES (3, N'M3') 
GO 
INSERT [dbo].[TeamMembers] ([Memberid], [MemberName]) VALUES (4, N'M4') 
GO 
INSERT [dbo].[TeamMembers] ([Memberid], [MemberName]) VALUES (5, N'M5') 
GO 
INSERT [dbo].[TeamMembers] ([Memberid], [MemberName]) VALUES (6, N'M6') 
GO 
SET IDENTITY_INSERT [dbo].[TeamMembers] OFF 
GO 
;WITH cte AS (
SELECT NTILE((SELECT COUNT(*) FROM TeamMembers)) OVER(ORDER BY orderid) as GroupNum, * 
FROM Orders 
) 

SELECT MemberName, cte.OrderID, cte.ErrorName 
FROM cte JOIN dbo.teamMembers ON cte.GroupNum = Memberid 
0

這爲從1開始的每個訂單分配一個數字並且與成員相同。然後使用加入成員編號的成員數量的順序編號的mod來儘可能均等地分配它們。

編輯:如果您通過NEWID()而不是OrderId和MemberId進行排序,那麼排序會更隨機,但仍然是偶數,並且甚至在沒有偶數時,誰得到的變化最大。

DECLARE @orders table (OrderId int NOT NULL PRIMARY KEY CLUSTERED, Error char(1) NOT NULL); 
INSERT INTO @orders (OrderId, Error) 
VALUES 
    (1 , 'a'), 
    (2 , 'a'), 
    (3 , 'b'), 
    (4 , 'e'), 
    (5 , 'e'), 
    (6 , 'a'), 
    (7 , 'b'), 
    (8 , 'e'), 
    (9 , 'd'), 
    (10 , 'd'), 
    (11 , 'a'), 
    (12 , 'e'), 
    (13 , 'e'), 
    (14 , 'c'), 
    (15 , 'i'), 
    (16 , 'b'), 
    (17 , 'f'), 
    (18 , 'g'), 
    (19 , 'c'), 
    (20 , 'b'), 
    (21 , 'f'), 
    (22 , 'g'), 
    (23 , 'c'), 
    (24 , 'i'), 
    (25 , 'g'), 
    (26 , 'h'), 
    (27 , 'i'), 
    (28 , 'h'), 
    (29 , 'd'), 
    (30 , 'c'), 
    (31 , 'i') 
; 

DECLARE @members table (MemberId int NOT NULL PRIMARY KEY CLUSTERED, MemberName varchar(50) NOT NULL); 
INSERT INTO @members (MemberId, MemberName) 
VALUES 
    (1, 'John Smith'), 
    (2, 'Ted Rogers'), 
    (3, 'Amy Williams'), 
    (4, 'Natasha James'), 
    (5, 'Ahmed Benali'), 
    (6, 'Darshit Khatri') 
; 

DECLARE @memberCount int = ( 
    SELECT COUNT(*) 
    FROM @members 
    ); 

WITH 
Orders AS 
(
    SELECT OrderId, Error, Number = ROW_NUMBER() OVER (ORDER BY OrderId) 
    FROM @orders 
), 
Members AS 
(
    SELECT MemberId, MemberName, Number = ROW_NUMBER() OVER (ORDER BY MemberId) - 1 
    FROM @members 
) 

SELECT M.MemberName, O.Error 
FROM Orders O 
JOIN Members M ON O.Number % @memberCount = M.Number 
; 
0
SELECT o.Order#, 
     o.Error, 
     s.StaffName 
FROM (
      SELECT NTILE((SELECT COUNT(*) FROM Staff)) OVER(ORDER BY Order#) as n, 
        * 
      FROM OrderError 
     ) o 
     JOIN (
       SELECT StaffName, 
         ROW_NUMBER() OVER (ORDER BY StaffName) as MemberID 
       FROM Staff 
      ) s 
      ON o.n = s.MemberID