Search code examples
sqlmissing-datainsertion

How do you get get absent rows from a SQL table?


I am not sure if I had asked the question correctly in title, but here the proper scenario.

Suppose I have SQL table in which rows are inserted everyday. In these rows there is one column which has one set of values. That means in this column, let's call it Source_system, we receive 20 values each day. Same distinct values everyday. Now is there any way I can get the name of the source system which is not inserted for that day?

EDIT: Sorry for bad English.


Solution

  • There are several ways to do this depending on the way your data is formatted. If the Source_system entries that are not inserted are null, a simple Where statement can be used to find what's blank and get the name of the rest of the columns

    Select *
    From Table_name
    Where Source_system is null
    

    Since your 20 distinct values are known, if you have these in a table, you can also Outer Join the tables and find the rows where Source_system doesn't match the entries in the table of distinct values

    Select Date_inserted_into_db, Source_system
    From Table_name T1
    Full Outer Join Table_of_distinct_values T2 on T1.Source_system = T2.Source_system
    Where Source_system is null