Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Vergleichen Sie und erhalten Sie die neuen Daten basierend auf dem Monat in die Tabelle eingefügt

Sie könnten ein Minus

verwenden
select SYS_DB_NAME, ENTITY_ID, MONTH_ID
from my_table 
where MONTH_ID = to_char(sysdate, 'YYYY') || lpad( extract(month from sysdate), 2,'0')
minus  
select  SYS_DB_NAME, ENTITY_ID, MONTH_ID
from my_table 
where MONTH_ID = to_char(sysdate, 'YYYY') || lpad( (extract(month from sysdate) -1) , 2,'0') 

und wenn Sie den Zeileninhalt benötigen

select * from  my_table  m
inner join  (
    select SYS_DB_NAME, ENTITY_ID, MONTH_ID
    from my_table 
    where MONTH_ID = to_char(sysdate, 'YYYY') || lpad( extract(month from sysdate), 2,'0')
    minus  
    select  SYS_DB_NAME, ENTITY_ID, MONTH_ID
    from my_table 
    where MONTH_ID = to_char(sysdate, 'YYYY') || lpad( (extract(month from sysdate) -1) , 2,'0')
) T on m.SYS_DB_NAME = t.SYS_DB_NAME 
      AND m.ENTITY_ID = t.ENTITY_ID 
        AND m.MONTH_ID = t.MONTH_ID

und wenn Sie nur zählen müssen

select count(*) from  
inner join  (
    select SYS_DB_NAME, ENTITY_ID, MONTH_ID
    from my_table 
    where MONTH_ID = to_char(sysdate, 'YYYY') || lpad( extract(month from sysdate), 2,'0')
    minus  
    select  SYS_DB_NAME, ENTITY_ID, MONTH_ID
    from my_table 
    where MONTH_ID = to_char(sysdate, 'YYYY') || lpad( (extract(month from sysdate) -1) , 2,'0')
) T