我有两张表如下:
loy_credits_ledger
+-----------------+------------------+-----------------+-----------------+ | CRL_OUTLET_CODE | CRL_REDEEM_MONTH | crl_redeem_year | CRL_RM_REDEEMED | +-----------------+------------------+-----------------+-----------------+ | 144000581 | APR | 2014 | 273.3 | | 144000581 | JUL | 2014 | 194.1 | | 144000581 | OCT | 2014 | 216.3 | | 144000581 | JAN | 2015 | 24.9 | | 144000581 | JAN | 2015 | 177.1 | +-----------------+------------------+-----------------+-----------------+
jti_qtr_credit_data
+-----------------+----------+-------------+------------+------------+---------------------+ | QCD_OUTLET_CODE | QCD_YEAR | QCD_QUARTER | QCD_CREDIT | QCD_POINTS | QCD_CREDITS_BALANCE | +-----------------+----------+-------------+------------+------------+---------------------+ | 144000581 | 2014 | Q1 | 273.3 | 54660 | 0 | | 144000581 | 2014 | Q2 | 194.1 | 38820 | 0 | | 144000581 | 2014 | Q3 | 378.8 | 75760 | 0 | | 144000581 | 2014 | Q4 | 202 | 40400 | 0 | | 144000581 | 2015 | Q1 | 321.55 | 64310 | 0 | +-----------------+----------+-------------+------------+------------+---------------------+
我需要在某些条件下加入这两个表。除了凌乱的桌子结构(我的控制之外),我设法让我的结果几乎正确。基本上,这是我的目标:
+-----------------+----------+-------------+------------+------------+--------------+---------------------+ | QCD_OUTLET_CODE | QCD_YEAR | QCD_QUARTER | QCD_POINTS | QCD_CREDIT | QCD_REDEEMED | QCD_CREDITS_BALANCE | +-----------------+----------+-------------+------------+------------+--------------+---------------------+ | 144000581 | 2014 | Q2 | 38820 | 194.1 | 194.1 | 0 | | 144000581 | 2014 | Q4 | 40400 | 202 | 202 | 0 | | 144000581 | 2014 | Q1 | 54660 | 273.3 | 273.3 | 0 | | 144000581 | 2014 | Q3 | 75760 | 378.8 | 216.3 | 162.5 | +-----------------+----------+-------------+------------+------------+--------------+---------------------+
它只缺少第五条记录,只出现在 jti_qtr_credit_data 中。
我使用脚本做到了:
SELECT Q.QCD_OUTLET_CODE, Q.QCD_YEAR, Q.QCD_QUARTER, Q.QCD_POINTS, Q.QCD_CREDIT, nvl(SUM(L.REDEEMED), 0) AS QCD_REDEEMED, Q.QCD_CREDIT - NVL(SUM(L.REDEEMED),0) AS QCD_CREDITS_BALANCE
FROM
(SELECT
(case when CRL_REDEEM_MONTH = 'JAN' Then 'Q4'
when CRL_REDEEM_MONTH = 'FEB' Then 'Q4'
when CRL_REDEEM_MONTH = 'MAR' Then 'Q4'
when CRL_REDEEM_MONTH = 'APR' Then 'Q1'
when CRL_REDEEM_MONTH = 'MAY' Then 'Q1'
when CRL_REDEEM_MONTH = 'JUN' Then 'Q1'
when CRL_REDEEM_MONTH = 'JUL' Then 'Q2'
when CRL_REDEEM_MONTH = 'AUG' Then 'Q2'
when CRL_REDEEM_MONTH = 'SEP' Then 'Q2'
when CRL_REDEEM_MONTH = 'OCT' Then 'Q3'
when CRL_REDEEM_MONTH = 'NOV' Then 'Q3'
when CRL_REDEEM_MONTH = 'DEC' Then 'Q3'
ELSE NULL END) QUARTER,
(case
when CRL_REDEEM_MONTH = 'JAN' Then crl_redeem_year-1
when CRL_REDEEM_MONTH = 'FEB' Then crl_redeem_year-1
when CRL_REDEEM_MONTH = 'MAR' Then crl_redeem_year-1
when CRL_REDEEM_MONTH = 'APR' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'MAY' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'JUN' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'JUL' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'AUG' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'SEP' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'OCT' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'NOV' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'DEC' Then crl_redeem_year
ELSE NULL END) redeem_year,
CRL_OUTLET_CODE, CRL_CREDITS_EARNED, NVL(CRL_RM_REDEEMED, 0) REDEEMED
FROM loy_credits_ledger
where TO_CHAR(crl_redeem_year) is not null
AND CRL_OUTLET_CODE = 144000581
--GROUP BY CRL_REDEEM_MONTH, crl_redeem_year, CRL_OUTLET_CODE, CRL_CREDITS_EARNED
) L
full outer JOIN jti_qtr_credit_data Q
ON L.CRL_OUTLET_CODE = Q.QCD_OUTLET_CODE
AND L.redeem_year = Q.QCD_YEAR
AND L.QUARTER = Q.QCD_QUARTER
AND L.CRL_OUTLET_CODE = 144000581
GROUP BY Q.QCD_OUTLET_CODE, Q.QCD_YEAR, Q.QCD_QUARTER, Q.QCD_YEAR, Q.QCD_CREDIT, Q.QCD_POINTS;
现在,问题是,我尝试左和右加入。因为我有QCD_Quarter = Q1& QCD_Year = 2015 jti_qtr_credit_data ,但不在 loy_credits_ledger ;左边还是右边连接不应该显示空值的记录?
此问题是否与使用join的子查询有关?
答案 0 :(得分:0)
在正常的 WHERE 条件中使用(+)符号告诉DB包含所有不匹配的行,其中包含NULL值;我设法得到了理想的结果。
以下是修改后的脚本:
SELECT Q.QCD_OUTLET_CODE, Q.QCD_YEAR, Q.QCD_QUARTER, Q.QCD_POINTS, Q.QCD_CREDIT, nvl(SUM(L.REDEEMED), 0) AS QCD_REDEEMED, Q.QCD_CREDIT - NVL(SUM(L.REDEEMED),0) AS QCD_CREDITS_BALANCE
FROM
(SELECT
(case when CRL_REDEEM_MONTH = 'JAN' Then 'Q4'
when CRL_REDEEM_MONTH = 'FEB' Then 'Q4'
when CRL_REDEEM_MONTH = 'MAR' Then 'Q4'
when CRL_REDEEM_MONTH = 'APR' Then 'Q1'
when CRL_REDEEM_MONTH = 'MAY' Then 'Q1'
when CRL_REDEEM_MONTH = 'JUN' Then 'Q1'
when CRL_REDEEM_MONTH = 'JUL' Then 'Q2'
when CRL_REDEEM_MONTH = 'AUG' Then 'Q2'
when CRL_REDEEM_MONTH = 'SEP' Then 'Q2'
when CRL_REDEEM_MONTH = 'OCT' Then 'Q3'
when CRL_REDEEM_MONTH = 'NOV' Then 'Q3'
when CRL_REDEEM_MONTH = 'DEC' Then 'Q3'
ELSE NULL END) QUARTER,
(case
when CRL_REDEEM_MONTH = 'JAN' Then crl_redeem_year-1
when CRL_REDEEM_MONTH = 'FEB' Then crl_redeem_year-1
when CRL_REDEEM_MONTH = 'MAR' Then crl_redeem_year-1
when CRL_REDEEM_MONTH = 'APR' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'MAY' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'JUN' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'JUL' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'AUG' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'SEP' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'OCT' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'NOV' Then crl_redeem_year
when CRL_REDEEM_MONTH = 'DEC' Then crl_redeem_year
ELSE NULL END) redeem_year,
CRL_OUTLET_CODE, CRL_CREDITS_EARNED, NVL(CRL_RM_REDEEMED, 0) REDEEMED
FROM loy_credits_ledger
where TO_CHAR(crl_redeem_year) is not null
AND CRL_OUTLET_CODE = 144000581
--GROUP BY CRL_REDEEM_MONTH, crl_redeem_year, CRL_OUTLET_CODE, CRL_CREDITS_EARNED
) L
,
jti_qtr_credit_data q
where L.CRL_OUTLET_CODE(+) = Q.QCD_OUTLET_CODE
AND L.redeem_year(+) = Q.QCD_YEAR
AND L.QUARTER(+) = Q.QCD_QUARTER
and q.QCD_OUTLET_CODE = 144000581
GROUP BY Q.QCD_OUTLET_CODE, Q.QCD_YEAR, Q.QCD_QUARTER, Q.QCD_YEAR, Q.QCD_CREDIT, Q.QCD_POINTS;
结果
+-----------------+----------+-------------+------------+------------+--------------+---------------------+ | QCD_OUTLET_CODE | QCD_YEAR | QCD_QUARTER | QCD_POINTS | QCD_CREDIT | QCD_REDEEMED | QCD_CREDITS_BALANCE | +-----------------+----------+-------------+------------+------------+--------------+---------------------+ | 144000581 | 2014 | Q2 | 38820 | 194.1 | 194.1 | 0 | | 144000581 | 2014 | Q4 | 40400 | 202 | 202 | 0 | | 144000581 | 2014 | Q1 | 54660 | 273.3 | 273.3 | 0 | | 144000581 | 2014 | Q3 | 75760 | 378.8 | 216.3 | 162.5 | | 144000581 | 2015 | Q1 | 64310 | 321.55 | 0 | 321.55 | +-----------------+----------+-------------+------------+------------+--------------+---------------------+