I have a Pl/SQL query that pull all records from the 3 tables. That is good. Now I want pull last 24 updated records from 2 table (tbl_constit, tbl_email).
See below actual query
SELECT DISTINCT c.constit_id AS constitid,
REPLACE (c.in_labelname, 'None', '') AS fullname,
c.firstname AS firstname, c.lastname AS lastname,
c.indiv_title AS title, e.e_addr AS email,
'InActive' AS status
FROM tbl_constit cn, tbl_email e,tbl_catcod s
WHERE c.constit_id = e.constit_id
AND c.constit_id = s.constit_id(+)
AND e.e_type = 'EMAIL'
AND e.e_default = '1'
AND s.cat_code IN ('SPEMU', 'SPENM')
ORDER BY c.constit_id;
tables tbl_constit cn, tbl_email e has 'CHGD_DT' field.
This date field change when record is updated. Now How can I pull last 24 updated records from tbl_constit cn or tbl_email by using 'CHGD_DT' field?
Change can happen any one of two tables.
SELECT DISTINCT c.constit_id AS constitid,
REPLACE (c.in_labelname, 'None', '') AS fullname,
c.firstname AS firstname, c.lastname AS lastname,
c.indiv_title AS title, e.e_addr AS email,
'InActive' AS status
FROM tbl_constit cn, tbl_email e,tbl_catcod s
WHERE c.constit_id = e.constit_id
AND c.constit_id = s.constit_id(+)
AND e.e_type = 'EMAIL'
AND e.e_default = '1'
AND s.cat_code IN ('SPEMU', 'SPENM')
AND ((cn.chgd_dt > (SYSDATE - 1)) OR (e.chgd_dt > (SYSDATE - 1)))
ORDER BY c.constit_id;
This will show all rows where either cn or e was updated in the last 24 hours, if you want to show rows where both where updated, replace the inner OR with an AND.
What you want is SYSDATE - 1
, like this:
SELECT DISTINCT c.constit_id AS constitid,
REPLACE (c.in_labelname, 'None', '') AS fullname,
c.firstname AS firstname, c.lastname AS lastname,
c.indiv_title AS title, e.e_addr AS email,
'InActive' AS status
FROM tbl_constit cn, tbl_email e,tbl_catcod s
WHERE c.constit_id = e.constit_id
AND c.constit_id = s.constit_id(+)
AND e.e_type = 'EMAIL'
AND e.e_default = '1'
AND s.cat_code IN ('SPEMU', 'SPENM')
AND cn.CHGD_DT > SYSDATE - 1
ORDER BY c.constit_id;
That condition will get everything where CHGD_DT is greater then the date right now (minus 1 day, so yesterday).
Assuming you mean "rows that have changed in the last 24 hours", as your title implies and not "last 24 updated records" which implies that you want exactly 24 rows returned regardless of when they were changed, something like
SELECT DISTINCT c.constit_id AS constitid,
REPLACE (c.in_labelname, 'None', '') AS fullname,
c.firstname AS firstname, c.lastname AS lastname,
c.indiv_title AS title, e.e_addr AS email,
'InActive' AS status
FROM tbl_constit cn, tbl_email e,tbl_catcod s
WHERE c.constit_id = e.constit_id
AND c.constit_id = s.constit_id(+)
AND e.e_type = 'EMAIL'
AND e.e_default = '1'
AND s.cat_code IN ('SPEMU', 'SPENM')
AND greatest( cn.chgd_dt, e.chgd_dt ) > sysdate - interval '1' day
ORDER BY c.constit_id;
or
SELECT DISTINCT c.constit_id AS constitid,
REPLACE (c.in_labelname, 'None', '') AS fullname,
c.firstname AS firstname, c.lastname AS lastname,
c.indiv_title AS title, e.e_addr AS email,
'InActive' AS status
FROM tbl_constit cn, tbl_email e,tbl_catcod s
WHERE c.constit_id = e.constit_id
AND c.constit_id = s.constit_id(+)
AND e.e_type = 'EMAIL'
AND e.e_default = '1'
AND s.cat_code IN ('SPEMU', 'SPENM')
AND (cn.chgd_dt > sysdate - interval '1' day OR
e.chgd_dt > sysdate - interval '1' day)
ORDER BY c.constit_id;
The latter may be more efficient if CHGD_DT
is indexed in either table.
SELECT *
FROM
(
select DISTINCT
c.constit_id AS constitid,
REPLACE (c.in_labelname, 'None', '') AS fullname,
c.firstname AS firstname, c.lastname AS lastname,
c.indiv_title AS title, e.e_addr AS email,
'InActive' AS status
FROM tbl_constit cn, tbl_email e,tbl_catcod s
WHERE c.constit_id = e.constit_id
AND c.constit_id = s.constit_id(+)
AND e.e_type = 'EMAIL'
AND e.e_default = '1'
AND s.cat_code IN ('SPEMU', 'SPENM')
ORDER BY GREATEST (cn.CHGD_DT, e.CHGD_DT)
)
WHERE rownum <= 24
ORDER BY c.constit_id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.