GROUP_CONCAT重复值

时间:2015-05-21 08:52:41

标签: mysql sql group-concat

尝试使用GROUP_CONCAT调用班级的10个工作日出勤记录并在其中获取重复值。

查询

SELECT wda.gs_id, wda.gr_no, wda.abridged_name, wda.call_name, wda.grade_name, wda.section_name, wda.`2015-05-11`, wda.`2015-05-12`, wda.`2015-05-13`, wda.`2015-05-14`, wda.`2015-05-15`, wda.`2015-05-18`, wda.`2015-05-19`, wda.`2015-05-20`, wda.`2015-05-21`, wda.`2015-05-22`, wda.daypass_used_ten, wda.total_p, wda.total_l, wda.total_a, wda.total_d, ((wda.total_a * 3)+(wda.total_l * 2)+(if(wda.daypass_used_ten > 0, wda.daypass_used_ten, 0))) as penalty  FROM (SELECT atd.gs_id, cl.gr_no, cl.abridged_name, cl.call_name, cl.grade_name, cl.section_name, dp.daypass_used_ten,GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 11 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 11 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-11', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 12 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 12 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-12', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 13 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 13 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-13', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 14 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 14 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-14', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 15 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 15 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-15', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 18 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 18 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-18', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 19 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 19 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-19', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 20 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 20 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-20', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 21 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 21 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-21', GROUP_CONCAT(DISTINCT if(DAY(atd.Date) = 22 and MONTH(atd.date) = 05, atd.time, if(DAY(aas.date) = 22 and MONTH(aas.date) = 05, abc.dname, NULL))) AS '2015-05-22', COUNT(if(atd.time > '00:00:00', atd.time, NULL)) AS 'total_p',                       
                    COUNT(if(atd.time >= '07:41:00', atd.time, NULL)) AS 'total_l',                     
                    (10 - COUNT(if(atd.time > '00:00:00', atd.time, NULL))) as total_a,                     
                    10 as total_d
                    FROM (select * from atif_attendance.student_attendance group by gs_id, date order by date asc, time asc) as atd
                    right join atif.students_current_classlist cl
                    on cl.gs_id = atd.gs_id
                    left join 
                    (select 
                        atif_attendance.tmpcard_student_used.gs_id AS GS_ID,
                        atif_attendance.tmpcard_student_used.date AS DATE,
                        count(0) AS DayPass_Used_TEN from atif_attendance.tmpcard_student_used
                        where ((atif_attendance.tmpcard_student_used.date >= '2015-05-11')
                        and (atif_attendance.tmpcard_student_used.tmp_card_no > 500)
                        and (atif_attendance.tmpcard_student_used.tmp_card_no < 700))
                        group by atif_attendance.tmpcard_student_used.gs_id
                        order by year(atif_attendance.tmpcard_student_used.date) desc,
                        month(atif_attendance.tmpcard_student_used.date) desc, dayofmonth(atif_attendance.tmpcard_student_used.date) desc) as dp
                    on dp.gs_id = atd.gs_id

                    left join atif_attendance.attendance_absent_solved aas
                    on aas.gs_id = atd.gs_id and aas.date BETWEEN '2015-05-11' and '2015-05-26'
                    left join atif_attendance.attendance_absent_case abc
                    on abc.id = aas.absent_case_id
                    left join atif_attendance.attendance_absent_reasons abr
                    on abr.id = aas.absent_reason_id

                    WHERE atd.date BETWEEN '2015-05-11' and '2015-05-26'
                    and cl.grade_name = 'VI' and cl.section_name = 'W'                      
                    GROUP BY atd.gs_id
                    order by total_p asc, total_l desc, daypass_used_ten desc) as wda order by penalty desc

结果

gs_id   gr_no   abridged_name   call_name   grade_name  section_name    2015-05-11  2015-05-12  2015-05-13  2015-05-14  2015-05-15  2015-05-18  2015-05-19  2015-05-20  2015-05-21  2015-05-22  daypass_used_ten    total_p total_l total_a total_d penalty

14-648  12593   Alyaan Shaikh   Alyaan  VI  W   07:31:35    07:29:36    07:24:48    07:40:42    07:29:49    07:31:06    07:33:23    NULL    08:25:50    NULL    NULL    8   1   2   10  8
11-669  10788   Yousuf Mirza    Yousuf  VI  W   07:23:08    07:16:18    NULL    07:16:33    NULL    07:16:50    NULL    A   U   U   NULL    12  0   -2  10  -6

结果不准确。总存在计数错误

  

A = AuthorizedAbsent
  U =未经授权的缺席

表atif_attendance.attendance_absent_solved with columns

gs_id, date,       type
08-051 2015-05-11, A
08-051 2015-05-14, A

0 个答案:

没有答案