简体   繁体   English

Part Id 3900 将错误的技术 id 设为 7,它必须为 2,因为 Feature Name 和 Value 存在?

[英]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 .在表#partsfeatureas 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)

Expected Result For Parts Inserted插入零件的预期结果之前存在错误的数据技术 ID

Use NOT EXISTS() to check for existance of FeatureName and FeatureValue .使用NOT EXISTS()检查FeatureNameFeatureValue是否存在。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM