我有12个表(每个月),需要检索一个总和,让我显示查询:
UPDATE otimizacao_equipamento
SET dic_anual = subquery.DIC,
fic_anual = subquery.FIC
FROM (
SELECT sum(dic) AS DIC,sum(fic) AS FIC FROM (
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes1 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes2 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes3 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes4 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes5 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes6 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes7 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes8 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes9 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes10 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes11 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento UNION ALL
SELECT sum(m.dic)/count(m.codConsumidor) AS dic, sum(m.fic)/count(m.codConsumidor) AS fic FROM hierarquiaTemp AS h INNER JOIN gisTemp AS g ON g.codTransf = h.codInstalacao INNER JOIN mes12 AS m ON m.codConsumidor = g.idSap WHERE h.codEquipamento = '351CH004665' AND g.conjuntoAneel = '18' group by g.conjuntoaneel,h.codEquipamento
) a
) AS subquery
WHERE equipamento = '351CH004665' AND conjunto = '18'
版本:" PostgreSQL 9.4.0,由Visual C ++ build 1800编译,64位"
解释:http://explain.depesz.com/s/hIK
PC:Windows 8,8gb ram,64bits,i7
SQL CREATE:https://jsfiddle.net/DirkWeber/nnqc8/1/
每个月和gis表的平均值为7.000.000。
有时查询需要几分钟才能执行。
关系所需的所有列都是索引。
我注意到如果我运行一次查询,第二次它的速度非常快。
希望有人可以提供帮助,如果你们需要更多信息,请问。感谢