2014-03-13 138 views
2

所以我曾經有10個查詢,相同的SELECT語句,不同的WHERE。一個查詢是這樣的:MYSQL查詢,其中SELECT語句(要多行/ where語句)

SELECT 
    workdate AS `Date`, 
    DAYNAME(workdate) AS `Day`, 
    COALESCE(TRUNCATE(SUM((CASE 
        WHEN 
         paycode = '03 Overtime 2.0' 
        then 
         (IF(historyemployeepay.`Other Rate` = 0, 
          (employeehours * (historyemployeepay.`Base Rate` * 2.0)), 
          (employeehours * (historyemployeepay.`Other Rate` * 2.0)))) 
        WHEN paycode = '01 Ordinary' then (employeehours * historyemployeepay.`Base Rate`) 
        WHEN 
         paycode = '02 Overtime 1.5' 
          OR paycode = '03 Overtime 2.0' 
        then 
         (IF(historyemployeepay.`Other Rate` = 0, 
          (employeehours * (historyemployeepay.`Base Rate` * 1.5)), 
          (employeehours * (historyemployeepay.`Other Rate` * 1.5)))) 
       end)), 
       2), 
      0) AS `Amount` 
FROM 
    otherrates, 
    employeedata 
     INNER JOIN 
    employeehours ON employeedata.`ID Number` = employeehours.employeeid 
     JOIN 
    historyemployeepay ON employeehours.employeeid = historyemployeepay.EmployeeID 
WHERE 
    shift = 'PM' AND DriverDock = 'Driv' 
     AND `Salary Code Description` LIKE '%Perm%' 
     AND DAYNAME(workdate) <> 'Saturday' 
     AND historyemployeepay.FromDate <= employeehours.workdate 
     AND (historyemployeepay.ToDate IS NULL 
     OR employeehours.workdate <= historyemployeepay.ToDate) 
GROUP BY workdate 

我想有他們所有網站頁面上,如果我這樣做單獨的網站頁面需要年齡加載...所以我想到了一個好主意的嘗試將它們鏈接在一起,如下所示。請注意我在SELECT語句中取出了八行,僅留下了兩個例子。

我該如何得到這個工作?它不顯示正確的值(空),我做錯了什麼?

SELECT 
    workdate AS `Date`, 
    DAYNAME(workdate) AS `Day`, 
    (CASE 
     WHEN 
      shift = 'AM' AND DriverDock = 'Driv' 
       AND `Salary Code Description` LIKE '%Perm%' 
     then 
      COALESCE(TRUNCATE(SUM((CASE 
          WHEN 
           paycode = '03 Overtime 2.0' 
          then 
           (IF(historyemployeepay.`Other Rate` = 0, 
            (employeehours * (historyemployeepay.`Base Rate` * 2.0)), 
            (employeehours * (historyemployeepay.`Other Rate` * 2.0)))) 
          WHEN paycode = '01 Ordinary' then (employeehours * historyemployeepay.`Base Rate`) 
          WHEN 
           paycode = '02 Overtime 1.5' 
            OR paycode = '03 Overtime 2.0' 
          then 
           (IF(historyemployeepay.`Other Rate` = 0, 
            (employeehours * (historyemployeepay.`Base Rate` * 1.5)), 
            (employeehours * (historyemployeepay.`Other Rate` * 1.5)))) 
         end)), 
         2), 
        0) 
    end) AS `AMPERMDRIV`, 
    (CASE 
     WHEN 
      shift = 'AM' AND DriverDock = 'Dock' 
       AND `Salary Code Description` LIKE '%Perm%' 
     then 
      COALESCE(TRUNCATE(SUM((CASE 
          WHEN 
           paycode = '03 Overtime 2.0' 
          then 
           (IF(historyemployeepay.`Other Rate` = 0, 
            (employeehours * (historyemployeepay.`Base Rate` * 2.0)), 
            (employeehours * (historyemployeepay.`Other Rate` * 2.0)))) 
          WHEN paycode = '01 Ordinary' then (employeehours * historyemployeepay.`Base Rate`) 
          WHEN 
           paycode = '02 Overtime 1.5' 
            OR paycode = '03 Overtime 2.0' 
          then 
           (IF(historyemployeepay.`Other Rate` = 0, 
            (employeehours * (historyemployeepay.`Base Rate` * 1.5)), 
            (employeehours * (historyemployeepay.`Other Rate` * 1.5)))) 
         end)), 
         2), 
        0) 
    end) AS `AMPERMDOCK` 
FROM 
    otherrates, 
    employeedata 
     INNER JOIN 
    employeehours ON employeedata.`ID Number` = employeehours.employeeid 
     JOIN 
    historyemployeepay ON employeehours.employeeid = historyemployeepay.EmployeeID 
WHERE 
    DAYNAME(workdate) <> 'Saturday' 
     AND historyemployeepay.FromDate <= employeehours.workdate 
     AND (historyemployeepay.ToDate IS NULL 
     OR employeehours.workdate <= historyemployeepay.ToDate) 
GROUP BY workdate 

看一看結果:

enter image description here

+0

爲什麼對SUM集合外部的'shift','DriverDock'進行條件測試? ......是否真的想要評估那些聚合後的人? (對我來說不正確)。在評估可能返回NULL的CASE後,爲什麼沒有執行COALESCE和TRUNCATE?爲什麼對'otherrates'表的CROSS JOIN操作,它是否只包含一行,以及爲什麼連接的逗號語法?給這些表分配簡短的別名可以使合格的列引用更短,並且許多列引用不合格,我們無法確定這些列來自哪個表; – spencer7593

+0

爲什麼第二次測試'03加班2.0',這已經在早些時候被抓到了? – spencer7593

回答

1

的CASE表達式有一個隱含的ELSE NULL,而你沒有COALESCE捕捉NULL上最CASE。

在聚合計算後,未包含在GROUP BY中的非聚合的評估看起來不正確;看起來會返回「錯誤」的結果。

我想你想要更多的東西是這樣的:

SELECT h.workdate AS `Date` 
    , DAYNAME(h.workdate) AS `Day` 
    , COALESCE(TRUNCATE(SUM(
     CASE 
     WHEN (shift="AM" AND DriverDock = 'Driv' AND `Salary Code Description` LIKE '%Perm%') 
      THEN 
      CASE 
      WHEN paycode IN ('03 Overtime 2.0') 
       THEN (employeehours * IF(p.`Other Rate`=0,p.`Base Rate`,p.`Other Rate`) * 2.0) 
      WHEN paycode IN ('02 Overtime 1.5') 
       THEN (employeehours * IF(p.`Other Rate`=0,p.`Base Rate`,p.`Other Rate`) * 1.5) 
      WHEN paycode IN ('01 Ordinary') 
       THEN (employeehours * p.`Base Rate`) 
      END 
     END 
     ),2),0) AS `AMPERMDRIV` 
    , COALESCE(TRUNCATE(SUM(
     CASE 
     WHEN (shift="AM" AND DriverDock = 'Dock' AND `Salary Code Description` LIKE '%Perm%') 
      THEN 
      CASE 
      WHEN paycode IN ('03 Overtime 2.0') 
       THEN (employeehours * IF(p.`Other Rate`=0,p.`Base Rate`,p.`Other Rate`) * 2.0) 
      WHEN paycode IN ('02 Overtime 1.5') 
       THEN (employeehours * IF(p.`Other Rate`=0,p.`Base Rate`,p.`Other Rate`) * 1.5) 
      WHEN paycode IN ('01 Ordinary') 
       THEN (employeehours * p.`Base Rate`) 
      END 
     END 
     ),2),0) AS `AMPERMDOCK` 
    FROM employeedata d 
    JOIN employeehours h 
    ON d.`ID Number` = h.employeeid 
    JOIN historyemployeepay p 
    ON (h.employeeid = p.EmployeeID) 
    AND (h.workdate >= p.FromDate) 
    AND (h.workdate <= p.ToDate OR p.ToDate IS NULL) 
CROSS 
    JOIN otherrates r 
WHERE DAYNAME(h.workdate) <> 'Saturday' 
GROUP BY h.workdate 

我會將所有的列引用,如果我知道的柱子來自哪個表。

+0

謝謝斯賓塞,那很完美。我還有一個問題,也許你可以幫忙?請看看這個問題:http://stackoverflow.com/questions/22394073/mysql-query-convert-row-to-columns-with-complex-query – atomapps