在WHERE中加入MAX

时间:2012-04-17 15:46:17

标签: mysql sql database join

有2个表:

temperature_now

machine_id | temperature
------------------------
    1      |     15
    2      |     20
    3      |     13

temperature_history

change_id | machine_id | temperature | controller
-------------------------------------------------
    1           1            6           Carl
    2           2            9           Steve
    3           1            7           John
    4           1            15          Peter
    5           2            20          Peter
    6           3            13          Martin

temperature_now.machine_id = temperature_history.machine_id

change_id是auto_increment

我需要在每台机器上进行最后一次温度变化

machine_id | temperature_now | temperature_before | controller
--------------------------------------------------------------
    1              15                   7              John
    2              20                   9              Steve

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

你可以使用Common Table Expression作为Bellow。

<强>已更新

WITH History (MachineID, Temp, Controller)
AS
(
   SELECT machine_id, temperature, controller 
   FROM dbo.temperature_history
   WHERE change_id in ((SELECT MAX(change_id) FROM dbo.temperature_history AS TH
   INNER JOIN dbo.temperature_now AS TN
   ON TN.machine_id = TH.machine_id
   WHERE TN.temperature != TH.temperature 
   GROUP BY TH.machine_id))
)

SELECT N.machine_id, 
N.temperature, 
H.Temp as 'temperature_before',
H.Controller
FROM dbo.temperature_now AS N
INNER JOIN History AS H
ON H.MachineID = N.machine_id
ORDER BY N.machine_id;

然后你将得到结果作为波纹:

machine_id   temperature    temperature_before  Controller
1            15             7                   John
2            20             9                   Steve

答案 1 :(得分:0)

select n.machine_id, n.temperature as temperature_now, 
    h.temperature as temperature_before, h.controller
from temperature_now n
left outer join (
    select th.machine_id, max(th.change_id) as MaxChangeID
    from temperature_history th
    Inner join temperature_now tn on th.machine_id = tn.machine_id and th.temperature <> tn.temperature
    group by th.machine_id
) hm on n.machine_id = hm.machine_id
left outer join temperature_history h on hm.machine_id = h.machine_id
    and hm.MaxChangeID = h.change_id
order by n.machine_id

答案 2 :(得分:0)

这可能有效,但嵌套查询可能会出现性能问题。 如果我可以优化查询,我会发布。


SELECT DISTINCT TN.MACHINE_ID,
                TN.TEMPERATURE,
                (SELECT DISTINCT TH.TEMPERATURE
                   FROM TEMPERATURE_HISTORY TH
                  WHERE TH.CHANGE_ID =
                        (SELECT MAX(TH1.CHANGE_ID)
                           FROM TEMPERATURE_HISTORY TH1
                          WHERE TH1.MACHINE_ID = TN.MACHINE_ID
                            AND TH1.CHANGE_ID <
                                (SELECT MAX(TH2.CHANGE_ID)
                                   FROM TEMPERATURE_HISTORY TH2
                                  WHERE TH2.MACHINE_ID = TN.MACHINE_ID))) AS TEMPERATURE_BEFORE,
                (SELECT DISTINCT TH.CONTROLLER
                   FROM TEMPERATURE_HISTORY TH
                  WHERE TH.CHANGE_ID =
                        (SELECT MAX(TH1.CHANGE_ID)
                           FROM TEMPERATURE_HISTORY TH1
                          WHERE TH1.MACHINE_ID = TN.MACHINE_ID
                            AND TH1.CHANGE_ID <
                                (SELECT MAX(TH2.CHANGE_ID)
                                   FROM TEMPERATURE_HISTORY TH2
                                  WHERE TH2.MACHINE_ID = TN.MACHINE_ID))) AS CONTROLLER
  FROM TEMPERATURE_NOW TN

希望有所帮助