我有7种不同类型的组件,它们从 APC,BPC,CPC,DPC,FPC,LPC,MPC 开始,并且已连接到ParentPart。在某些情况下,上级零件有3种成分(两种),其他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|
答案 0 :(得分:1)
使用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。
答案 1 :(得分:1)
有趣的问题。 我使用了“表变量”:
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
根据需要使用“排序依据”。