基于分销商代码生成订单号
Order Number Generation Based On Distributor Code
我需要根据分销商代码在以下情况下生成订单号。订单号格式如下
Order Number: Distributor Code/Retailer Code/TO/DDMMYYOrder
number(Order number will be 0001,0002,...)
Scenario 1 Order Uploaded Console time: 11AM (08-Jan'22)
Retailer 1 22520/1400002/TO/0801220001
Retailer 2 22520/1400003/TO/0801220002
Retailer 3 22520/1400004/TO/0801220003
Retailer 4 22520/1400005/TO/0801220004
Retailer 5 22520/1400006/TO/0801220005
Retailer 6 22520/1400007/TO/0801220006
Same Distributor different retailers. Order number carries forward Distributor wise and not retailer wise.
和
Scenario 2 Order Uploaded Console time 2PM (08-Jan'22)
Retailer 1 22520/1400002/TO/0801220007
Retailer 2 22520/1400003/TO/0801220008
Retailer 7 22520/1400011/TO/0801220009
Retailer 8 22520/1400012/TO/0801220010
Same Distributor different retailers and same retailers. For same retailers for the "same product" same or unique quantity gets uploaded.
和
Scenario 3 Order Uploaded Console time 10AM (14-Jan'22)
Retailer 4 22520/1400005/TO/1401220011
Retailer 5 22520/1400006/TO/1401220012
Retailer 6 22520/1400007/TO/1401220013
Retailer 8 22520/1400012/TO/1401220014
Same Distributor different day order was uploaded. Please note the series
我已经使用以下 SP
将一些数据从 EXCEL 上传到数据库
CREATE PROC [dbo].[Proc_TeleOrderUpload]
(
@ORDER_UPLOAD NTEXT = NULL,
@UserId INT = 0
)
AS
BEGIN
DECLARE @idoc AS INT
Create Table #Tbl_TeleOrderUpload
(
DistCode NVARCHAR(50),
CompRtrCode NVARCHAR(50),
ProductCode NVARCHAR(50),
QtyInPieces Varchar(10),
errStatus INT
)
EXEC sp_xml_preparedocument @idoc OUTPUT, @ORDER_UPLOAD
INSERT INTO #Tbl_TeleOrderUpload
SELECT
DistCode,
CompRtrCode,
ProductCode,
QtyInPieces,
0 AS errStatus
FROM OPENXML (@idoc, '/TeleOrderUpload/Record',2)
WITH
(
DistCode NVARCHAR(50),
CompRtrCode NVARCHAR(50),
ProductCode NVARCHAR(50),
QtyInPieces Varchar(10)
)
EXEC sp_xml_removedocument @idoc
INSERT INTO Tbl_TeleOrderUpload
(
DistCode,
CompRtrCode,
ProductCode,
QtyInPieces,
OrderTaken,
OrderNumber,
UploadFlag,
CreatedId,
CreatedDate
)
SELECT DISTINCT
DistCode,
CompRtrCode,
ProductCode,
CAST(QtyInPieces AS int),
'Console',
'',
'Y',
@UserId,
GETDATE()
FROM
#Tbl_TeleOrderUpload
SELECT 'DATA UPLOADED SUCCESSFULLY...'
END
在这里,我需要插入上面的订单号。这里游标不应该用来更新订单号。我需要实现以下逻辑
DECLARE @number AS INT = 0
SET @number = (SELECT MAX(RIGHT(OrderNumber,6)) FROM Tbl_TeleOrderUpload WHERE DistCode=@DistCode)
IF @number IS NULL
SET @number=1
ELSE
SET @number =@number+1
SELECT RIGHT('000000' + CAST(@number AS VARCHAR(6)) , 6)
示例数据如下:
CREATE TABLE [dbo].[Tbl_TeleOrderUpload](
[DistCode] [nvarchar](50) NULL,
[CompRtrCode] [nvarchar](50) NULL,
[ProductCode] [nvarchar](50) NULL,
[QtyInPieces] [int] NULL,
[OrderTaken] [varchar](10) NULL,
[OrderNumber] [varchar](100) NULL,
[UploadFlag] [varchar](10) NULL,
[CreatedId] [int] NULL,
[CreatedDate] [datetime] NULL
)
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',1,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',2,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',3,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',4,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',5,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','15001600220','GNOTNA06',1,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','150011600220','GNOTNA06',2,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','15001600220','GNOTNA06',3,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','15001600220','GNOTNA06',4,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','150011600220','GNOTNA06',5,'Console','','Y',1,GETDATE())
请指导我实现这个。
我的 SQL 服务器版本是
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) Standard
Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1)
(Hypervisor)
提前致谢
看起来 ROW_NUMBER()
在 SQL 2008 年工作,所以这可能就是您想要的:
SELECT *, RIGHT('000000' + CAST(@MyOrderNumber AS VARCHAR(6)) , 6)
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DistCode ORDER BY CompRtrCode) As MyOrderNumber
FROM Table
) ST
我相信您的要求远不止于此。
这将从静态数据集中生成一些订单号。如果愿意,您可以使用它来更新 table。
但它并没有考虑到新记录的出现。而且它绝对不考虑新记录何时出现在现有记录
中间
这就是为什么像这样人工生成的特殊 ID 不是个好主意。
如果您希望能够基于现有记录并从那里增加订单号,解决方案就更复杂了。
SELECT ST.*, RIGHT('000000' + CAST(ST.MyOrderNumber + ISNULL(CN.HighestOrderNo) AS VARCHAR(6)) , 6)
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DistCode ORDER BY CompRtrCode) As MyOrderNumber
FROM Table
) ST
LEFT JOIN
(
SELECT DistCode, MAX(ExistingOrderNo) HighestOrderNo
GROUP BY DistCode
FROM Table
) CN
ON CN.DistCode = ST.DistCode
但我要重申:
根据这样的特殊规则生成密钥是个坏主意,因为随着不同的记录到达,您最终会出现间隙和不一致的数字。
具体这个案例:
Retailer 4 22520/1400005/TO/1401220011
Retailer 5 22520/1400006/TO/1401220012
Retailer 6 22520/1400007/TO/1401220013
Retailer 8 22520/1400012/TO/1401220014
现在您导入下一批当天到达的数据,它包含这条记录:
Retailer 6 22520/1400008/TO/140122
它应该有什么订单号? 1400008在140007之后,所以应该是000014,但是已经被占用了
我需要根据分销商代码在以下情况下生成订单号。订单号格式如下
Order Number: Distributor Code/Retailer Code/TO/DDMMYYOrder number(Order number will be 0001,0002,...)
Scenario 1 Order Uploaded Console time: 11AM (08-Jan'22)
Retailer 1 22520/1400002/TO/0801220001
Retailer 2 22520/1400003/TO/0801220002
Retailer 3 22520/1400004/TO/0801220003
Retailer 4 22520/1400005/TO/0801220004
Retailer 5 22520/1400006/TO/0801220005
Retailer 6 22520/1400007/TO/0801220006
Same Distributor different retailers. Order number carries forward Distributor wise and not retailer wise.
和
Scenario 2 Order Uploaded Console time 2PM (08-Jan'22)
Retailer 1 22520/1400002/TO/0801220007
Retailer 2 22520/1400003/TO/0801220008
Retailer 7 22520/1400011/TO/0801220009
Retailer 8 22520/1400012/TO/0801220010
Same Distributor different retailers and same retailers. For same retailers for the "same product" same or unique quantity gets uploaded.
和
Scenario 3 Order Uploaded Console time 10AM (14-Jan'22)
Retailer 4 22520/1400005/TO/1401220011
Retailer 5 22520/1400006/TO/1401220012
Retailer 6 22520/1400007/TO/1401220013
Retailer 8 22520/1400012/TO/1401220014
Same Distributor different day order was uploaded. Please note the series
我已经使用以下 SP
将一些数据从 EXCEL 上传到数据库CREATE PROC [dbo].[Proc_TeleOrderUpload]
(
@ORDER_UPLOAD NTEXT = NULL,
@UserId INT = 0
)
AS
BEGIN
DECLARE @idoc AS INT
Create Table #Tbl_TeleOrderUpload
(
DistCode NVARCHAR(50),
CompRtrCode NVARCHAR(50),
ProductCode NVARCHAR(50),
QtyInPieces Varchar(10),
errStatus INT
)
EXEC sp_xml_preparedocument @idoc OUTPUT, @ORDER_UPLOAD
INSERT INTO #Tbl_TeleOrderUpload
SELECT
DistCode,
CompRtrCode,
ProductCode,
QtyInPieces,
0 AS errStatus
FROM OPENXML (@idoc, '/TeleOrderUpload/Record',2)
WITH
(
DistCode NVARCHAR(50),
CompRtrCode NVARCHAR(50),
ProductCode NVARCHAR(50),
QtyInPieces Varchar(10)
)
EXEC sp_xml_removedocument @idoc
INSERT INTO Tbl_TeleOrderUpload
(
DistCode,
CompRtrCode,
ProductCode,
QtyInPieces,
OrderTaken,
OrderNumber,
UploadFlag,
CreatedId,
CreatedDate
)
SELECT DISTINCT
DistCode,
CompRtrCode,
ProductCode,
CAST(QtyInPieces AS int),
'Console',
'',
'Y',
@UserId,
GETDATE()
FROM
#Tbl_TeleOrderUpload
SELECT 'DATA UPLOADED SUCCESSFULLY...'
END
在这里,我需要插入上面的订单号。这里游标不应该用来更新订单号。我需要实现以下逻辑
DECLARE @number AS INT = 0
SET @number = (SELECT MAX(RIGHT(OrderNumber,6)) FROM Tbl_TeleOrderUpload WHERE DistCode=@DistCode)
IF @number IS NULL
SET @number=1
ELSE
SET @number =@number+1
SELECT RIGHT('000000' + CAST(@number AS VARCHAR(6)) , 6)
示例数据如下:
CREATE TABLE [dbo].[Tbl_TeleOrderUpload](
[DistCode] [nvarchar](50) NULL,
[CompRtrCode] [nvarchar](50) NULL,
[ProductCode] [nvarchar](50) NULL,
[QtyInPieces] [int] NULL,
[OrderTaken] [varchar](10) NULL,
[OrderNumber] [varchar](100) NULL,
[UploadFlag] [varchar](10) NULL,
[CreatedId] [int] NULL,
[CreatedDate] [datetime] NULL
)
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',1,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',2,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',3,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',4,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000014811','148111600220','GNOTNA06',5,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','15001600220','GNOTNA06',1,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','150011600220','GNOTNA06',2,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','15001600220','GNOTNA06',3,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','15001600220','GNOTNA06',4,'Console','','Y',1,GETDATE())
INSERT INTO Tbl_TeleOrderUpload VALUES ('0000015000','150011600220','GNOTNA06',5,'Console','','Y',1,GETDATE())
请指导我实现这个。
我的 SQL 服务器版本是
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor)
提前致谢
看起来 ROW_NUMBER()
在 SQL 2008 年工作,所以这可能就是您想要的:
SELECT *, RIGHT('000000' + CAST(@MyOrderNumber AS VARCHAR(6)) , 6)
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DistCode ORDER BY CompRtrCode) As MyOrderNumber
FROM Table
) ST
我相信您的要求远不止于此。
这将从静态数据集中生成一些订单号。如果愿意,您可以使用它来更新 table。
但它并没有考虑到新记录的出现。而且它绝对不考虑新记录何时出现在现有记录
中间这就是为什么像这样人工生成的特殊 ID 不是个好主意。
如果您希望能够基于现有记录并从那里增加订单号,解决方案就更复杂了。
SELECT ST.*, RIGHT('000000' + CAST(ST.MyOrderNumber + ISNULL(CN.HighestOrderNo) AS VARCHAR(6)) , 6)
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY DistCode ORDER BY CompRtrCode) As MyOrderNumber
FROM Table
) ST
LEFT JOIN
(
SELECT DistCode, MAX(ExistingOrderNo) HighestOrderNo
GROUP BY DistCode
FROM Table
) CN
ON CN.DistCode = ST.DistCode
但我要重申:
根据这样的特殊规则生成密钥是个坏主意,因为随着不同的记录到达,您最终会出现间隙和不一致的数字。
具体这个案例:
Retailer 4 22520/1400005/TO/1401220011
Retailer 5 22520/1400006/TO/1401220012
Retailer 6 22520/1400007/TO/1401220013
Retailer 8 22520/1400012/TO/1401220014
现在您导入下一批当天到达的数据,它包含这条记录:
Retailer 6 22520/1400008/TO/140122
它应该有什么订单号? 1400008在140007之后,所以应该是000014,但是已经被占用了