对于零件级别为 0 的同一零件,如何获取具有映射到和没有映射的零件 ID?
How to get Part Id that have map to and not have map from for same part where part level 0?
如何获取零件级别为 0 而没有贴图的零件 ID?
我使用 SQL Server 2012 并面临这样的问题,即对于零件级别为 0 的同一零件,我无法获得具有映射到和没有映射的零件。
如果零件的代码类型为 1273200,则零件级别 0 的代码类型必须为 974451。
如果零件的代码类型为 194480,则零件级别 0 的代码类型必须为 7320911。
所以我需要显示:
- 代码类型为 1273200 的部件
- 零件级别 0 且没有来自 974451 的地图
或
- 代码类型为 194480 的部件
- 零件级别 0 且没有来自 7320911 的地图
代码:
create table #codes
(
PartId int,
CodeTypeId int,
Partlevel int
)
insert into #codes (PartId, CodeTypeId, Partlevel)
values
---this is correct----
(1250, 974451, 0), ---map from
(1250, 1273200, 0), ---map to
(1250, 7320911, 0), ---map from
(1250, 194480, 0), --map to
------------------
--where map from 974451 for part id 1900 for partlevel 0
(1900, 1273200, 0),---map to
(1900, 7320911, 0),---map from
(1900, 194480, 0),--map to
------------------
(2200, 974451, 0),---map from
(2200, 1273200, 0),---map to
--where map from 7320911 for part id 2200 for partlevel 0
(2200, 194480, 0),--map to
-----------------
(3400, 974451, 1), --where map from 974451 for part id 3400 for partlevel 0 so if 1 it is wrong
(3400, 1273200, 0), ---map to
(3400, 7320911, 0), ---map from
(3400, 194480, 0), --map to
-----------------
--where map from 974451 for part id 3900 for partlevel 0 so if 1 then it is not exist
(3900, 1273200, 0), ---map to
(3900, 1997801, 0),
(3900, 7320911, 0), ---map from
(3900, 194480, 0), --map to
---------------
(5020, 974451, 1),
(5020, 1997801, 1),
(5020, 7320911, 1), --where map from 7320911 for part id 5020 for partlevel 0 if 1 then it is not exist
(5020, 194480, 0), --map to
------------------
---map from 974451 not exist for part id 7050 but not care because I need only parts have partlevel 0
(7050,1273200,1), ---map to
(7050,7320911,1), ---map from
(7050,194480,1), --map to
-----------------
---map from 7320911 not exist for part id 8900 for partlevel 0 if part level 1 then not exist
(8900,7320911,1), ---map from
(8900,194480,0), --map to
-----------------
---map from 7320911 not exist for part id 9200 for partlevel 0
(9200,194480,0) --map to
-----------------
我尝试的是
select partid,codetypeid from #codes
where partlevel=0 and codetypeid=974451 and codetypeid <> 1273200
group by partid,codetypeid
union all
select partid,codetypeid from #codes
where partlevel=0 and codetypeid=194480 and codetypeid <> 7320911
group by partid,codetypeid
正确的结果必须如下:
解决方案
- Select 行(
c1
)在第 0 级...
c1.PartLevel = 0
- ... 并筛选
to
行。
c1.CodeTypeId in (194480, 1273200)
- 对应行(
c2
)...
where c2.PartId = c1.PartId and c2.PartLevel = c1.PartLevel
- ...必须丢失...
where not exists (...)
- ...链接到固定的
from
/to
对。
and (c1.CodeTypeId <> 1273200 or c2.CodeTypeId = 974451)
and (c1.CodeTypeId <> 194480 or c2.CodeTypeId = 7320911)
第 5 步在伪代码中转换为以下内容:
if (c1.CodeTypeId = 1273200)
{
c2.CodeTypeId = 974451;
}
if (c1.CodeTypeId = 194480)
{
c2.CodeTypeId = 7320911;
}
结合一切:
select c1.PartId,
c1.CodeTypeId,
c1.PartLevel
from codes c1
where c1.PartLevel = 0
and c1.CodeTypeId in (194480, 1273200) -- "to"
and not exists ( select 'x'
from codes c2
where c2.PartId = c1.PartId
and c2.PartLevel = c1.PartLevel
and (c1.CodeTypeId <> 1273200 or c2.CodeTypeId = 974451) -- "from" v1
and (c1.CodeTypeId <> 194480 or c2.CodeTypeId = 7320911) ); -- "from" v2
结果
PartId CodeTypeId PartLevel
------ ---------- ---------
1900 1273200 0
2200 194480 0
3400 1273200 0
3900 1273200 0
5020 194480 0
8900 194480 0
9200 194480 0
Fiddle 查看所有内容。
如何获取零件级别为 0 而没有贴图的零件 ID?
我使用 SQL Server 2012 并面临这样的问题,即对于零件级别为 0 的同一零件,我无法获得具有映射到和没有映射的零件。
如果零件的代码类型为 1273200,则零件级别 0 的代码类型必须为 974451。
如果零件的代码类型为 194480,则零件级别 0 的代码类型必须为 7320911。
所以我需要显示:
- 代码类型为 1273200 的部件
- 零件级别 0 且没有来自 974451 的地图
或
- 代码类型为 194480 的部件
- 零件级别 0 且没有来自 7320911 的地图
代码:
create table #codes
(
PartId int,
CodeTypeId int,
Partlevel int
)
insert into #codes (PartId, CodeTypeId, Partlevel)
values
---this is correct----
(1250, 974451, 0), ---map from
(1250, 1273200, 0), ---map to
(1250, 7320911, 0), ---map from
(1250, 194480, 0), --map to
------------------
--where map from 974451 for part id 1900 for partlevel 0
(1900, 1273200, 0),---map to
(1900, 7320911, 0),---map from
(1900, 194480, 0),--map to
------------------
(2200, 974451, 0),---map from
(2200, 1273200, 0),---map to
--where map from 7320911 for part id 2200 for partlevel 0
(2200, 194480, 0),--map to
-----------------
(3400, 974451, 1), --where map from 974451 for part id 3400 for partlevel 0 so if 1 it is wrong
(3400, 1273200, 0), ---map to
(3400, 7320911, 0), ---map from
(3400, 194480, 0), --map to
-----------------
--where map from 974451 for part id 3900 for partlevel 0 so if 1 then it is not exist
(3900, 1273200, 0), ---map to
(3900, 1997801, 0),
(3900, 7320911, 0), ---map from
(3900, 194480, 0), --map to
---------------
(5020, 974451, 1),
(5020, 1997801, 1),
(5020, 7320911, 1), --where map from 7320911 for part id 5020 for partlevel 0 if 1 then it is not exist
(5020, 194480, 0), --map to
------------------
---map from 974451 not exist for part id 7050 but not care because I need only parts have partlevel 0
(7050,1273200,1), ---map to
(7050,7320911,1), ---map from
(7050,194480,1), --map to
-----------------
---map from 7320911 not exist for part id 8900 for partlevel 0 if part level 1 then not exist
(8900,7320911,1), ---map from
(8900,194480,0), --map to
-----------------
---map from 7320911 not exist for part id 9200 for partlevel 0
(9200,194480,0) --map to
-----------------
我尝试的是
select partid,codetypeid from #codes
where partlevel=0 and codetypeid=974451 and codetypeid <> 1273200
group by partid,codetypeid
union all
select partid,codetypeid from #codes
where partlevel=0 and codetypeid=194480 and codetypeid <> 7320911
group by partid,codetypeid
正确的结果必须如下:
解决方案
- Select 行(
c1
)在第 0 级...
c1.PartLevel = 0
- ... 并筛选
to
行。
c1.CodeTypeId in (194480, 1273200)
- 对应行(
c2
)...
where c2.PartId = c1.PartId and c2.PartLevel = c1.PartLevel
- ...必须丢失...
where not exists (...)
- ...链接到固定的
from
/to
对。
and (c1.CodeTypeId <> 1273200 or c2.CodeTypeId = 974451)
and (c1.CodeTypeId <> 194480 or c2.CodeTypeId = 7320911)
第 5 步在伪代码中转换为以下内容:
if (c1.CodeTypeId = 1273200)
{
c2.CodeTypeId = 974451;
}
if (c1.CodeTypeId = 194480)
{
c2.CodeTypeId = 7320911;
}
结合一切:
select c1.PartId,
c1.CodeTypeId,
c1.PartLevel
from codes c1
where c1.PartLevel = 0
and c1.CodeTypeId in (194480, 1273200) -- "to"
and not exists ( select 'x'
from codes c2
where c2.PartId = c1.PartId
and c2.PartLevel = c1.PartLevel
and (c1.CodeTypeId <> 1273200 or c2.CodeTypeId = 974451) -- "from" v1
and (c1.CodeTypeId <> 194480 or c2.CodeTypeId = 7320911) ); -- "from" v2
结果
PartId CodeTypeId PartLevel
------ ---------- ---------
1900 1273200 0
2200 194480 0
3400 1273200 0
3900 1273200 0
5020 194480 0
8900 194480 0
9200 194480 0
Fiddle 查看所有内容。