2017-07-25 47 views
1

這是結果我的腳本任何人都可以幫助按此日期分組。就像你在圖片上看到的那樣,它在每個計數器中給出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' 
+0

請注意DISTINCT不是函數。並參見https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-查詢 – Strawberry

+0

你能提供一個輸出應該是什麼樣子的文本示例嗎?這通常有助於 – stombeur

+0

@stombeur用戶的日誌應該在組中。 –

回答

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不允許你不同的結果通過特定列 - 不同只能應用於整個行,以便返回唯一的行。重複生效日期,因爲生效日期COUNTER_1,COUNTER_2,COUNTER_3和COUNTER_4的每個新組合都被視爲不同的行。

以上使用group by effectiveDate給你你需要什麼?

相關問題