0
SQL引擎:MSSQL如何避免笛卡爾乘積當我們總結(從DIF兩種不同的列表)
目標:合併兩個不同的總和
這是我的查詢:
SELECT z.zlec_id AS zlec,
(
CASE WHEN Sum(netto_blind_discout * p.count) IS NOT NULL
THEN
Sum(netto_blind_discout * p.count)
ELSE
0
END
+
CASE WHEN Sum(netto2 * d.count) IS NOT NULL
THEN
Sum(netto2 * d.count)
ELSE
0
END
) AS res,
Sum(netto_blind_discout * p.count),
Sum(netto2 * d.count)
FROM zetter z
FULL OUTER JOIN
(
SELECT netto_blind_discout,
count,
zlec_id
FROM position
) AS p
ON z.zlec_id = p.zlec_id
FULL OUTER JOIN
(
SELECT netto2,
count,
zlec_id
FROM d_additional
) AS d
ON z.zlec_id = d.zlec_id
WHERE z.zlec_id = 123123
GROUP BY z.zlec_id
如何避免笛卡爾產品第一次加入和第二次? 它的產生,我奇怪的結果:
最終的結果是
(SUM of product * number_of_rows in d_additional) + (SUM of d_additional * number_of_rows in product), (SUM of product * number_of_rows in d_additional), (SUM of d_additional * number_of_rows in product)
哪裏是我的錯嗎?
我用隨機SQL格式來格式化您的查詢。它仍然是可怕的,但比你發佈時還要少... – Sebas
@Sebas,修正了語法sql-a –