我有7种不同的组件,从APC,BPC,CPC,DPC,FPC,LPC,MPC开始,它们连接到ParentPart。在某些情况下,有三个组件(两种)连接到父组件,其他5组件等。从下面给出的表格中,我想找出父组件中的组件以及父组件中没有的组件。
表的代码(以帮助用户,以便他们可以轻松地将它放在小提琴或某处:
CREATE TABLE #test(
PARENTPART varchar(20),
ParentDescription varchar(15),
Component varchar(15),
Altkey varchar(20),
Qty int,
)
INSERT INTO #test
VALUES ('APF.20.015.09','Person Comp','APC2032','000123',1),
('APF.20.015.09','Person Comp','APC2038','000223',1),
('APF.20.015.09','Person Comp','CPC3042','000103',1),
('APF.20.015.09','Person Comp','DPC4032','000124',1),
('APF.20.019.09','Laptop','LPC2039','000123',1),
('APF.20.019.09','Laptop','FPC2034','0001L3',1),
('APF.20.019.09','Laptop','FPC1092','0001K3',1),
('APF.20.019.09','Laptop','CPCL032','0001M3',1);
--Below is the table generated from the above code.
PARENTPART | ParentDescr| Component| AltKey |Qty|
APF.20.015.09| Person Comp| APC2032| 000123| 1 |
APF.20.015.09| Person Comp| APC2038| 000223| 1 |
APF.20.015.09| Person Comp| CPC3042| 000103| 1 |
APF.20.015.09| Person Comp| DPC4032| 000124| 1 |
APF.20.019.09| Laptop | LPC2039| 000123| 1 |
APF.20.019.09| Laptop | FPC2034| 0001L3| 1 |
APF.20.019.09| Laptop | FPC1092| 0001K3| 1 |
APF.20.019.09| Laptop | CPCL032| 0001M3| 1 |
我在上面的表中尝试了以下查询,但它只给出了有关当前组件的结果。
SELECT *,(CASE WHEN Component LIKE 'APC%' OR Component LIKE 'BPC%' OR Component LIKE 'CPC%' OR Component LIKE 'DPC%' OR Component LIKE 'FPC%'
OR Component LIKE 'LPC%' OR Component LIKE 'MPC%' THEN 'PRESENT' ELSE NULL END) as C FROM #test;
我想要以下输出:
PARENTPART | ParentDescr| Component| AltKey |Qty |
APF.20.015.09| Person Comp| APC2032| 000123| 1 |
APF.20.015.09| Person Comp| APC2038| 000223| 1 |
APF.20.015.09| Person Comp| CPC3042| 000103| 1 |
APF.20.015.09| Person Comp| DPC4032| 000124| 1 |
APF.20.015.09| Person Comp| BPC | NULL | NULL|
APF.20.015.09| Person Comp| FPC | NULL | NULL|
APF.20.015.09| Person Comp| MPC | NULL | NULL|
APF.20.015.09| Person Comp| LPC | NULL | NULL|
APF.20.019.09| Laptop | LPC2039| 000123| 1 |
APF.20.019.09| Laptop | FPC2034| 0001L3| 1 |
APF.20.019.09| Laptop | FPC1092| 0001K3| 1 |
APF.20.019.09| Laptop | CPCL032| 0001M3| 1 |
APF.20.019.09| Laptop | APC | NULL | NULL|
APF.20.019.09| Laptop | BPC | NULL | NULL|
APF.20.019.09| Laptop | DPC | NULL | NULL|
APF.20.019.09| Laptop | MPC | NULL | NULL|
使用cross join
生成行 - 一个用于父项,一个用于部分。然后使用left join
引入数据:
select p.parentpart, p.parentdescription,
c.component, t.altkey, t.qty
from (select distinct parentpart, parentdescription
from #test
) p cross join
(select distinct component
from #test
) c left join
#test t
on t.parentpart = p.parentpart and t.component = c.component;
Here是一个db <>小提琴。
这是一个返回缩写组件名称的版本:
select p.parentpart, p.parentdescription,
coalesce(t.component, c.component_3) as component,
t.altkey, t.qty
from (select distinct parentpart, parentdescription
from test
) p cross join
(select v.component_3
from (values ('APC'), ('BPC'), ('CPC'), ('DPC'), ('FPC'), ('LPC'), ('MPC')) v(component_3)
) c left join
test t
on t.parentpart = p.parentpart and left(t.component, 3) = c.component_3
order by parentpart, parentdescription, component;
和相应的db<>fiddle。
有趣的问题。我用'表变量':
declare @table table
(ParentPart varchar(50),
ParentDescr varchar(30),
Component varchar(10))
insert into @table values ('APF.20.015.09','Person Comp','APC')
insert into @table values ('APF.20.015.09','Person Comp','BPC')
insert into @table values ('APF.20.015.09','Person Comp','CPC')
insert into @table values ('APF.20.015.09','Person Comp','DPC')
insert into @table values ('APF.20.015.09','Person Comp','FPC')
insert into @table values ('APF.20.015.09','Person Comp','LPC')
insert into @table values ('APF.20.015.09','Person Comp','MPC')
insert into @table values ('APF.20.019.09','laptop','APC')
insert into @table values ('APF.20.019.09','laptop','BPC')
insert into @table values ('APF.20.019.09','laptop','CPC')
insert into @table values ('APF.20.019.09','laptop','DPC')
insert into @table values ('APF.20.019.09','laptop','FPC')
insert into @table values ('APF.20.019.09','laptop','LPC')
insert into @table values ('APF.20.019.09','laptop','MPC')
SELECT
T2.PARENTPART,T2.ParentDescr,
CASE WHEN SUBSTRING(T1.Component,1,3) = t2.Component THEN T1.Component
ELSE T2.Component
END AS COMPONENT, T1.Altkey,T1.Qty
FROM #TEST T1
RIGHT JOIN @table T2
ON T1.PARENTPART = T2.ParentPart
AND T1.ParentDescription = T2.ParentDescr
AND SUBSTRING(T1.Component,1,3) = t2.Component
根据需要使用'order by'。