我正尝试在MS-SQL上执行此操作,但在Group by行返回错误
update #temp
Set Dos=Count(1)
From Temp_Table2010 s
where Id=s.Total and s.total in (Select Id from #temp)
group by s.Total有谁知道我怎样才能有好的性能来解决这个问题。
发布于 2011-08-08 23:47:18
试一试
;with counts
AS
(
SELECT total, COUNT(*) as dos
FROM temp_table2010
WHERE total in (select id from #temp)
)
UPDATE T
SET dos=counts.dos
FROM #temp T
INNER JOIN counts
ON t.id = counts.total 发布于 2013-10-04 02:46:14
在SQL Server中,您可以在更新查询中执行聚合,只需在子查询中执行聚合,然后在要更新的表上连接它。
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 执行此操作:
WHERE total in (select id from #temp)然后:
INNER JOIN counts
ON t.id = counts.total 是多余的。
连接解决了"total in (...)“要求。在键上分组,然后加入。
发布于 2010-05-18 07:09:49
对于初学者来说,您不能在UPDATE查询中使用聚合-尽管您没有在原始问题中包含错误消息,但我怀疑这就是它告诉您的。
您需要在update之前计算聚合,并将结果存储在临时表中,然后连接到该表以执行update。
https://stackoverflow.com/questions/2853403
复制相似问题