MYSQL查询,SELECT中的语句(有多行/ where语句)

时间:2014-03-13 05:38:50

标签: mysql database

所以我以前有10个查询,相同的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语句中的八行,只留下两行作为示例。

我怎样才能让它发挥作用?它没有显示正确的值(null),我做错了吗?

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

1 个答案:

答案 0 :(得分:1)

CASE表达式隐含了ELSE NULL,并且您没有COALESCE在最外面的CASE上捕获NULL。

在计算总计之后,未包括在GROUP BY中的非总量的评估,这看起来并不合适;看起来它会回归&#34;错误&#34;结果

我想你想要更像这样的东西:

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

如果我知道列来自哪个表,我会限定所有列引用。