1

嗨,我有两张包含大量数据的表。我必须得到两个表的一些数据。为此,我使用inner join. 但是当我使用SUM我得到错误的结果。我知道结果乘以返回的行数。我怎样才能解决这个问题?

请求:

select  SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'cot nette',
    SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS 'Fond comp',
    SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'Carte Verte',
    SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS 'Adhesion',
    SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe',
    SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Fond Comp',
    SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Adhésion',
    SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe carte verte',
    SUM(hst.Q_Access) as 'Access'
from  OtoHistorique hst
inner Join OtoHistoriqueDet det On det.IdHistorique = hst.IdHistorique
where hst.POLICE = 3221086

当预期结果为 20 时,我在Access列中得到 60

任何人都可以帮助我吗?抱歉,如果我在请求中使用了错误的语法,但我只是 SQL 的初学者,目前结果比语法更重要。

4

2 回答 2

1

您可能在表之间存在一对多的关系,这意味着某些值SUM会重复。你可以通过避免这样的连接来解决这个问题:

select  SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'cot nette',
    SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS 'Fond comp',
    SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'Carte Verte',
    SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS 'Adhesion',
    SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe',
    SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Fond Comp',
    SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Adhésion',
    SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe carte verte',
    (SELECT SUM(hst.Q_Access) 
     FROM OtoHistorique hst 
     WHERE det.IdHistorique = hst.IdHistorique) as 'Access' 
FROM OtoHistoriqueDet det 
WHERE EXISTS (SELECT 1 
              FROM OtoHistorique hst2 
              WHERE hst2.POLICE = 3221086 
                AND hst2.IdHistorique = det.IdHistorique)

这可能会更优化,但我们需要有关两个表之间逻辑的更多信息。

于 2013-09-16T10:57:46.783 回答
0

One approach is to do the aggregation before the join:

select det.*,
       SUM(hst.Q_Access) as "Access"
from  OtoHistorique hst inner Join
      (select det.IdHistoric,
              SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) as "cot nette"
              SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS "Fond comp",
              SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS "Carte Verte",
              SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS "Adhesion",
              SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe",
              SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Fond Comp",
              SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Adhésion",
              SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe carte verte",
              SUM(hst.Q_Access) as "Access"
       from OtoHistoriqueDet det
       group by det.IdHistorique
      ) det
      On det.IdHistorique = hst.IdHistorique
where hst.POLICE = 3221086;

Because you are filtering the query, this might be expensive. The entire details table has to be aggregated. So, you can do the join in the subquery for filtering purposes and then again for the calculation:

select det.*,
       SUM(hst.Q_Access) as "Access"
from  OtoHistorique hst inner Join
      (select det.IdHistoric,
              SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) as "cot nette"
              SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS "Fond comp",
              SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS "Carte Verte",
              SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS "Adhesion",
              SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe",
              SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Fond Comp",
              SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Adhésion",
              SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe carte verte"
       from OtoHistoriqueDet det join
            OtoHistorique hst 
            On det.IdHistorique = hst.IdHistorique and hst.POLICE = 3221086
       group by det.IdHistorique
      ) det
      On det.IdHistorique = hst.IdHistorique
where hst.POLICE = 3221086;
于 2013-09-16T10:52:16.013 回答