由于数据未正确获取,以下外连接的使用是否正确
Is the below use of outer join correct as the data doesn't fetch correctly
您好,我在 table 中有以下数据
Exsiting data
通过以下查询,我只得到普通凭证
Output
但是我还需要没有具有相应 -ve 的优惠券的行(它基本上是销售优惠券和兑换优惠券。)
因此只有 1 张优惠券已售出和兑换,还有一张尚未兑换。
我如何恢复尚未兑换的行。
SELECT t1.STORE,
t1.DAY,
t1.tran_seq_no,
t2.tran_seq_no,
t2.DAY,
t1.tender_amt,
t2.tender_amt,
t1.voucher_no,
t2.voucher_no
FROM sa_tran_tender t1,
sa_tran_tender t2
where t1.voucher_no(+)=t2.voucher_no
AND t1.DAY=4
AND t1.tender_amt<0
AND t1.tender_type_group ='VOUCH'
AND t2.tender_amt>0
AND t1.STORE=400112
AND t1.tran_seq_no IN(2632729010,2632729056,3140772029)
AND t1.STORE(+)=t2.STORE
and t1.tender_type_group(+)=t2.tender_type_group;
当您使用 oracle joun 语法时,您应该将 (+)
添加到左连接 table 的 WHERE
条件中的每个字段(不仅适用于与另一个 table) -
where t1.voucher_no(+)=t2.voucher_no
AND t1.DAY=4(+)
AND t1.tender_amt(+)<0
AND t1.tender_type_group(+) ='VOUCH'
AND t2.tender_amt>0
AND t1.STORE(+)=400112
AND t1.tran_seq_no(+) IN(2632729010,2632729056,3140772029)
AND t1.STORE(+)=t2.STORE
and t1.tender_type_group(+)=t2.tender_type_group
或者您可以使用 ANSI 连接。
假设将进行一次代金券销售(负数 tender_amt
),然后进行一次可选的代金券兑换(正数 tender_amt
和稍后的 tran_seq_no
),然后您不需要使用自连接;相反,您可以使用 LAG
或 LEAD
分析函数(与自连接的两次 table 扫描相比,只需要一次 table 扫描):
Oracle 11g R2 模式设置:
CREATE TABLE sa_tran_tender ( store, day, tran_seq_no, voucher_no, tender_amt, tender_type_group ) AS
SELECT 400112, 4, 2632729010, '400112*101*21090', -39.95, 'VOUCH' FROM DUAL UNION ALL
SELECT 400112, 4, 2632729056, '400112*101*21136', -6.99, 'VOUCH' FROM DUAL UNION ALL
SELECT 400112, 5, 3140772029, '400112*101*21090', 39.95, 'VOUCH' FROM DUAL;
查询 1:
SELECT *
FROM (
SELECT t.*,
LEAD( DAY ) OVER ( PARTITION BY voucher_no, store, tender_type_group ORDER BY tran_seq_no ) AS redeemed_day,
LEAD( tender_amt ) OVER ( PARTITION BY voucher_no, store, tender_type_group ORDER BY tran_seq_no ) AS redeemed_amt,
LEAD( tran_seq_no ) OVER ( PARTITION BY voucher_no, store, tender_type_group ORDER BY tran_seq_no ) AS redeemed_tran_seq_no
FROM sa_tran_tender t
)
WHERE tran_seq_no IN( 2632729010, 2632729056, 3140772029)
AND tender_type_group ='VOUCH'
AND STORE =400112
AND DAY =4
| STORE | DAY | TRAN_SEQ_NO | VOUCHER_NO | TENDER_AMT | TENDER_TYPE_GROUP | REDEEMED_DAY | REDEEMED_AMT | REDEEMED_TRAN_SEQ_NO |
|--------|-----|-------------|------------------|------------|-------------------|--------------|--------------|----------------------|
| 400112 | 4 | 2632729010 | 400112*101*21090 | -39.95 | VOUCH | 5 | 39.95 | 3140772029 |
| 400112 | 4 | 2632729056 | 400112*101*21136 | -6.99 | VOUCH | (null) | (null) | (null) |
但是,如果您确实想使用(低效的)自连接,那么您可以这样做:
SELECT t1.STORE,
t1.DAY,
t1.tran_seq_no,
t2.tran_seq_no,
t2.DAY,
t1.tender_amt,
t2.tender_amt,
t1.voucher_no,
t2.voucher_no
FROM sa_tran_tender t1
LEFT OUTER JOIN
sa_tran_tender t2
ON ( t1.voucher_no = t2.voucher_no
AND t2.tender_amt > 0
AND t1.store = t2.store
AND t1.tender_type_group = t2.tender_type_group )
WHERE t1.DAY = 4
AND t1.tender_amt < 0
AND t1.tender_type_group = 'VOUCH'
AND t1.STORE = 400112
AND t1.tran_seq_no IN (2632729010,2632729056,3140772029);
您好,我在 table 中有以下数据 Exsiting data 通过以下查询,我只得到普通凭证 Output
但是我还需要没有具有相应 -ve 的优惠券的行(它基本上是销售优惠券和兑换优惠券。)
因此只有 1 张优惠券已售出和兑换,还有一张尚未兑换。 我如何恢复尚未兑换的行。
SELECT t1.STORE,
t1.DAY,
t1.tran_seq_no,
t2.tran_seq_no,
t2.DAY,
t1.tender_amt,
t2.tender_amt,
t1.voucher_no,
t2.voucher_no
FROM sa_tran_tender t1,
sa_tran_tender t2
where t1.voucher_no(+)=t2.voucher_no
AND t1.DAY=4
AND t1.tender_amt<0
AND t1.tender_type_group ='VOUCH'
AND t2.tender_amt>0
AND t1.STORE=400112
AND t1.tran_seq_no IN(2632729010,2632729056,3140772029)
AND t1.STORE(+)=t2.STORE
and t1.tender_type_group(+)=t2.tender_type_group;
当您使用 oracle joun 语法时,您应该将 (+)
添加到左连接 table 的 WHERE
条件中的每个字段(不仅适用于与另一个 table) -
where t1.voucher_no(+)=t2.voucher_no
AND t1.DAY=4(+)
AND t1.tender_amt(+)<0
AND t1.tender_type_group(+) ='VOUCH'
AND t2.tender_amt>0
AND t1.STORE(+)=400112
AND t1.tran_seq_no(+) IN(2632729010,2632729056,3140772029)
AND t1.STORE(+)=t2.STORE
and t1.tender_type_group(+)=t2.tender_type_group
或者您可以使用 ANSI 连接。
假设将进行一次代金券销售(负数 tender_amt
),然后进行一次可选的代金券兑换(正数 tender_amt
和稍后的 tran_seq_no
),然后您不需要使用自连接;相反,您可以使用 LAG
或 LEAD
分析函数(与自连接的两次 table 扫描相比,只需要一次 table 扫描):
Oracle 11g R2 模式设置:
CREATE TABLE sa_tran_tender ( store, day, tran_seq_no, voucher_no, tender_amt, tender_type_group ) AS
SELECT 400112, 4, 2632729010, '400112*101*21090', -39.95, 'VOUCH' FROM DUAL UNION ALL
SELECT 400112, 4, 2632729056, '400112*101*21136', -6.99, 'VOUCH' FROM DUAL UNION ALL
SELECT 400112, 5, 3140772029, '400112*101*21090', 39.95, 'VOUCH' FROM DUAL;
查询 1:
SELECT *
FROM (
SELECT t.*,
LEAD( DAY ) OVER ( PARTITION BY voucher_no, store, tender_type_group ORDER BY tran_seq_no ) AS redeemed_day,
LEAD( tender_amt ) OVER ( PARTITION BY voucher_no, store, tender_type_group ORDER BY tran_seq_no ) AS redeemed_amt,
LEAD( tran_seq_no ) OVER ( PARTITION BY voucher_no, store, tender_type_group ORDER BY tran_seq_no ) AS redeemed_tran_seq_no
FROM sa_tran_tender t
)
WHERE tran_seq_no IN( 2632729010, 2632729056, 3140772029)
AND tender_type_group ='VOUCH'
AND STORE =400112
AND DAY =4
| STORE | DAY | TRAN_SEQ_NO | VOUCHER_NO | TENDER_AMT | TENDER_TYPE_GROUP | REDEEMED_DAY | REDEEMED_AMT | REDEEMED_TRAN_SEQ_NO |
|--------|-----|-------------|------------------|------------|-------------------|--------------|--------------|----------------------|
| 400112 | 4 | 2632729010 | 400112*101*21090 | -39.95 | VOUCH | 5 | 39.95 | 3140772029 |
| 400112 | 4 | 2632729056 | 400112*101*21136 | -6.99 | VOUCH | (null) | (null) | (null) |
但是,如果您确实想使用(低效的)自连接,那么您可以这样做:
SELECT t1.STORE,
t1.DAY,
t1.tran_seq_no,
t2.tran_seq_no,
t2.DAY,
t1.tender_amt,
t2.tender_amt,
t1.voucher_no,
t2.voucher_no
FROM sa_tran_tender t1
LEFT OUTER JOIN
sa_tran_tender t2
ON ( t1.voucher_no = t2.voucher_no
AND t2.tender_amt > 0
AND t1.store = t2.store
AND t1.tender_type_group = t2.tender_type_group )
WHERE t1.DAY = 4
AND t1.tender_amt < 0
AND t1.tender_type_group = 'VOUCH'
AND t1.STORE = 400112
AND t1.tran_seq_no IN (2632729010,2632729056,3140772029);