如何将重复数据值分组到我的脚本

时间:2017-07-25 05:10:10

标签: mysql sql tsql

这是我的脚本的结果,任何人都可以帮助按此日期分组。正如你在该图像上看到的那样,它在每个计数器中都有4行。 This is the image of output

   SELECT DISTINCT(effectiveDate),


      IF(note = "REGULAR LOGGED" and counter = '1',log,
      IF(note = "SICK LEAVE" and counter = '1',"SICK LEAVE",
      IF(note = "VACATION LEAVE" and counter = '1',"VACATION LEAVE",
      IF(note = "HOLIDAY" and counter = '1',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
      IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '1','Saturday',
      IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '1','Sunday','')))))) as COUNTER_1,



      IF(note = "REGULAR LOGGED" and counter = '2',log,
      IF(note = "SICK LEAVE" and counter = '2',"SICK LEAVE",
      IF(note = "VACATION LEAVE" and counter = '2',"VACATION LEAVE",
      IF(note = "HOLIDAY" and counter = '2',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
      IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '2','Saturday',
      IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '2','Sunday','')))))) as COUNTER_2,


       IF(note = "REGULAR LOGGED" and counter = '3',log,
      IF(note = "SICK LEAVE" and counter = '3',"SICK LEAVE",
      IF(note = "VACATION LEAVE" and counter = '3',"VACATION LEAVE",
      IF(note = "HOLIDAY" and counter = '3',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
      IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '3','Saturday',
      IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '3','Sunday','')))))) as COUNTER_3,


        IF(note = "REGULAR LOGGED" and counter = '4',log,
      IF(note = "SICK LEAVE" and counter = '4',"SICK LEAVE",
      IF(note = "VACATION LEAVE" and counter = '4',"VACATION LEAVE",
      IF(note = "HOLIDAY" and counter = '4',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
      IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '4','Saturday',
      IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '4','Sunday','')))))) as COUNTER_4


    FROM schedules sched
      LEFT JOIN timesheet ON sched.empid = timesheet.empid
    WHERE sched.empid='40'

1 个答案:

答案 0 :(得分:0)

SELECT effectiveDate,


  sec_to_time(SUM(time_to_sec(IF(note = "REGULAR LOGGED" and counter = '1',log,
  IF(note = "SICK LEAVE" and counter = '1',"SICK LEAVE",
  IF(note = "VACATION LEAVE" and counter = '1',"VACATION LEAVE",
  IF(note = "HOLIDAY" and counter = '1',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
  IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '1','Saturday',
  IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '1','Sunday',''))))))))) as COUNTER_1,



  sec_to_time(SUM(time_to_sec(IF(note = "REGULAR LOGGED" and counter = '2',log,
  IF(note = "SICK LEAVE" and counter = '2',"SICK LEAVE",
  IF(note = "VACATION LEAVE" and counter = '2',"VACATION LEAVE",
  IF(note = "HOLIDAY" and counter = '2',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
  IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '2','Saturday',
  IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '2','Sunday',''))))))))) as COUNTER_2,


  sec_to_time(SUM(time_to_sec(IF(note = "REGULAR LOGGED" and counter = '3',log,
  IF(note = "SICK LEAVE" and counter = '3',"SICK LEAVE",
  IF(note = "VACATION LEAVE" and counter = '3',"VACATION LEAVE",
  IF(note = "HOLIDAY" and counter = '3',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
  IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '3','Saturday',
  IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '3','Sunday',''))))))))) as COUNTER_3,


  sec_to_time(SUM(time_to_sec(IF(note = "REGULAR LOGGED" and counter = '4',log,
  IF(note = "SICK LEAVE" and counter = '4',"SICK LEAVE",
  IF(note = "VACATION LEAVE" and counter = '4',"VACATION LEAVE",
  IF(note = "HOLIDAY" and counter = '4',(SELECT description FROM holidays WHERE sched.effectiveDate = holidays.date),
  IF(DAYNAME(sched.effectiveDate)='Saturday' and counter = '4','Saturday',
  IF(DAYNAME(sched.effectiveDate)='Sunday' and counter = '4','Sunday',''))))))))) as COUNTER_4


FROM schedules sched
  LEFT JOIN timesheet ON sched.empid = timesheet.empid
WHERE sched.empid='40'
GROUP BY effectiveDate

MYSQL不允许您通过特定列区分结果 - distinct只能应用于整个行以便返回唯一行。生效日期会重复,因为生效日期COUNTER_1,COUNTER_2,COUNTER_3和COUNTER_4的每个新组合都被视为不同的行。

以上使用有效日期的群组是否可以满足您的需求?