I need a query to insert rows into a table within the last N days from today.
Insert into Table select 'xpto', name
from users where login_date between
TO_DATE(:DATE || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS')
and TO_DATE(:DATE || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS')
I need this :DATE to be represented by last 30 days... (each day).. So it would be 30 inserts.
How can I do that?
EDIT
Lets say I want to insert data from the last 30 days... So we have:
01/18/2016
01/19/2016
01/20/2016
...
02/01/2016
02/02/2016
02/03/2016
....
02/15/2016
....
02/22/2016
I want a single query - or statement - to insert my data based on each days above like this:
Insert into Table select 'xpto', name
from users where login_date between
TO_DATE('01/15/2016' || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS')
and TO_DATE('01/15/2016' || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS')
--- another insert
Insert into Table select 'xpto', name
from users where login_date between
TO_DATE('01/16/2016' || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS')
and TO_DATE('01/16/2016' || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS')
I don't want to do one query for each day...
ANOTHER EDIT
I'm sorry I just got my work notebook here...here is the real sample:
BEGIN
for day in (SELECT to_char(TO_DATE (SYSDATE, 'dd/mm/yyyy')-30 + LEVEL) AS DATE_CHECK
FROM DUAL
CONNECT BY SYSDATE - 30 + LEVEL <= SYSDATE)
LOOP
v_date := to_char(day.date_check);
INSERT INTO resume (date_check, type, total)
SELECT v_data AS DATA, type, COUNT (*) total
FROM ( select ....
from table
WHERE DATE_COLUMN BETWEEN TO_DATE(v_data ||' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and TO_DATE(v_date || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
union
select ....
from table
WHERE DATE_COLUMN BETWEEN TO_DATE(v_data ||' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and TO_DATE(v_date || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
)
end loop;
end;
but it doesn't work... if I do the same insert manually replacing v_date by any date (14/02/2016) it works....
:(
If I understand what you're trying to do correctly the following might serve:
Insert into Table
select 'xpto', name
from users
where login_date between TRUNC(SYSDATE) - INTERVAL '30' DAY
and TRUNC(SYSDATE) + INTERVAL '1' DAY - INTERVAL '1' SECOND
Based on the edit to the question it appears that we can just expand the range, as in:
Insert into Table
select 'xpto', name
from users
where login_date between TO_DATE('01/18/2016', 'MM/DD/YYYY')
and TO_DATE('02/22/2016', 'MM/DD/YYYY') + INTERVAL '1' DAY - INTERVAL '1' SECOND
Thank you for clarifying. Perhaps the following will help:
BEGIN
for day in (SELECT TRUNC(SYSDATE)-30 + LEVEL AS DATE_CHECK
FROM DUAL
CONNECT BY TRUNC(SYSDATE) - 30 + LEVEL <= TRUNC(SYSDATE))
LOOP
INSERT INTO resume (date_check, type, total)
SELECT day.DATE_CHECK AS DATA, type, COUNT (*) total
FROM (select ....
from table
WHERE DATE_COLUMN = TRUNC(day.DATE_CHECK));
end loop;
end;
Best of luck.
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.