我想显示结果,其中表中存储的日期不在查询中指定的日期之间。
如果last_Tran_date != from_date
如果last_Tran_date != to_date
因此没有交易。
所以我想显示结果。
示例 最后交易日期
1-JAN-16
2-JAN-16
8-FEB-16
10-MAC-16
PERIOD TO QUERY :( FROM 2-JAN-16 TO 8-FEB-16
)
如果上次交易日期不在期间查询之间, 然后显示结果。
SELECT L.TDR_CODE||' - '||T.TDR_NAME TDR_CODE,L.CLIENT_NO,L.CLIENT_TYPE
,L.AMLA_RISK,L.ACCT_TYPE,L.CLIENT_NAME,L.DATE_CREATED,L.ANNUAL_INCOME
,L.NET_WORTH,L.ACCT_GROUP,L.PAIDUP_CAPITAL,L.SHAREHOLDER_FUND,L.OCCUPATION
,L.LAST_TRAN_DATE,K.CHQ_BANK,K.CHQ_NO,K.CHQ_AMT,decode(K.category,'3'
, decode(nvl(K.cancel_flag,'N'),'N',1,-2) ,0) chqamt_cash
FROM BOS_M_CLIENT L
, BOS_M_TRADER T,BOS_M_LEDGER_REC K
WHERE ((K.CHQ_NO IS NOT NULL AND K.CHQ_AMT>50000)
OR (K.CATEGORY='3' AND K.CHQ_AMT>10000))
AND L.PROHIBIT_TRADE<>'C'
AND L.CLIENT_NO = K.CLIENT_NO(+)
AND L.amla_risk='High'
AND L.TDR_CODE=T.TDR_CODE
AND L.tdr_code>=:P_FROM_TDR_CODE
AND L.tdr_code<=:P_TO_TDR_CODE
AND K.TRAN_DATE>=:P_FROM_DATE
AND K.TRAN_DATE<=:P_TO_DATE
AND L.LAST_TRAN_DATE NOT BETWEEN :P_FROM_DATE AND :P_TO_DATE
答案 0 :(得分:0)
如果有&#34; 间隙&#34;在您的数据中,SQL将不会显示缺失的数据,除非您执行额外的操作。 e.g。
trans_date
2016-01-01
-- there is a "gap" here, there are "missing dates"
2016-01-12
您现在需要的是1月1日至1月12日期间每个日期的一组行。有很多方法可以获得这些行,下面我使用过&#34; connect by leve;&#34;这是Oracle特定的技术,演示了我们如何找到&#34;缺少日期&#34;:
CREATE TABLE YOURTABLE (TRANS_DATE date);
INSERT INTO YOURTABLE (TRANS_DATE) VALUES (to_date('2016-01-01','yyyy-mm-dd')); INSERT INTO YOURTABLE (TRANS_DATE) VALUES (to_date('2016-01-12','yyyy-mm-dd'));
2 rows affected
SELECT c.cal_date, t.* FROM ( SELECT to_date('2016-01-01','yyyy-mm-dd') + ROWNUM - 1 as cal_date FROM ( SELECT ROWNUM FROM ( SELECT 1 FROM DUAL CONNECT BY LEVEL <= (to_date('2016-01-12','yyyy-mm-dd') - (to_date('2016-01-01','yyyy-mm-dd')-1)) ) ) ) c LEFT JOIN yourtable t ON c.cal_date = t.trans_date WHERE t.trans_date IS NOT NULL ORDER BY c.cal_date ;
CAL_DATE | TRANS_DATE :-------- | :--------- 01-JAN-16 | 01-JAN-16 12-JAN-16 | 12-JAN-16
SELECT c.cal_date, t.* FROM ( SELECT to_date('2016-01-01','yyyy-mm-dd') + ROWNUM - 1 as cal_date FROM ( SELECT ROWNUM FROM ( SELECT 1 FROM DUAL CONNECT BY LEVEL <= (to_date('2016-01-12','yyyy-mm-dd') - (to_date('2016-01-01','yyyy-mm-dd')-1)) ) ) ) c LEFT JOIN yourtable t ON c.cal_date = t.trans_date WHERE t.trans_date IS NULL ORDER BY c.cal_date ;
CAL_DATE | TRANS_DATE :-------- | :--------- 02-JAN-16 | null 03-JAN-16 | null 04-JAN-16 | null 05-JAN-16 | null 06-JAN-16 | null 07-JAN-16 | null 08-JAN-16 | null 09-JAN-16 | null 10-JAN-16 | null 11-JAN-16 | null
dbfiddle here