在我的情况下,根据分配数量,每个区域都有产品交付。
Rowstamp DocNos ProductID SiteCode SiteDesc Qty
1 AAAA 1AA 0200 Mckinley Area 2
2 AAAA 1BB 0200 Mckinley Area 3
3 AAAA 1CC 0200 Mckinley Area 1
4 BBBB 1AA 0300 Southwoods 4
5 BBBB 1BB 0300 Southwoods 2
6 BBBB 1CC 0300 Southwoods 2
但是现在每个元素都需要一行。这将用于单独标记项目框。这是我想要的输出:
Rowstamp DocNos ProductID SiteCode Description Qty
1 AAAA 1AA 0200 Mckinley Area 1
1 AAAA 1AA 0200 Mckinley Area 1
2 AAAA 1BB 0200 Mckinley Area 1
2 AAAA 1BB 0200 Mckinley Area 1
2 AAAA 1BB 0200 Mckinley Area 1
3 AAAA 1CC 0200 Mckinley Area 1
4 BBBB 1AA 0300 Southwoods 1
4 BBBB 1AA 0300 Southwoods 1
4 BBBB 1AA 0300 Southwoods 1
4 BBBB 1AA 0300 Southwoods 1
5 BBBB 1BB 0300 Southwoods 1
5 BBBB 1BB 0300 Southwoods 1
6 BBBB 1CC 0300 Southwoods 1
6 BBBB 1CC 0300 Southwoods 1
谢谢您的一贯支持!
答案 0 :(得分:4)
一种简单的方法使用递归子查询:
with cte as (
select Rowstamp, DocNos, ProductID, SiteCode, Description, qty, 1 as num
from t
union all
select Rowstamp, DocNos, ProductID, SiteCode, Description, qty, num + 1
from cte
where num <= qty
)
select Rowstamp, DocNos, ProductID, SiteCode, Description, num as qty
from cte;
答案 1 :(得分:0)
您可以加入号码表。在这里,我使用来自sysobjects
数据库的master
上的查询,因为它始终存在并且可能有很多行。如果您碰巧拥有比qty
中的行数大的sysobjects
值,则可以使用其他方法来获取数字来源。
SELECT T.RowStamp, T.DocNos, T.ProductID, T.SiteCode, T.Description, T.qty
FROM T
INNER JOIN (
SELECT ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY id) AS num
FROM master.dbo.sysobjects
) AS B
ON B.num <= T.qty
答案 2 :(得分:0)
游标创建记录:
CREATE TABLE #Tags
(
[Rowstamp] [int] NOT NULL,
[DocNos] [nvarchar](50) NULL,
[ProductID] [nvarchar](50) NULL,
[SiteCode] [nvarchar](50) NULL,
[SiteDesc] [nvarchar](50) NULL,
[Qty] [int] NULL
) ON [PRIMARY]
Declare @RowStamp Int, @DocNos nVarChar(50),@ProductID nVarChar(50), @SiteCode nVarChar(50),@SiteDesc nVarChar(50),@Qty nVarChar(50)
DECLARE record_cursor CURSOR FOR
SELECT [Rowstamp], [DocNos], [ProductID], [SiteCode], [SiteDesc], [Qty]
FROM [tbl_Tags];
OPEN record_cursor;
FETCH NEXT FROM record_cursor INTO
@RowStamp, @DocNos,@ProductID, @SiteCode, @SiteDesc, @Qty;
Declare @vRecs Int
Set @vRecs = 0
WHILE @@FETCH_STATUS = 0
BEGIN
While @vRecs < @Qty
Begin
Insert Into #Tags Values(@RowStamp, @DocNos,@ProductID, @SiteCode, @SiteDesc, 1)
Set @vRecs = @vRecs+1
End
Set @vRecs = 0
FETCH NEXT FROM record_cursor INTO @RowStamp, @DocNos,@ProductID, @SiteCode, @SiteDesc, @Qty;
END
Select * From #Tags
Drop table #Tags
CLOSE record_cursor;
DEALLOCATE record_cursor;