0

There are 2 tables:

Table temperature_now:

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

Table 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 is auto_increment

I need to get the last change of temperature at each machine:

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

Thanks for your help.

4

3 回答 3

1

您可以使用 Common Table Expression as 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
于 2012-04-17T16:38:48.037 回答
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
于 2012-04-17T15:50:46.503 回答
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

希望能帮助到你

于 2012-04-18T05:03:45.707 回答