簡體   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?

我在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存在

在表#partsfeatureas 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)

插入零件的預期結果之前存在錯誤的數據技術 ID

使用NOT EXISTS()檢查FeatureNameFeatureValue是否存在。 子查詢從表中獲取現有的最大 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