Can’t do what you’re asking using just a simple query using SELECT
.
You need to map all rules involved and use the PL/SQL
to extract only the data you want.
Below is an example with explanatory comments on how to make an extract based on the information he gave:
CREATE GLOBAL TEMPORARY TABLE registros --tabela temporária para adicionar os registros a serem exibidos
( registro CHAR(1),
dtabertura DATE))
ON COMMIT PRESERVE ROWS;
begin
Declare Cursor cursorRegs is
WITH tabela_(registro, data) as ( --tabela criada apenas para montar o exemplo
select 'x', to_date('13/03/2017', 'DD/MM/YYYY') from dual union all
select 'y', to_date('14/03/2017', 'DD/MM/YYYY') from dual union all
select 'x', to_date('17/03/2017', 'DD/MM/YYYY') from dual union all
select 'x', to_date('21/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('13/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('19/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('25/03/2017', 'DD/MM/YYYY') from dual union all
select 'z', to_date('31/03/2017', 'DD/MM/YYYY') from dual )
select * from tabela_; --selecionado todos os dados da tabela
--declare aqui as váriaveis que você for utilizar
aData DATE;
oRegistroAtual VARCHAR2(50);
--Se necessário, adicione outras variáveis aqui
Begin
For reg in cursorRegs Loop --Para cada registro selecionado
--Mapeie toda a lógica envolvida e aqui filtre qual registro deve ou não ser inserido na tabela temporária
--Exemplo:
oData := reg.data;
oRegistroAtual := reg.registro;
Declare Cursor registrosAdicionar is
WITH tabela_(registro, data) as (
select 'x', to_date('13/03/2017', 'DD/MM/YYYY') from dual union all
select 'y', to_date('14/03/2017', 'DD/MM/YYYY') from dual union all
select 'x', to_date('17/03/2017', 'DD/MM/YYYY') from dual union all
select 'x', to_date('21/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('13/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('19/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('25/03/2017', 'DD/MM/YYYY') from dual union all
select 'z', to_date('31/03/2017', 'DD/MM/YYYY') from dual )
--selecionado apenas os registros que ocorreram entra data e data+7
select * from tabela_ where registro = oRegistroAtual and data between reg.data and reg.data+7;
For regAdd in registrosAdicionar Loop
--aqui será percorrido o segundo cursor adicionando os valores na tabela temporária
INSERT INTO registros values (regAdd.registro, regAdd.data);
--Talvez aqui você precise incrementar sua lógica, pois alguns dados serão inserido de forma repetida.
--Essa tabela é apenas um exemplo, mas no caso real você pode usar a PK da tabela e verificar se esse caso específico já existe na temp table
End Loop;
End Loop;
End;
end;
After executing the code make a select *
in the temporary table:
select * from registros;
Note that I have not tested this code. It’s just a base for you to start your extraction.
During development if you have a specific problem open another question.
Learn more about PL/SQL by reading oracle documentation.
Note that the excerpt below is only to mount a table with some example records, if you want to change this to better adapt to the real case, or use the real table.
WITH tabela_(registro, data) as (
select 'x', to_date('13/03/2017', 'DD/MM/YYYY') from dual union all
select 'y', to_date('14/03/2017', 'DD/MM/YYYY') from dual union all
select 'x', to_date('17/03/2017', 'DD/MM/YYYY') from dual union all
select 'x', to_date('21/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('13/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('19/03/2017', 'DD/MM/YYYY') from dual union all
select 'w', to_date('25/03/2017', 'DD/MM/YYYY') from dual union all
select 'z', to_date('31/03/2017', 'DD/MM/YYYY') from dual )
select * from tabela_;
The first solution that occurs to me is to create a Function that receives : code , start date and range , count how many occurrences have before the date until the range limit and return this number, sql would call this Function
– Motta
But if there is one on 13/03/2017, another on 19/03/2017 and another on 25/03/2017. From 13 to 25 went from 7 day, but from 19 to 25 no. Should show?
– George Wurthmann
Yes, I imagined this situation and further complicated kk. I thought about doing something that counted the occurrences, for example: occurred in that period 2 times and one of them was between 7 days, I would display: RECORD X | 1 TIME. Any idea for that?
– Mat