Найти значения, которых нет в таблице - PullRequest
1 голос
/ 11 апреля 2019

У меня есть 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|

Ответы [ 2 ]

1 голос
/ 11 апреля 2019

Интересный вопрос. Я использовал «табличную переменную»:

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', как хотите.

1 голос
/ 11 апреля 2019

Используйте 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;

Здесь - это 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 <> скрипка .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...