伙计们,我有一个在oracle 10.2中运行两个表的代码。这些表有大量数据,在使用和存在函数时,需要很长时间才能给出答案。我在下面的代码中做错了什么。
SELECT 'Total' nationality,
SUM(
CASE
WHEN new_store_code IN ('40022', '40041', '40021', '40023','40074')
THEN amount
END
) cy_all_sale
FROM txn_2017_18 a
WHERE (CONCEPT) = 'Home center'
AND invoice_date <= (select To_DT from Data_Date)
And Department_Code in ('306','307','308','309','310','311','312','313','314','315','318')
AND EXISTS (
SELECT 1
FROM txn_2016_17 b
WHERE a.member_card_number = b.member_card_number
AND (b.CONCEPT) = 'Home center'
AND invoice_date <= (select LY_To_DT from Data_Date)
And Department_Code in '306','307','308','309','310','311','312','313','314','315','318')
AND (a.CONCEPT) = (b.CONCEPT)
and A.New_Store_Code=B.New_Store_Code
)
答案 0 :(得分:0)
我认为你可以尝试这种方法,使用2 sub select
,每个来自txn_2017_18
&amp; txn_2016_17
,然后JOIN
将它们放在一起,如下所示。但首先要确保您的表根据查询条件正确编制索引。
SELECT
'Total' nationality,
SUM(amount) cy_all_sale
FROM
(
SELECT * FROM txn_2017_18
WHERE
new_store_code IN ('40022', '40041', '40021', '40023', '40074')
AND CONCEPT = 'Home center'
AND invoice_date <= (select To_DT from Data_Date)
AND Department_Code in ('306','307','308','309','310','311','312','313','314','315','318')
) a
JOIN
(
SELECT * FROM txn_2016_17
WHERE
new_store_code IN ('40022', '40041', '40021', '40023', '40074')
AND CONCEPT = 'Home center'
AND invoice_date <= (select LY_To_DT from Data_Date)
And Department_Code in '306','307','308','309','310','311','312','313','314','315','318')
) b
ON a.member_card_number = b.member_card_number
AND a.New_Store_Code = b.New_Store_Code
AND a.CONCEPT = b.CONCEPT