下面我試圖從子表更新父表的值和計數匹配值。我的數據庫中的表格:子表中的更新,只有一個值得到更新
issue_dimension
id = issue_id並具有列accno。star_schema
id爲star_id,這個孩子柱已FK ISSUE_ID和列book_frequency
的book_frequency需要匹配在父表中每個登錄號的數量,我想這
update [test1] .[dbo] .star_schema
set [book_frequency] = (
select top 1 COUNT([issue_dimension].ACCNO)as book_frequency
from issue_dimension
group by ACCNO having (COUNT(*)>1) and
issue_dimension.ACCNO = star_schema .ACCNO
)
它僅更新第一個值計數issue_dimension。我需要計算issue_dimension中的每個accno並將其更新爲匹配star_schema的accno。
UPDATE [test1].[dbo].star_schema SET
[book_frequency] =
(
SELECT COUNT([issue_dimension].ACCNO)
FROM issue_dimension
WHERE issue_dimension .ACCNO = star_schema.ACCNO
HAVING COUNT(*)>1
)
它再次只更新一個值,它只計算一個值 – user1074474 2011-12-29 07:52:07
有多少行返回:'SELECT ACCNO,COUNT(*)as CNT FROM issue_dimension GROUP BY ACC_NO HAVING COUNT(*)> 1'? – chopikadze 2011-12-29 08:35:27