SQL Server Update Group by

前端 未结 3 1596
逝去的感伤
逝去的感伤 2020-11-30 08:13

I\'m trying to execute this on MS-SQL but returns me an error just at the Group by line

update #temp
Set Dos=Count(1)
From Temp_Table2010 s
where Id=s.Total          


        
3条回答
  •  眼角桃花
    2020-11-30 09:00

    In SQL Server you can do aggregation in an update query you just have to do it in a subquery and then join it on the table you want to update.

    UPDATE  #temp
    SET     Dos = Cnt
    FROM    #temp 
        INNER JOIN (SELECT Total, COUNT(*) AS Cnt FROM Temp_Table2010 GROUP BY Total) AS s
            ON Id = s.Total 
    

    Doing this:

    WHERE total in (select id from #temp)
    

    And then:

     INNER JOIN counts 
        ON t.id = counts.total 
    

    Is redundant.

    The join solves the "total in (...)" requirement. Group on the key and then join.

提交回复
热议问题