比较一年的结束日期和今年的开始日期+ 1

时间:2016-08-12 15:07:14

标签: sql

year X+1 2009 2008-12-21 00:00:00.000 2009-12-28 00:00:00.000 
year X 2008 2007-12-26 00:00:00.000 2008-12-20 00:00:00.000

我想将年份x(2008年)的结束日期与年份x + 1(2009年)的开始日期进行比较

1 个答案:

答案 0 :(得分:0)

LEFT JOINS应用于您的示例查询:

   SELECT CONCAT(stu.firstname,' ', stu.surname) student,
          COALESCE(f_group.weight_sum,0) + COALESCE(a_group.weight_sum,0) + COALESCE(s_group.weight_sum,0) points
     FROM students stu
LEFT JOIN (
       SELECT s_f.id, SUM(f.category_id IN (10,13,15) + 2 * f.category_id IN (1,7,8)) weight_sum
         FROM students s_f
         JOIN flags f
           ON f.student_id = s_f.id
          AND f.category_id IN (1,7,8,10,13,15) 
        WHERE s_f.year_group = :year_group
     GROUP BY s_f.id           
          ) f_group
LEFT JOIN (
       SELECT s_a.id, 3 * COUNT(*) weight_sum
         FROM students s_a
         JOIN alerts a
           ON a.student_id = s_a.id
          AND a.type = 'P' 
        WHERE s_a.year_group = :year_group
     GROUP BY s_a.id           
          ) a_group
LEFT JOIN (
       SELECT s_s.id, COUNT(*) weight_sum
         FROM students s_s
         JOIN sanctions s
           ON s.student_id = s_s.id
          AND s.category_id = 1
        WHERE s_s.year_group = :year_group
     GROUP BY s_s.id           
          ) s_group
    WHERE stu.year_group = :year_group
 ORDER BY points DESC
    LIMIT 0, 20