Ad

Fill NULL And Get Distinct Record

I am ingesting data from two different source (source A and source B). Although data may be coming from two different sources, there is a possibility that an ID for a record may exist in both sources.

The problem is, there are cases when one source is more complete than the other. In the example below, Source A has a NULL for column C, however Source B has a complete record.

I would like to enrich the data where I get a complete record back.

  • Note: the 'Source' field below is just for illustration purposes, and not an actual field in the solution.
IDABCDSource
asc12312NULL4Source A
asc1231234Source B

The end result should be as follows:

IDABCD
asc1231234

I have attempting something like this, but have not been successful:

SELECT  ID
       ,A
       ,B
       ,COALESCE(A.C, B.C) AS C
       ,D
       ,Source
    FROM(
    SELECT * FROM (
        SELECT 'asc123' AS ID, 1 AS 'A', 2 AS 'B', NULL AS 'C', 4 AS 'D', 'Source A' AS 'Source'
        ) A
    UNION

    SELECT * FROM (
        SELECT 'asc123' AS ID, 1 AS 'A', 2 AS 'B', 3 AS 'C', 4 AS 'D', 'Source B' AS 'Source'
        ) B
    ) X
Ad

Answer

You can just aggregate to remove the NULL values:

with sampledata as (
    select 'asc123' AS ID, 1 AS A, 2 AS B, NULL AS C, 4 as D, 'Source A' AS Source 
    union all
    select 'asc123' AS ID, 1 AS A, 2 AS B, 3 AS C, 4 AS D, 'Source B' AS Source
)
select Id,
  max(A)A, Max(B)B, Max(C)C, Max(D)D 
from sampledata
group by Id

Note also that for column aliases, if you need to delimit them use [] or "", single quotes are for literal values.

Ad
source: stackoverflow.com
Ad