我正在编写一个查询,我正在计算 2 个不同表的总和,这些表具有外键约束 (1:n)。
所以有一张桌子Kunde
,里面有客户。每个Customer
都由Adm
. 每个Kunde
交易都有 N 个不同的交易(PbsRow
),而每个交易包含 N 个不同的产品(WarengruppeVK
)。每笔交易都有一个月和一年(Monat
和Jahr
)
我需要的是包含以下信息的结果:1)Adm 的名称,2)属于 2013 年该特定 Adm 的客户之一的所有和值的总和,3)属于的所有sollfracht
和值的总和该客户在 2013 年的其中一个,4 和 5) 与 2) 和 3) 仅在 2012 年相同handling
netto
db_basis
PbsRow
我已经尝试过不同的事情,但是当我使用下一个表进行联接时,我总是最终得到一个带有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 时,sollfracht
andhandling
值是正确的。
提前致谢。
编辑:这里有一些示例。
这是我从上面的查询中得到的结果:
这是我删除第一个连接时的结果:
您会注意到这一点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 个值。我上面结果的第一个屏幕截图包含正确的、和值,但不正确的、和值。summeVJ
summeDBJ
summeDBVJ
summeJ
summeVJ
summeDBJ
summeDBVJ
handlingJ
handlingVJ
sollfrachtJ
sollfrachtVJ
编辑 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