我希望从[data_sekolah]表到[data_baru]表的复制数据[jumlah siswa]和[{1}}函数(列[jumlah siswa]按列[class]和[date]汇总)
我试过了这个查询
sum
我收到错误:
聚合可能不会出现在UPDATE语句的集合列表中。
如果我没有使用update [data_baru]
set [jumlah siswa]= SUM(DS.[jumlah siswa])
from [data_baru] DB
inner join [data_sekolah] DS on DB.[class] = DS.[class]
and month(DB.[Date]) = month(DS.[Date])
函数,我可以按sum
表
jumlah siswa
中排名前1
答案 0 :(得分:1)
使用以下查询:
UPDATE [data_baru]
SET [jumlah siswa]= (SELECT SUM(DS.[jumlah siswa]
FROM [data_sekolah] DS
WHERE DB.[class]=DS.[class]
AND month(DB.[Date]) = month(DS.[Date])
FROM [data_baru] DB
答案 1 :(得分:1)
UPDATE Process_Master
SET cnt = a.ct
FROM Process_Master
INNER JOIN ( SELECT dbo.Process_Master.Process_Id ,
sum(1) AS ct
FROM dbo.Employee_Process
INNER JOIN dbo.Process_Master ON dbo.Employee_Process.Process_Id = dbo.Process_Master.Process_Id
GROUP BY dbo.Process_Master.Process_Id
) AS a ON Process_Master.Process_Id = a.Process_Id
答案 2 :(得分:1)
以下查询应该可以解决问题!
UPDATE DB
SET [jumlah siswa] = DS.Sum_jumlah_siswa
FROM [data_baru] AS DB
CROSS APPLY
( SELECT SUM(DS.[jumlah siswa]) AS Sum_jumlah_siswa
FROM [data_sekolah] AS DS
WHERE DB.[class]=DS.[class]
AND month(DB.[Date]) = month(DS.[Date])
) AS DS
答案 3 :(得分:1)
尝试使用以下查询..
update DB
set [jumlah siswa]= SUM_jumlah siswa
FROM [data_baru] DB
INNER JOIN
( Select class,month([Date]) Month
,SUM([jumlah siswa]) SUM_jumlah siswa
From [data_sekolah]
Group by class,month([date]) ) DS
ON DB.[class]=DS.[class]
and month(DB.[Date]) =DS.Month)
答案 4 :(得分:1)
试试这个:
UPDATE DB SET [jumlah siswa]= t.tot
FROM [data_baru] DB
OUTER APPLY (SELECT SUM(DS.[jumlah siswa]) tot FROM [data_sekolah] DS
WHERE DB.[class]=DS.[class] AND MONTH(DB.[Date]) = MONTH(DS.[Date])
GROUP BY DS.[class], MONTH(DS.[Date])) t
答案 5 :(得分:0)
首先对组进行求和(匹配条件)
; with temp as (
select DB.[class] as class , month(DB.[Date]) as date ,
SUM(DS.[jumlah siswa]) as sum_jumlahsiswa
from [data_sekolah] DS group by DB.[class], month(DB.[Date])
)
update [data_baru] set [jumlah siswa] =sum_jumlahsiswa
FROM [data_baru] DB
INNER JOIN temp DS ON DB.[class]=DS.[class] and DB.[Date] = month(DS.[Date] )