有一个要写入数据的表IndustryScore结构:
IndustryCode nvarchar
CapableId int
KeyId int
MaxScore float
MinScore float
AvaScore float
其中前三个是主键,
通过
SELECT vocationCode, CapId, KeyId,MAX(Data) AS maxS, MIN(Data) AS minS, AVG(Data) AS avgS      
FROM CompanyKeyData
GROUP BY CapId, KeyId, vocationCode
检索出的结果要写入IndustryScore,如果IndustryCode,CapableId, KeyId三个值数据库中已经有相同的了,则对相应的值进行修改,如果
没有,则执行insert操作,如何用一条语句来写成,谢谢,在线等

解决方案 »

  1.   

    Create Proc dbo.P_WriteIndustryScoreas set nocount onInsert Into IndustryScore
    SELECT vocationCode, CapId, KeyId,MAX(Data) AS maxS, MIN(Data) AS minS, AVG(Data) AS avgS
    FROM CompanyKeyData as pqh
    Where not Exists(select 1 from IndustryScore  as pqs
    where pqh.IndustryCode=pqs.vocationCode
    and  pqs.CapableId=pqh.CapId and pqs.KeyId=pqh.KeyId)
    GROUP BY pqh.CapId,pqh.KeyId,pqh.vocationCode
    update IndustryScore
    set MaxScore=MAX(Data),MinScore=MIN(Data),AvaScore=AVG(Data)
    FROM CompanyKeyData 
    where 
    CompanyKeyData.IndustryCode=IndustryScore.vocationCode
    and  IndustryScore.CapableId=CompanyKeyData.CapId
     and CompanyKeyData.KeyId=IndustryScore.KeyId)
    GROUP BY CompanyKeyData.CapId, CompanyKeyData.KeyId, CompanyKeyData.vocationCode
    set nocount off
    go
      

  2.   

    楼上的,提示说聚合不能出现在update中
      

  3.   

    1、insertinsert into IndustryScore
    select * from

      SELECT vocationCode, CapId, KeyId,MAX(Data) AS maxS, MIN(Data) AS minS, AVG(Data) AS avgS      
      FROM CompanyKeyData
      GROUP BY CapId, KeyId, vocationCode
    ) t
    where cast(vocationCode as varchar) + cast(CapId as varchar) + cast(KeyId as varchar) not in 
    (select cast(IndustryCode as varchar) + cast(CapableId as varchar) + cast(KeyId as varchar) from IndustryScore) 2、update
    update IndustryScore
    set MaxScore = t.maxS,
        MinScore = t.minS,
        AvaScore = t.avgs
    from IndustryScore m,
    (
      SELECT vocationCode, CapId, KeyId,MAX(Data) AS maxS, MIN(Data) AS minS, AVG(Data) AS avgS      
      FROM CompanyKeyData
      GROUP BY CapId, KeyId, vocationCode
    ) t
    where m.IndustryCode = t.vocationCode and m.CapableId = t.CapId and m.KeyId = t.KeyId
      

  4.   

    我觉得插入难度大了点,直接做一个左连接插入到新表行不行?
    比如这样:select ……insert newtable form table1 left join table2 on  table1。field=table2.field
      

  5.   

    呵呵,我直接copy你的sql忘记有group by了。
    修改一下  ,我没有测试,理论上没有问题了Create Proc dbo.P_WriteIndustryScoreasset nocount on--先把聚合的数据写到临时表里
    SELECT vocationCode, CapId as Cap_Id, KeyId as Key_Id,
    MAX(Data) AS maxS,
    MIN(Data) AS minS,
    AVG(Data) AS avgS Into #Pqh
    FROM CompanyKeyData
    GROUP BY CapId, KeyId, vocationCodeInsert Into IndustryScore
    SELECT * FROM #Pqh as pqh
    Where not Exists(select 1 from IndustryScore as pqs
    where pqh.IndustryCode=pqs.vocationCode
    and pqs.CapableId=pqh.CapId and pqs.KeyId=pqh.KeyId)update IndustryScore
    set 
    MaxScore=maxS,
    MinScore=minS,
    AvaScore=avgS
    FROM #Pqh
    where IndustryCode=vocationCode
    and   CapableId=Cap_Id
    and   KeyId=Key_Id)
    Truncate Table #Pqh
    Drop Table #Pqhset nocount off
    go
      

  6.   

    楼主说的要求用一条SQL基本上不可能完成,我原来也遇到过这种问题,就是把查询结果更新到一个表中,如果表中已有相同的,就更新不同的项目,如果表中没有的,就插入。解决方法基本上就是分几个步骤走,1,把查询结果放到一个临时表中;2.用临时表和需要更新的表用Key键进行关联,对相同的KEy的不同值进行更新;3.用临时表和需要更新的表用KEy键进行关联,查找临时表有而更新表中没有的记录,并把这些记录插入到更新表中。思路就是这样,我相信以楼主的水平,写出这样的SQL应该不是难事。