使用SQL分配记录

时间:2018-12-14 05:10:07

标签: sql-server

如何正确分配每个站点的SKU。无论数量有无,所有站点都必须有SKUS。请参考下表

DocNos  SKU  SiteCode  Site         Qty
2222    AAA  001       Eastwood      1
2222    BBB  002       Holywood      2
2222    BBB  003       Baywood       1
2222    CCC  004       RiverBank     3
2222    AAA  004       RiverBank     3

在Eastwood站点上的示例中,此站点上仅分配了SKU AAA,但是我们需要在该站点上包括其他SKU(BBB,CCC),但仅分配0。

所需的输出

DocNos  SKU  SiteCode  Site         Qty
2222    AAA  001       Eastwood      1
2222    BBB  001       Eastwood      0
2222    CCC  001       Eastwood      0
2222    BBB  002       Holywood      2
2222    AAA  002       Holywood      0
2222    CCC  002       Holywood      0
2222    BBB  003       Baywood       1
2222    AAA  003       Baywood       0
2222    CCC  003       Baywood       0
2222    CCC  004       RiverBank     3
2222    AAA  004       RiverBank     3
2222    BBB  004       RiverBank     0

3 个答案:

答案 0 :(得分:0)

您可以借助sql中的row_number来完成。

以下是代码供您参考。

CREATE TABLE #recipes (
DocNos INT ,
SKU VARCHAR(30) ,
SiteCode VARCHAR(30) ,
Site VARCHAR(30) ,
Qty  INT ,
);

INSERT INTO #recipes VALUES     (2222,'AAA','001','Eastwood',1)
INSERT INTO #recipes VALUES     (2222,'BBB','002','Holywood',2)
INSERT INTO #recipes VALUES     (2222,'AAA','002','Holywood',0)
INSERT INTO #recipes VALUES     (2222,'BBB','001','Eastwood',0)

select * from #recipes

SELECT
ROW_NUMBER() OVER(PARTITION BY Site ORDER BY Qty desc)  AS rownum,
DocNos, SKU, SiteCode, Site,Qty FROM #recipes

我希望它对您有用。

谢谢:)

答案 1 :(得分:0)

您可以简单地执行此操作。创建CTE,然后交叉联接和左联接:

测试数据:

create table test(docnos int, SKU varchar(3), sitecode varchar(3), site varchar(20), qty int);
insert into test values (2222, 'AAA', '001', 'Eastwood', 1);
insert into test values (2222, 'BBB', '002', 'Holywood', 2);
insert into test values (2222, 'BBB', '003', 'Baywood', 1);
insert into test values (2222, 'CCC', '004', 'RiverBank', 3);
insert into test values (2222, 'AAA', '004', 'RiverBank', 3);

with cteUniqueSKU (SKU) as (select distinct sku from test),
cteUniqueSite(Sitecode) as (select distinct sitecode from test),
cteCounts (docnos, site, qty, sku, sitecode) as (select docnos, site, qty, sku, sitecode from test)

select c.docnos, t.SKU, g.sitecode, c.site, coalesce (c.qty, 0)
from cteUniqueSKU as t cross join cteUniqueSite as g
left join cteCounts as c on c.sitecode = g.sitecode and c.sku = t.sku

答案 2 :(得分:0)

这应该作为注释,但是由于文本内容较大,发布答案将在以后删除:

注意:这不是您想要的预期输出,但是您可以对该查询进行操作以获得预期输出,

示例查询以生成数据:

CREATE TABLE #temp (
DocNos INT ,
SKU VARCHAR(30) ,
SiteCode VARCHAR(30) ,
Site VARCHAR(30) ,
Qty  INT ,
);

INSERT INTO #temp VALUES     (2222,'AAA','001','Eastwood ',1)
INSERT INTO #temp VALUES     (2222,'BBB','002','Holywood ',2)
INSERT INTO #temp VALUES     (2222,'BBB','003','Baywood  ',1)
INSERT INTO #temp VALUES     (2222,'CCC','004','RiverBank',3)
INSERT INTO #temp VALUES     (2222,'AAA','004','RiverBank',3)

查询:

SELECT DISTINCT SKU 
INTO #ALLSKU
FROM #temp


SELECT B.DocNos,B.SKU,B.SiteCode,B.Site,B.Qty
INTO #CJ
FROM #temp B
CROSS JOIN #ALLSKU


SELECT A.* 
FROM 
#CJ A
JOIN #temp B ON B.SKU = A.SKU AND B.SiteCode = A.SiteCode

当前输出:

DocNos  SKU SiteCode    Site    Qty
2222    AAA 001       Eastwood  1
2222    BBB 002       Holywood  2
2222    BBB 003       Baywood   1
2222    CCC 004       RiverBank 3
2222    AAA 004       RiverBank 3
2222    AAA 001       Eastwood  1
2222    BBB 002       Holywood  2
2222    BBB 003       Baywood   1
2222    CCC 004       RiverBank 3
2222    AAA 004       RiverBank 3
2222    AAA 001       Eastwood  1
2222    BBB 002       Holywood  2
2222    BBB 003       Baywood   1
2222    CCC 004       RiverBank 3
2222    AAA 004       RiverBank 3