さまざまな条件のテーブルから日付を取得し、さまざまなデータを合計してさまざまな列に配置する必要があります。多くのテストの後、私はこのクエリを持っていますが、遅すぎて大きすぎて、すべてのユニットを取得していません:
SELECT cal.dt, cal.h, a.measure, b.measure, c.measure, d.measure, e.measure, f.measure, g.measure, h.measure, ud.unit_name
FROM calendar cal
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agent FROM MEASURES
WHERE sesion = 0 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) a
ON a.date_bid = dt AND a.hour = h
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agent FROM MEASURES
WHERE sesion = 1 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hora, id_unit, id_agent) b
ON b.date_bid = dt AND b.hour = h AND a.id_unit = b.id_unit AND a.id_agent = b.id_agent
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agent FROM MEASURES
WHERE sesion = 2 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) c
ON c.date_bid = dt AND c.hour = h AND a.id_unit = c.id_unit AND a.id_agent = c.id_agent
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agent FROM MEASURES
WHERE sesion = 3 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) d
ON d.date_bid = dt AND d.hour = h AND a.id_unit = d.id_unit AND a.id_agent = d.id_agent
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agent FROM MEASURES
WHERE sesion = 4 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) e
ON e.date_bid = dt AND e.hour = h AND a.id_unit = e.id_unit AND a.id_agent = e.id_agent
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agente FROM MEASURES
WHERE sesion = 5 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) f
ON f.date_bid = dt AND f.hour = h AND a.id_unit = f.id_unit AND a.id_agent = f.id_agent
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agente FROM MEASURES
WHERE sesion = 6 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) g
ON g.date_bid = dt AND g.hour = h AND a.id_unit = g.id_unit AND a.id_agent = g.id_agent
LEFT JOIN (SELECT date_bid, hour, ROUND(SUM(measure),3) as measure, id_unit, id_agent FROM MEASURES
WHERE sesion = 7 AND date_bid BETWEEN '02/27/2013' AND '02/28/2013' GROUP BY date_bid, hour, id_unit, id_agent) h
ON h.date_bid = dt AND h.hour = h AND a.id_unit = h.id_unit AND a.id_agent = h.id_agent
INNER JOIN FACILITIES ud ON a.id_unit = ud.id_unit
WHERE cal.dt BETWEEN '02/27/2013' AND '02/28/2013' AND a.id_agent = 42
--GROUP BY a.date_bid, a.hour, ud.unit_name
ORDER BY cal.dt, ud.unit_name, cal.h
以前、副選択なしでテーブル間で直接 JOINSを試しましたが、それぞれの結果の数が異なる可能性があるため、機能しませんでしたsesion
。
現在の問題は、このクエリに必要な膨大な時間とは別に、最初のサブセレクト ( sesion = 1
) に表示されるユニットのデータのみを取得していて、ユニットにデータが含まれていない場合、残りのデータが表示されないことです。データ。
このクエリに直面するためのより良い方法があるはずだと思います。