1

我正在编写一个查询,我正在计算 2 个不同表的总和,这些表具有外键约束 (1:n)。

在此处输入图像描述

所以有一张桌子Kunde,里面有客户。每个Customer都由Adm. 每个Kunde交易都有 N 个不同的交易(PbsRow),而每个交易包含 N 个不同的产品(WarengruppeVK)。每笔交易都有一个月和一年(MonatJahr

我需要的是包含以下信息的结果:1)Adm 的名称,2)属于 2013 年该特定 Adm 的客户之一的所有和值的总和,3)属于的所有sollfracht和值的总和该客户在 2013 年的其中一个,4 和 5) 与 2) 和 3) 仅在 2012 年相同handlingnettodb_basisPbsRow

我已经尝试过不同的事情,但是当我使用下一个表进行联接时,我总是最终得到一个带有sollfracht和值的笛卡尔积。handling

请看一下我的查询:

SELECT vj.*,
       j.*,
       adm.ZNAME
FROM ZADM adm,
  (SELECT k.ZADMITARBEITER AS admidvj,
          SUM(vk.ZNETTO) AS summeVJ,
          SUM(vk.ZDB_BASIS) AS summeDBVJ,
          SUM(p.ZSOLLFRACHT) AS sollfrachtVJ,
          SUM(p.ZHANDLING) AS handlingVJ
   FROM ZWARENGRUPPEVK vk
   LEFT JOIN ZPBSROW p ON p.Z_PK=vk.ZPBSROW
   LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
   WHERE ZJAHR=2012
     AND ZMONAT>=1
     AND ZMONAT<=6
   GROUP BY k.ZADMITARBEITER) vj,
  (SELECT k.ZADMITARBEITER AS admidj,
          SUM(vk.ZNETTO) AS summeJ,
          SUM(vk.ZDB_BASIS) AS summeDBJ,
          SUM(p.ZSOLLFRACHT) AS sollfrachtJ,
          SUM(p.ZHANDLING) AS handlingJ
   FROM ZWARENGRUPPEVK vk
   LEFT JOIN ZPBSROW p ON p.Z_PK=vk.ZPBSROW
   LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
   WHERE ZJAHR=2013
     AND ZMONAT>=1
     AND ZMONAT<=6
   GROUP BY k.ZADMITARBEITER) j
WHERE vj.admidvj=j.admidj
  AND vj.admidvj=adm.Z_PK

我该怎么做才能避免使用这种笛卡尔积?当我WarengruppeVK从结果中删除 -table 时,sollfrachtandhandling值是正确的。

提前致谢。

编辑:这里有一些示例。

这是我从上面的查询中得到的结果: 在此处输入图像描述

这是我删除第一个连接时的结果: 在此处输入图像描述

您会注意到这一点sollfrachtVJ并且handlingVJ现在有所不同。它们取自PbsRow笛卡尔积发生的地方。所以这两个值实际上是正确的,但我还需要我注释掉的两个值的总和。

这是我删除那个 Join 后的 SQL 语句:

SELECT vj.*,
       j.*,
       adm.ZNAME
FROM ZADM adm,
  (SELECT k.ZADMITARBEITER AS admidvj,
          --          SUM(vk.ZNETTO) AS summeVJ,
--          SUM(vk.ZDB_BASIS) AS summeDBVJ,

          SUM(p.ZSOLLFRACHT) AS sollfrachtVJ,
          SUM(p.ZHANDLING) AS handlingVJ
   FROM -- ZWARENGRUPPEVK vk
 ZPBSROW p -- LEFT JOIN ZPBSROW p ON p.Z_PK=vk.ZPBSROW

   LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
   WHERE ZJAHR=2012
     AND ZMONAT>=1
     AND ZMONAT<=6
   GROUP BY k.ZADMITARBEITER) vj,
  (SELECT k.ZADMITARBEITER AS admidj,
          SUM(vk.ZNETTO) AS summeJ,
          SUM(vk.ZDB_BASIS) AS summeDBJ,
          SUM(p.ZSOLLFRACHT) AS sollfrachtJ,
          SUM(p.ZHANDLING) AS handlingJ
   FROM ZWARENGRUPPEVK vk
   LEFT JOIN ZPBSROW p ON p.Z_PK=vk.ZPBSROW
   LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
   WHERE ZJAHR=2013
     AND ZMONAT>=1
     AND ZMONAT<=6
   GROUP BY k.ZADMITARBEITER) j
WHERE vj.admidvj=j.admidj
  AND vj.admidvj=adm.Z_PK

编辑 2

好的,这是包含正确结果但缺少 4 列的 SQL 语句。

SELECT vj.*,
       j.*,
       adm.ZNAME
FROM ZADM adm,
  (SELECT k.ZADMITARBEITER AS admidvj,
          SUM(p.ZSOLLFRACHT) AS sollfrachtVJ,
          SUM(p.ZHANDLING) AS handlingVJ
   FROM ZPBSROW p
   LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
   WHERE ZJAHR=2012
     AND ZMONAT>=1
     AND ZMONAT<=6
   GROUP BY k.ZADMITARBEITER) vj,
  (SELECT k.ZADMITARBEITER AS admidj,
          SUM(p.ZSOLLFRACHT) AS sollfrachtJ,
          SUM(p.ZHANDLING) AS handlingJ
   FROM ZPBSROW p
   LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
   WHERE ZJAHR=2013
     AND ZMONAT>=1
     AND ZMONAT<=6
   GROUP BY k.ZADMITARBEITER) j
WHERE vj.admidvj=j.admidj
  AND vj.admidvj=adm.Z_PK

如您所见,、summeJ和不包括在内,这就是问题所在。此结果中的所有值都是正确的,但我的结果中还需要包含这 4 个值。我上面结果的第一个屏幕截图包含正确的、和值,但不正确的、和值。summeVJsummeDBJsummeDBVJsummeJsummeVJsummeDBJsummeDBVJhandlingJhandlingVJsollfrachtJsollfrachtVJ

编辑 3:

我终于找到了一种方法来做到这一点。这是有效的查询。这只是几个子查询:

    SELECT ((summeJ-summeVJ)/summeVJ*100) AS abwNetto,
       (summeJ-summeVJ) AS abwNettoAbs,
       ((summeDBJ-summeDBVJ)/summeDBVJ*100) AS abwDB,
       (summeDBJ-summeDBVJ) AS abwDBAbs,
       t0.*,
       t1.*,
       adm.ZNAME
FROM ZADM adm,
  (SELECT vj.*,
          j.*
   FROM
     (SELECT k.ZADMITARBEITER AS admidvj,
             SUM(p.ZSOLLFRACHT) AS sollfrachtVJ,
             SUM(p.ZHANDLING) AS handlingVJ
      FROM ZPBSROW p
      LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
      WHERE ZJAHR=2012
        AND ZMONAT>=1
        AND ZMONAT<=6
      GROUP BY k.ZADMITARBEITER) vj
   LEFT JOIN
     (SELECT k.ZADMITARBEITER AS admidj,
             SUM(p.ZSOLLFRACHT) AS sollfrachtJ,
             SUM(p.ZHANDLING) AS handlingJ
      FROM ZPBSROW p
      LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
      WHERE ZJAHR=2013
        AND ZMONAT>=1
        AND ZMONAT<=6
      GROUP BY k.ZADMITARBEITER) j ON vj.admidvj = j.admidj) t0,
  (SELECT vj.*,
          j.*
   FROM
     (SELECT k.ZADMITARBEITER AS admidvj,
             SUM(vk.ZNETTO) AS summeVJ,
             SUM(vk.ZDB_BASIS) AS summeDBVJ
      FROM ZPBSROW p
      LEFT JOIN ZWARENGRUPPEVK vk ON vk.ZPBSROW=p.Z_PK
      LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
      WHERE ZJAHR=2012
        AND ZMONAT>=1
        AND ZMONAT<=6
      GROUP BY k.ZADMITARBEITER) vj
   LEFT JOIN
     (SELECT k.ZADMITARBEITER AS admidj,
             SUM(vk.ZNETTO) AS summeJ,
             SUM(vk.ZDB_BASIS) AS summeDBJ
      FROM ZPBSROW p
      LEFT JOIN ZWARENGRUPPEVK vk ON vk.ZPBSROW=p.Z_PK
      LEFT JOIN ZKUNDE k ON k.Z_PK=p.ZKUNDE
      WHERE ZJAHR=2013
        AND ZMONAT>=1
        AND ZMONAT<=6
      GROUP BY k.ZADMITARBEITER) j ON vj.admidvj = j.admidj) t1
WHERE t0.admidvj=t1.admidvj
  AND t0.admidvj=adm.Z_PK

在此处输入图像描述

4

1 回答 1

2

连接的问题是所有东西都连接在一起。您应该改用独立的标量子查询:

SELECT name,
       (SELECT SUM(WarengruppeVK.netto)
        FROM Kunde
        JOIN PbsRow ON Kunde.PK = PbsRow.kunde
        JOIN WarengruppeVK ON PbsRow.PK = WarengruppeVK.pbsrow
        WHERE Kunde.admitarbeiter = Adm.PK
          AND PbsRow.jahr = 2012
       ) AS vj_netto,
       (SELECT SUM(PbsRow.sollfracht)
        FROM Kunde
        JOIN PbsRow ON Kunde.PK = PbsRow.kunde
        WHERE Kunde.admitarbeiter = Adm.PK
          AND PbsRow.jahr = 2012
       ) AS vj_sollfracht
       (SELECT SUM(WarengruppeVK.netto)
        FROM Kunde
        JOIN PbsRow ON Kunde.PK = PbsRow.kunde
        JOIN WarengruppeVK ON PbsRow.PK = WarengruppeVK.pbsrow
        WHERE Kunde.admitarbeiter = Adm.PK
          AND PbsRow.jahr = 2013
       ) AS j_netto,
       (SELECT SUM(PbsRow.sollfracht)
        FROM Kunde
        JOIN PbsRow ON Kunde.PK = PbsRow.kunde
        WHERE Kunde.admitarbeiter = Adm.PK
          AND PbsRow.jahr = 2013
       ) AS j_sollfracht
FROM Adm
于 2013-10-16T10:29:20.727 回答