3

我从 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

好吧,我或多或少地知道为什么会出现此错误:这是因为我的选择案例在不同时间提取了两行,但我不知道如何摆脱它..:/

4

2 回答 2

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;
于 2014-06-08T11:34:55.553 回答
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);
于 2014-06-08T11:27:38.747 回答