[英]Part Id 3900 take wrong technology id as 7 and it must Be 2 because Feature Name and Value Exist?
I work on sql server 2017
I have table #partsfeature
already exist as below我在
sql server 2017
上工作我的表#partsfeature
已经存在,如下所示
create table #partsfeature
(
PartId int,
FeatureName varchar(300),
FeatureValue varchar(300),
TechnologyId int
)
insert into #partsfeature(PartId,FeatureName,FeatureValue,TechnologyId)
values
(1211,'AC','5V',1),
(2421,'grail','51V',2),
(6211,'compress','33v',3)
my issue Done For Part id 3900
it take wrong我的问题完成了
Part id 3900
它错了
Technology Id 7 and Correct Must be 2技术 ID 7 且正确必须为 2
Because Feature name and Feature Value Exist因为存在特征名称和特征值
So it Must Take Same TechnologyId
Exist所以它必须
Take Same TechnologyId
Id存在
on Table #partsfeature
as Technology Id 2
.在表
#partsfeature
上as Technology Id 2
。
correct will be as Below正确的将如下
+--------+--------------+---------------+-------------
| PartID | FeatureName | FeatureValue | TechnologyId
+--------+--------------+---------------+-------------
| 3900 | grail | 51V | 2
+--------+--------------+---------------+-------
what I try is我尝试的是
insert into #partsfeature(PartId,FeatureName,FeatureValue,TechnologyId)
select PartId,FeatureName,FeatureValue,
TechnologyId = dense_rank() over (order by FeatureName,FeatureValue)
+ (select max(TechnologyId) from #partsfeature)
from
(
values
(3900,'grail','51V',NULL),
(5442,'compress','30v',NULL),
(7791,'AC','59V',NULL),
(8321,'Angit','50V',NULL)
) s (PartId,FeatureName,FeatureValue,TechnologyId)
Use NOT EXISTS()
to check for existance of FeatureName
and FeatureValue
.使用
NOT EXISTS()
检查FeatureName
和FeatureValue
是否存在。 Sub query to get existing maximum TechnologyId from table and row_number()
to generate a running sequence子查询从表中获取现有的最大 TechnologyId 和
row_number()
以生成运行序列
insert into #partsfeature(PartId,FeatureName,FeatureValue,TechnologyId)
select PartId,FeatureName,FeatureValue,
TechnologyId = row_number() over (order by PartId)
+ (select max(TechnologyId) from #partsfeature)
from
(
values
(3900,'grail','51V',NULL),
(5442,'compress','30v',NULL),
(7791,'AC','59V',NULL),
(8321,'Angit','50V',NULL)
) s (PartId,FeatureName,FeatureValue,TechnologyId)
where not exists
(
select *
from #partsfeature x
where x.FeatureName = s.FeatureName
and x.FeatureValue = s.FeatureValue
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.