[英]Part Id 3900 take wrong technology id as 7 and it must Be 2 because Feature Name and Value Exist?
我在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)
我的問題完成了Part id 3900
它錯了
技術 ID 7 且正確必須為 2
因為存在特征名稱和特征值
所以它必須Take Same TechnologyId
Id存在
在表#partsfeature
上as Technology Id 2
。
正確的將如下
+--------+--------------+---------------+-------------
| PartID | FeatureName | FeatureValue | TechnologyId
+--------+--------------+---------------+-------------
| 3900 | grail | 51V | 2
+--------+--------------+---------------+-------
我嘗試的是
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)
使用NOT EXISTS()
檢查FeatureName
和FeatureValue
是否存在。 子查詢從表中獲取現有的最大 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.