我从postgres开始,我在选择查询中遇到多个选择案例的问题。
bd_david=> select * from edt_sem;
code_module | groupe | week | day | hour | room
-------------+----------+------+-----+--------+-------
M3106 | INFOFCAS | 1 | 1 | 1 | O104
M3105 | INFOFCAS | 1 | 1 | 5 | O104
M3106 | INFOFCAS | 2 | 1 | 1 | O104
(3 rows)
我想要做的是创建一个仅显示特定星期行的函数,并且我正在使用此函数:
CREATE or REPLACE function show_edt(IN paramSem numeric)
RETURNS TABLE(module varchar, groupe varchar, week numeric, day text, hour text, room varchar) AS
$$
BEGIN
RETURN QUERY
SELECT e.code_module,
e.groupe,
e.sem,
(SELECT CASE
WHEN e.jour = 1 THEN 'Monday'
WHEN e.jour = 2 THEN 'Tuesday'
WHEN e.jour = 3 THEN 'Wednesday'
WHEN e.jour = 4 THEN 'Thursday'
WHEN e.jour = 5 THEN 'Friday'
END "JOUR"
FROM edt_sem WHERE edt_sem.sem = paramSem [ LIMIT 1... ? Don't know ] ),
(SELECT CASE
WHEN heured = 1 THEN '9h'
WHEN heured = 2 THEN '10h'
WHEN heured = 3 THEN '11h'
WHEN heured = 4 THEN '12h'
WHEN heured = 5 THEN '14h'
WHEN heured = 6 THEN '15h'
WHEN heured = 7 THEN '16h'
WHEN heured = 8 THEN '17h'
END "HEURE"
FROM edt_sem WHERE edt_sem.sem = paramSem [ LIMIT 2 ?? don't know.. ]),
e.salle
FROM edt_sem e where e.sem = paramSem;
END;
$$ LANGUAGE plpgsql;
适用于一行:
select * from afficher_edt(2);
module | groupe | week | day | hour | room
--------+----------+---------+-------+-------+-------
M3106 | INFOFCAS | 2 | Monday| 9h | O104
(1 row)
但我有多行问题:
bd_david=> select * from afficher_edt(1);
module | groupe | week | day | hour | room
--------+----------+---------+-------+-------+-------
M3106 | INFOFCAS | 1 | Lundi | 9h | O104
M3105 | INFOFCAS | 1 | Lundi | 9h /!\| O104
/!\应该显示14h而不是9h
编辑:错误消息:
bd_david=> select * from show_edt(1);
ERROR: more than one row returned by a subquery used as an expression
CONTEXT: PL/pgSQL function afficher_edt(numeric) line 3 at RETURN QUERY
好吧,我或多或少地得到了这个错误的原因:它是因为我的选择案例在不同的时间内提取了两行,但我不知道如何摆脱它......: /
答案 0 :(得分:2)
CREATE or REPLACE function show_edt(IN paramSem numeric)
RETURNS TABLE (
module varchar, groupe varchar, week numeric,
day text, hour text, room varchar
) AS $$
BEGIN
RETURN QUERY
SELECT
e.code_module,
e.groupe,
e.sem,
CASE e.jour
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
END as "JOUR",
CASE heured
WHEN 1 THEN '9h'
WHEN 2 THEN '10h'
WHEN 3 THEN '11h'
WHEN 4 THEN '12h'
WHEN 5 THEN '14h'
WHEN 6 THEN '15h'
WHEN 7 THEN '16h'
WHEN 8 THEN '17h'
END as "HEURE"
e.salle
FROM edt_sem e
where e.sem = paramSem;
END;
$$ LANGUAGE plpgsql;
你不需要plpgsql。只是sql
CREATE or REPLACE function show_edt(IN paramSem numeric)
RETURNS TABLE (
module varchar, groupe varchar, week numeric,
day text, hour text, room varchar
) AS $$
SELECT
e.code_module,
e.groupe,
e.sem,
CASE e.jour
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
END as "JOUR",
CASE heured
WHEN 1 THEN '9h'
WHEN 2 THEN '10h'
WHEN 3 THEN '11h'
WHEN 4 THEN '12h'
WHEN 5 THEN '14h'
WHEN 6 THEN '15h'
WHEN 7 THEN '16h'
WHEN 8 THEN '17h'
END as "HEURE"
e.salle
FROM edt_sem e
where e.sem = paramSem;
$$ LANGUAGE sql;
答案 1 :(得分:1)
您的主要问题是不需要在子查询中使用CASE。您的查询应该看起来像这个伪代码:
SELECT
e.code_module,
e.groupe,
e.sem,
CASE ...
CASE ...
e.salle
FROM ...
但首先要说的是使用CASE的一些提示。而不是:
SELECT CASE
WHEN jour = 1 THEN 'Monday'
WHEN jour = 2 THEN 'Tuesday'
WHEN jour = 3 THEN 'Wednesday'
WHEN jour = 4 THEN 'Thursday'
WHEN jour = 5 THEN 'Friday'
END "JOUR"
FROM (select 2 as jour) x
使用:
SELECT CASE jour
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
END "JOUR"
FROM (select 2 as jour) x
但你可以用这种简单的方式做到('2014-01-05'是星期天):
SELECT to_char('2014-01-05'::date+ jour, 'day')
FROM (select 2 as jour) x
您可以写下:而不是第二个CASE:
SELECT (heured + 8)::text || 'h'
FROM (select 2 as heured) x
现在您的基本查询如下所示:
SELECT
e.code_module,
e.groupe,
e.sem,
to_char('2014-01-05'::date+ jour, 'day'),
(heured + 8)::text || 'h',
e.salle
FROM edt_sem e where e.sem = 1;
你的功能:
CREATE or REPLACE function show_edt(paramSem numeric)
RETURNS TABLE(module varchar, groupe varchar, week numeric, day text, hour text, room varchar) AS
$$
BEGIN
RETURN QUERY
SELECT
e.code_module::varchar,
e.groupe::varchar,
e.sem::numeric,
to_char('2014-01-05'::date+ jour, 'day'),
(heured + 8)::text || 'h',
e.salle::varchar
FROM edt_sem e where e.sem = paramSem;
END;
$$ LANGUAGE plpgsql;
select * from show_edt(1);