mysql 2左连接,多个表

时间:2014-03-09 03:36:10

标签: mysql

我有以下查询,它应该在数据之间获得行差异,并计算计算行的2个表之间的总和。但是查询只是运行并运行。

SELECT 
    ThisLog.dt,
    ThisLog.pt04,
    ThisLog.pt06,
    ThisLog.pt08,
    ThisLog.pt10,
    Round(COALESCE(ThisLog.pt04 - PrevLog.pt04, 0),3)*40 AS consumption,
    Round(COALESCE(ThisLog.pt06 - PrevLog.pt06, 0),3)*40 AS consumption1,
    Round(COALESCE(ThisLog.pt08 - PrevLog.pt08, 0),3)*40 AS consumption2,
    Round(COALESCE(ThisLog.pt10 - PrevLog.pt10, 0),3)*40 AS consumption3,
    10Log.pt04,
    10Log.pt06,
    10Log.pt08,
    Round(COALESCE(10Log.pt04 - 10PrevLog.pt04, 0),3)*40 AS consumption4,
    Round(COALESCE(10Log.pt06 - 10PrevLog.pt06, 0),3)*40 AS consumption5,
    Round(COALESCE(10Log.pt08 - 10PrevLog.pt08, 0),3)*40 AS consumption6,
    (Round(COALESCE(10Log.pt04 - 10PrevLog.pt04, 0),3)*40 
        + Round(COALESCE(10Log.pt06 - 10PrevLog.pt06, 0),3)*40
        + Round(COALESCE(10Log.pt08 - 10PrevLog.pt08, 0),3)*40
        + Round(COALESCE(ThisLog.pt04 - PrevLog.pt04, 0),3)*40 
        + Round(COALESCE(ThisLog.pt06 - PrevLog.pt06, 0),3)*40
        + Round(COALESCE(ThisLog.pt08 - PrevLog.pt08, 0),3)*40
        + Round(COALESCE(ThisLog.pt10 - PrevLog.pt10, 0),3)*40
    ) as totalkwh,
FROM
    data_011 AS ThisLog,
    data_010 AS 10Log
LEFT JOIN data_011 AS PrevLog ON PrevLog.dt = (
    SELECT MAX(dt)
    FROM data_011
    WHERE
        dt < ThisLog.dt
)
LEFT JOIN data_010 AS 10PrevLog ON 10PrevLog.dt = (
    SELECT MAX(dt)
    FROM data_010
    WHERE
    dt < 10Log.dt
)
GROUP BY dt, consumption1,consumption2,consumption3

0 个答案:

没有答案