wiki:WikiStart/AdvancedReports

Version 15 (modified by 121232, 12 months ago) ( diff )

--

Напредни извештаи од базата (SQL и складирани процедури)

1. Листање(приказ) на денешни events, count на денешни часови, испити и tasks:

DECLARE correct_student_id INT;
SET correct_student_id = 11111;

WITH EventData AS (
    SELECT 
        ce.calendar_event_id,
        ce.event_name,
        ce.event_type,
        ce.event_start_time,
        ce.event_end_time,
        nre.event_date
    FROM 
        student_event AS se
    JOIN 
        calendar_event AS ce ON se.calendar_event_id = ce.calendar_event_id
    JOIN 
        non_repeating_events AS nre ON ce.calendar_event_id = nre.calendar_event_id
    WHERE 
        nre.event_date = CURRENT_DATE 
        AND se.student_id = correct_student_id

    UNION

    SELECT 
        ce.calendar_event_id,
        ce.event_name,
        ce.event_type,
        ce.event_start_time,
        ce.event_end_time, 
        CASE
            WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
            WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
            WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
            WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
            WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
            WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
            WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
        END AS event_date
    FROM 
        student_event AS se
    JOIN 
        calendar_event AS ce ON se.calendar_event_id = ce.calendar_event_id
    JOIN 
        repeating_events AS re ON ce.calendar_event_id = re.calendar_event_id
    WHERE 
        re.starting_date <= (
            CASE
                WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
                WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
                WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
                WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
                WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
                WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
                WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
            END
        )
        AND re.ending_date >= (
            CASE
                WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
                WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
                WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
                WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
                WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
                WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
                WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
            END
        )
        AND (EXTRACT(day FROM date_trunc('week', CURRENT_DATE) - date_trunc('week', (
            CASE
                WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
                WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
                WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
                WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
                WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
                WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
                WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
            END
        ))) % (7 * re.repeats_every_n_weeks)) = 0
        AND se.student_id = correct_student_id
) AS EventResult

SELECT 
    COUNT(CASE WHEN event_type = 'Exam' THEN 1 END) AS exam_count,
    COUNT(CASE WHEN event_type <> 'Exam' THEN 1 END) AS non_exam_count,
    COUNT(task.task_id) AS task_count
FROM EventResult
LEFT JOIN task ON EventResult.event_date = task.task_date;

2. Листање(приказ) на таскови за наредните три дена:

DECLARE correct_student_id INT;
SET correct_student_id = 11111;

SELECT 
    t.task_name,
    t.task_description,
    t.task_date,
    t.task_priority
FROM 
    task AS t
JOIN 
    student_task AS st ON t.task_id = st.task_id
JOIN 
    creates_task AS ct ON t.task_id = ct.task_id
JOIN 
    subject_at_term AS sat ON ct.term_subject_id = sat.term_subject_id
        AND ct.term_type = sat.term_type 
        AND ct.starting_year = sat.starting_year
        AND ct.finishing_year = sat.finishing_year
        AND ct.student_id = sat.student_id
WHERE 
    st.student_id = correct_student_id 
    AND t.task_date BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL '3 days'
ORDER BY 
    t.task_priority;

3. Листање(приказ) на испити за наредните седум дена:

DECLARE correct_student_id INT;
SET correct_student_id = 11111;

SELECT 
    ce.event_name, 
    sat.subject_name,
    ce.event_start_time,
    ce.event_end_time
FROM 
    subject_event AS se
JOIN 
    calendar_event AS ce ON se.calendar_event_id = ce.calendar_event_id
JOIN 
    subject_at_term AS sat ON se.term_subject_id = sat.term_subject_id
        AND se.term_type = sat.term_type 
        AND se.starting_year = sat.starting_year
        AND se.finishing_year = sat.finishing_year
        AND se.student_id = sat.student_id
JOIN 
    non_repeating_event AS nre ON ce.calendar_event_id = nre.calendar_event_id -- Corrected table name
WHERE 
    ce.event_type = 'Exam'
    AND se.student_id = correct_student_id
    AND nre.event_date >= CURRENT_DATE
    AND nre.event_date <= CURRENT_DATE + INTERVAL '7 days'
ORDER BY
    nre.event_date,
    ce.event_start_time;

4. Листање(приказ) на сите евенти за одредена недела:

DECLARE correct_student_id INT;
SET correct_student_id = 11111;

SELECT *
FROM (
  SELECT 
    ce.calendar_event_id,
    ce.event_name,
    ce.event_type,
    ce.event_start_time,
    ce.event_end_time,
    nre.event_date
  FROM 
    student_event AS se
  JOIN 
    calendar_event AS ce ON se.calendar_event_id = ce.calendar_event_id
  JOIN 
    non_repeating_events AS nre ON ce.calendar_event_id = nre.calendar_event_id
  WHERE 
    nre.event_date >= date_trunc('week', CURRENT_DATE) 
    AND nre.event_date < date_trunc('week', CURRENT_DATE) + INTERVAL '1 week'
    AND se.student_id = correct_student_id

  UNION

  SELECT 
    ce.calendar_event_id,
    ce.event_name,
    ce.event_type,
    ce.event_start_time,
    ce.event_end_time, 
    CASE
      WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
      WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
      WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
      WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
      WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
      WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
      WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
    END AS event_date
  FROM 
    student_event AS se
  JOIN 
    calendar_event AS ce ON se.calendar_event_id = ce.calendar_event_id
  JOIN 
    repeating_events AS re ON ce.calendar_event_id = re.calendar_event_id
  WHERE 
    re.starting_date <= (
      CASE
        WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
        WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
        WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
        WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
        WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
        WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
        WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
      END
    )
    AND re.ending_date >= (
      CASE
        WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
        WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
        WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
        WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
        WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
        WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
        WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
      END
    )
    AND (EXTRACT(day FROM date_trunc('week', CURRENT_DATE) - date_trunc('week', (
        CASE
          WHEN re.event_week_day = 'MONDAY' THEN date_trunc('week', CURRENT_DATE)::DATE
          WHEN re.event_week_day = 'TUESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '1 day'::DATE
          WHEN re.event_week_day = 'WEDNESDAY' THEN date_trunc('week', CURRENT_DATE) + interval '2 days'::DATE
          WHEN re.event_week_day = 'THURSDAY' THEN date_trunc('week', CURRENT_DATE) + interval '3 days'::DATE
          WHEN re.event_week_day = 'FRIDAY' THEN date_trunc('week', CURRENT_DATE) + interval '4 days'::DATE
          WHEN re.event_week_day = 'SATURDAY' THEN date_trunc('week', CURRENT_DATE) + interval '5 days'::DATE
          WHEN re.event_week_day = 'SUNDAY' THEN date_trunc('week', CURRENT_DATE) + interval '6 days'::DATE
        END
      ))) % (7 * re.repeats_every_n_weeks)) = 0
    AND se.student_id = correct_student_id
) AS Result
ORDER BY 
  event_date, -- Order by day of the week
  event_start_time; -- Order by time

5. Листање(приказ) на сите евенти за одреден семестар:

 DECLARE correct_student_id INT;
SET correct_student_id = 11111;
DECLARE c_term_type VARCHAR(20);
SET c_term_type = 'Zimski';
DECLARE c_starting_year INT;
SET c_starting_year = 2023;
DECLARE c_finishing_year INT;
SET c_finishing_year = 2024;

SELECT 
    ce.event_name,
    sat.subject_name,
    ce.event_type,
    ce.event_description
FROM
    subject_at_term AS sat
JOIN
    subject_event AS se
    ON sat.student_id = se.student_id
    AND sat.term_subject_id = se.term_subject_id
    AND sat.term_type = se.term_type
    AND sat.starting_year = se.starting_year
    AND sat.finishing_year = se.finishing_year
JOIN
    calendar_event AS ce
    ON se.calendar_event_id = ce.calendar_event_id
WHERE 
    se.student_id = correct_student_id
    AND se.starting_year = c_starting_year
    AND se.finishing_year = c_finishing_year
    AND se.term_type = c_term_type;

6. Листање(приказ) на сите евенти и task-ови за зададен предмет:

DECLARE correct_student_id INT;
SET correct_student_id = 11111;
DECLARE c_term_type VARCHAR(20);
SET c_term_type = 'Zimski';
DECLARE c_starting_year INT;
SET c_starting_year = 2023;
DECLARE c_finishing_year INT;
SET c_finishing_year = 2024;
DECLARE c_term_subject_id INT;
SET c_term_subject_id = 87654;

SELECT
    ce.calendar_event_id,
    ce.event_name,
    ce.event_type,
    ce.event_description,
    ce.event_start_time,
    ce.event_end_time,
    NULL AS task_id,
    NULL AS task_name,
    NULL AS task_description,
    NULL AS task_priority,
    NULL AS is_done
FROM
    subject_event se
JOIN
    calendar_event ce ON se.calendar_event_id = ce.calendar_event_id
WHERE
    se.term_subject_id = c_term_subject_id
    AND se.term_type = c_term_type
    AND se.starting_year = c_starting_year
    AND se.finishing_year = c_finishing_year
    AND se.student_id = correct_student_id

UNION

SELECT
    NULL AS calendar_event_id,
    NULL AS event_name,
    NULL AS event_type,
    NULL AS event_description,
    NULL AS event_start_time,
    NULL AS event_end_time,
    t.task_id,
    t.task_name,
    t.task_description,
    t.task_priority,
    t.is_done
FROM
    creates_task ct
JOIN 
    task AS t ON ct.task_id = t.task_id
WHERE
    ct.term_subject_id = c_term_subject_id
    AND ct.term_type = c_term_type
    AND ct.starting_year = c_starting_year
    AND ct.finishing_year = c_finishing_year
    AND ct.student_id = correct_student_id;

7. Листање(приказ) на сите на сите книги и автори по одреден предмет (пример:Бази на податоци):

DECLARE correct_student_id INT;
SET correct_student_id = 11111;

WITH SubjectBooks AS (
    SELECT 
        rb.book_id,
        rb.book_title,
        ba.author_id,
        a.author_name
    FROM 
        recommended_book AS rb
    JOIN 
        book_author AS ba ON rb.book_id = ba.book_id
    WHERE 
        rb.subject_name = 'Bazi na podatoci'
        AND rb.student_id = correct_student_id
)

SELECT 
    sb.book_id,
    sb.book_title,
    sb.author_id,
    sb.author_name
FROM 
    SubjectBooks AS sb;

8. Приказна на сите feedbacks (описи и наслови на feedback) со оценка помала од 3:

DECLARE correct_student_id INT;
SET correct_student_id = 11111;

SELECT 
    fb.fb_id,
    fb.fb_date,
    fb.fb_title,
    fb.fb_description,
    fb.fb_rating
FROM 
    feedback AS fb
WHERE 
    fb.fb_rating < 3
    AND EXISTS (
        SELECT 1
        FROM 
            gives_feedback AS gf
        WHERE 
            gf.fb_id = fb.fb_id
            AND gf.student_id = correct_student_id
    );
Note: See TracWiki for help on using the wiki.