PostgreSQL选择两个日期之间的出勤

时间:2015-12-30 09:15:26

标签: sql postgresql pivot crosstab time-and-attendance

我正在考虑日期范围的出勤表报告:

Student  01.12 02.12 03.12 04.12 05.12 06.12
STUDENT1 TRUE  TRUE  TRUE  FALSE TRUE  FALSE
STUDENT2 TRUE  FALSE TRUE  TRUE  TRUE  TRUE

我的表:

id_
student_
date_
present_

1 个答案:

答案 0 :(得分:1)

我很幸运。所以这是我猜你可能会问的问题:

SELECT * FROM crosstab(
      $$SELECT student_, date_, present_
        FROM   tbl
        WHERE  date_ BETWEEN '2015-12-01'::date AND '2015-12-06'::date
        ORDER  BY 1,2$$

     ,$$SELECT unnest('{2015-12-01, 2015-12-02, 2015-12-03
                      , 2015-12-04, 2015-12-05, 2015-12-06}'::date[])$$)
AS ct ("Student" text, "01.12" bool, "02.12" bool, "03.12" bool
                     , "04.12" bool, "05.12" bool, "06.12" bool);

说明: