我正在使用TOAD for Oracle。虽然我实现了一些SQL查询,但我遇到了这些问题:
我正在使用他们每个人都有的几张桌子。选择查询的10M行。 2个表有超过70M行数据。
让我说我有;
TRANSACTION表(prim.key:SQ_TRANSACTION_ID)
TRANSACTION_DETAIL表(外键:RF_TRANSACTION_ID, RF_PRODUCT_ID)
PRODUCT表(prim.key:SQ_PRODUCT_ID)
我的选择查询就像;
SELECT TR.TRANSACTION_ID,
SUM(CASE WHEN PR.CD_PRODCUT_TYPE = 'A'
THEN TRD.CS_INVOICE_PRICE ELSE 0 END) A_PRODUCT_TOTAL,
SUM(CASE WHEN PR.CD_PRODCUT_TYPE <> 'A'
THEN TRD.CS_INVOICE_PRICE ELSE 0 END) B_PRODUCT_TOTAL
FROM TRANSACTION TR,
TRANSACTION_DETAIL TRD,
PRODUCT PR
WHERE TR.SQ_TRANSACTION_ID = TRD.RF_TRANSACTION_ID
AND TRD.RF_PRODUCT_ID = PR.SQ_PRODUCT_ID
GROUP BY TR.TRANSACTION_ID,
CASE WHEN PR.CD_PRODCUT_TYPE = 'A' THEN TRD.CS_INVOICE_PRICE ELSE 0 END,
CASE WHEN PR.CD_PRODCUT_TYPE <> 'A' THEN TRD.CS_INVOICE_PRICE ELSE 0 END
有没有办法将此查询拆分为两个或多个部分,并使用其外键/主键互相引用?我的意思是分成两部分,第一部分取A_PRODUCT_TOTAL,第二部分取B_PRODUCT_TOTAL。每个部分的交易ID应与结果数据匹配。
答案 0 :(得分:0)
您的查询的直接翻译将是:
SELECT TR.TRANSACTION_ID, SUM(TRD.CS_INVOICE_PRICE) A_PRODUCT_TOTAL
FROM TRANSACTION TR join
TRANSACTION_DETAIL TRD
on TR.SQ_TRANSACTION_ID = TRD.RF_TRANSACTION_ID join
PRODUCT PR
on TRD.RF_PRODUCT_ID = PR.SQ_PRODUCT_ID
WHERE PR.CD_PRODCUT_TYPE = 'A'
GROUP BY TR.TRANSACTION_ID,
CASE WHEN PR.CD_PRODCUT_TYPE = 'A' THEN TRD.CS_INVOICE_PRICE ELSE 0 END
但是,我怀疑你不想要group by
中的第二个条款,因为每个交易将被拆分为发票价格相同的reows:
SELECT TR.TRANSACTION_ID, SUM(TRD.CS_INVOICE_PRICE) A_PRODUCT_TOTAL
FROM TRANSACTION TR join
TRANSACTION_DETAIL TRD
on TR.SQ_TRANSACTION_ID = TRD.RF_TRANSACTION_ID join
PRODUCT PR
on TRD.RF_PRODUCT_ID = PR.SQ_PRODUCT_ID
WHERE PR.CD_PRODCUT_TYPE = 'A'
GROUP BY TR.TRANSACTION_ID;
'B'
的查询类似。