sql-server - 关键字 GROUP 附近的语法不正确
问题描述
我有以下更新语句,但出现错误:
关键字“GROUP”附近的语法不正确
执行时。
CREATE TRIGGER [dbo].[target_cumulative]
ON [dbo].[Appointments]
AFTER INSERT, UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE T1
SET T1.[Target_Cumulative] = SUM(ISNULL(CAST([TARGET] AS FLOAT), 0))
FROM dbo.Appointments AS T1
INNER JOIN inserted AS I ON I.UniqueId = T1.UniqueId
GROUP BY CONVERT(DATE, T1.StartDate), T1.ResourceId;
END;
解决方案
干得好:
create table appt (target int, target_cumulative int, unique_id int, resource_id int, start_date datetime);
create table inserted(unique_id int, value int);
insert into appt values(10, 0, 1, 1, '2019-07-26 08:00:00');
insert into appt values(20, 0, 2, 2, '2019-07-26 07:00:00');
insert into appt values(30, 0, 3, 1, '2019-07-26 10:00:00');
insert into appt values(10, 0, 4, 1, '2019-07-26 11:00:00');
insert into appt values(20, 0, 5, 2, '2019-07-26 09:00:00');
insert into inserted values(1, 10);
insert into inserted values(2, 10);
insert into inserted values(3, 10);
insert into inserted values(4, 10);
insert into inserted values(5, 10);
update appt set target_cumulative=xtarget
from (select t1.resource_id, sum(t1.target)as xtarget from appt t1
inner join inserted as i on t1.unique_id=i.unique_id
group by convert(date, t1.start_date), t1.resource_id) t2
where appt.resource_id=t2.resource_id;
这是您可以根据需要检查和修改的小提琴:dbfiddle
推荐阅读
- java - Spring计划任务运行单独的线程
- java - zip 文件未在 gradle build 中生成
- r - 如何按特征值过滤 mlr3 任务数据集
- python - 嘿,我如何为我在 python 中用 tkinter 编写的 Rock、Paper、Scissor 编写规则?
- arrays - 在 Swift 中获取解析的 JSON 数据的索引
- python-3.x - 仅批处理读取/保存文件夹中的最后一张图像
- sql-server - Liquibase diffChangeLog 不报告存储过程
- flutter - 在设备上按下后退按钮时,我的应用程序应该关闭,而无需转到 Flutter 中的任何先前屏幕
- c++ - 数组嵌套结构初始化
- c# - 反序列化 JSON 并返回 C# 中的值?