我用Navicat创建了这个视图,但总和是错误的。
SELECT
planeamento_conf.id_Plano AS id_Plano,
planeamento_conf.plan_ConfEnc AS plan_ConfEnc,
planeamento_conf.plan_Modelo AS plan_Modelo,
planeamento_conf.plan_Total_Pecas AS plan_Total_Pecas,
planeamento_conf.plan_Min_Peca AS plan_Min_Peca,
planeamento_conf.plan_Confecao AS plan_Confecao,
planeamento_conf.plan_Cap_Prod AS plan_Cap_Prod,
planeamento_conf.plan_Horas_Dia AS plan_Horas_Dia,
planeamento_conf.plan_Fatura AS plan_Fatura,
confeccoes.Confeccao AS Confeccao,
Sum(saidas_conf.Pecas_entregues) AS TotalSaidas,
Sum(entradas_pa.Pecas_entregues) AS TotalEntradas
FROM
((planeamento_conf
JOIN confeccoes ON ((planeamento_conf.plan_Confecao = confeccoes.ID_Confeccao))
JOIN saidas_conf ON ((planeamento_conf.id_Plano = saidas_conf.Plano))
JOIN entradas_pa ON ((planeamento_conf.id_Plano = entradas_pa.Plano)))
GROUP BY
planeamento_conf.id_Plano
我要求的字段是sayas_conf.Pecas_entregues和entradas_pa.Pecas_entregues 我有这个id_Plano共同的3个表
答案 0 :(得分:1)
在我的案例中,我的使用方式类似。对我而言,它有效。我希望它也适合你。
SELECT
planeamento_conf.id_Plano AS id_Plano,
planeamento_conf.plan_ConfEnc AS plan_ConfEnc,
planeamento_conf.plan_Modelo AS plan_Modelo,
planeamento_conf.plan_Total_Pecas AS plan_Total_Pecas,
planeamento_conf.plan_Min_Peca AS plan_Min_Peca,
planeamento_conf.plan_Confecao AS plan_Confecao,
planeamento_conf.plan_Cap_Prod AS plan_Cap_Prod,
planeamento_conf.plan_Horas_Dia AS plan_Horas_Dia,
planeamento_conf.plan_Fatura AS plan_Fatura,
confeccoes.Confeccao AS Confeccao,
Sum(saidas_conf.Pecas_entregues) AS TotalSaidas,
Sum(entradas_pa.Pecas_entregues) AS TotalEntradas
FROM planeamento_conf
JOIN confeccoes ON planeamento_conf.plan_Confecao = confeccoes.ID_Confeccao
JOIN saidas_conf ON planeamento_conf.id_Plano = saidas_conf.Plano
JOIN entradas_pa ON planeamento_conf.id_Plano = entradas_pa.Plano
GROUP BY
planeamento_conf.id_Plano
答案 1 :(得分:1)
认为您必须在几个子查询中执行SUM: -
SELECT
planeamento_conf.id_Plano AS id_Plano,
planeamento_conf.plan_ConfEnc AS plan_ConfEnc,
planeamento_conf.plan_Modelo AS plan_Modelo,
planeamento_conf.plan_Total_Pecas AS plan_Total_Pecas,
planeamento_conf.plan_Min_Peca AS plan_Min_Peca,
planeamento_conf.plan_Confecao AS plan_Confecao,
planeamento_conf.plan_Cap_Prod AS plan_Cap_Prod,
planeamento_conf.plan_Horas_Dia AS plan_Horas_Dia,
planeamento_conf.plan_Fatura AS plan_Fatura,
confeccoes.Confeccao AS Confeccao,
sub_saidas_conf.TotalSaidas,
sub_entradas_pa.TotalEntradas
FROM
planeamento_conf
JOIN confeccoes ON ((planeamento_conf.plan_Confecao = confeccoes.ID_Confeccao))
JOIN (SELECT Plano, SUM(Pecas_entregues) AS TotalSaidas FROM saidas_conf GROUP BY Plano) AS sub_saidas_conf ON ((planeamento_conf.id_Plano = sub_saidas_conf.Plano))
JOIN (SELECT Plano, SUM(Pecas_entregues) AS TotalEntradas FROM entradas_pa GROUP BY Plano) AS sub_entradas_pa ON ((planeamento_conf.id_Plano = sub_entradas_pa.Plano))
GROUP BY
planeamento_conf.id_Plano
现在可能不需要外部GROUP BY(取决于confeccoes上是否有重复,您需要消除 - 在这种情况下,DISTINCT可能是更好的选择)。