我正在完成一项任务,但我已经接近完成任务了,我已经在一段时间内努力了。我需要显示一个具有SCHEDULE列的表和一个DAYS列,其中DAYS列显示基于SCHEDULE的天数列表。 SCHEDULE只有两个选项:'Weekend'和'Weekday。'
这是我想要达到的最终结果:
ID Schedule Days
001 Weekend Saturday, Sunday
我创建了一个进程并使用了此代码:
BEGIN
UPDATE schedules
SET days =
WHEN schedule = 'Weekend' THEN
(SELECT LISTAGG(day, ', ') WITHIN GROUP (ORDER BY day_order)
FROM days
WHERE schedule = 'Weekend'
)
WHEN schedule = 'Weekday' THEN
(SELECT LISTAGG(day, ', ') WITHIN GROUP (ORDER BY day_order)
FROM days
WHERE schedule = 'Weekday'
)
END
当我这样做时,我收到错误:
在遇到以下其中一项时遇到符号“文件结束”:begin function pragma procedure
如果有人能帮助我,我将不胜感激!
答案 0 :(得分:0)
您可以使用MERGE
声明:
MERGE INTO schedules dst
USING (
SELECT schedule,
LISTAGG( day, ', ' ) WITHIN GROUP ( ORDER BY day_order ) AS days
FROM days
GROUP BY schedule
) src
ON ( dst.schedule = src.schedule )
WHEN MATCHED THEN
UPDATE SET days = src.days;
但最好不要将值存储在分隔列表中,只需使用schedules.schedule
和days.schedule
之间的外键引用来连接这两个表。
答案 1 :(得分:0)
您的更新等同于此。
UPDATE schedules s
SET days = (SELECT LISTAGG(day, ', ')
within group ( ORDER BY day_order )
FROM days d
WHERE d.schedule IN ( 'Weekend', 'Weekday' )
AND d.schedule = s.schedule
GROUP BY d.schedule ) ;
但是,我会不推荐这个。将记录存储为逗号分隔值肯定会在将来导致问题。仅使用它来使用如下查询显示结果。这假设在您的日历表中,每天都有唯一的行。如果有重复项,则join
将其设置为从天开始的不同结果集。
SELECT d.id,
d.schedule,
LISTAGG(s.day, ', ')
within GROUP ( ORDER BY d.day_order )
FROM days d
join schedules s
ON ( d.schedule = s.schedule )
WHERE d.schedule IN ( 'Weekend', 'Weekday' )
GROUP BY d.id,
d.schedule