首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >部件Id 3900将错误的技术id作为7,并且必须是2,因为存在功能名称和值?

部件Id 3900将错误的技术id作为7,并且必须是2,因为存在功能名称和值?
EN

Stack Overflow用户
提问于 2022-06-11 11:59:34
回答 1查看 42关注 0票数 -1

我在sql server 2017上工作,#partsfeature表已经存在,如下所示

代码语言:javascript
复制
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

#partsfeature as Technology Id 2 .

更正如下所示:

代码语言:javascript
复制
   +--------+--------------+---------------+-------------
    | PartID |  FeatureName |  FeatureValue | TechnologyId   
    +--------+--------------+---------------+-------------
    |   3900 | grail        | 51V           |   2
    +--------+--------------+---------------+-------

我尝试的是

代码语言:javascript
复制
 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)

插入部件的预期结果

EN

回答 1

Stack Overflow用户

发布于 2022-06-11 12:39:51

使用NOT EXISTS()检查FeatureNameFeatureValue是否存在。子查询,从表中获取现有的最大TechnologyId,并通过row_number()生成运行序列

代码语言:javascript
复制
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
        )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72584146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档