Foreign key validation

Use this query to compare two versions of a data source or two distinct data sources and return coverage and distribution data for foreign keys across multiple data sources.

Note

This query should not be used to compare distinct foreign keys across data sources, such as fk-name-id and fk-customer-id.

When to use

  1. Looking for name IDs with multiple Amperity IDs.

  2. Looking for name IDs with different Amperity IDs.

  3. Inspecting name IDs between tables.

  4. Checking for foreign keys with multiple Amperity IDs between tables.

  5. Checking for foreign keys with different Amperity IDs between tables.

  6. Inspecting foreign keys between tables.

Configure query

  1. From the Queries page, open the Stitch QA folder, and then select this query.

    Tip

    Add the Stitch QA queries template folder if it does not already exist.

  2. Uncomment the specific SELECT statement to run, and then update that statement for the foreign key names and table names.

  3. Click Run Query and debug any issues that may arise.

  4. Click Activate.

Names, multiple IDs

Use any of the following SELECT statements to look for name IDs with multiple Amperity IDs. Update name_id to the name ID, and data_source or data_source_v2 to the name of the table or table version:

 1SELECT
 2  name_id
 3  ,COUNT(DISTINCT amperity_id)
 4FROM data_source
 5GROUP BY 1
 6HAVING COUNT(DISTINCT amperity_id) > 1
 7
 8SELECT
 9  name_id
10  ,COUNT(DISTINCT amperity_id)
11FROM data_source_v2
12GROUP BY 1
13HAVING COUNT(DISTINCT amperity_id) > 1

Names, different IDs

Use any of the following SELECT statements to look for name IDs with different Amperity IDs. Update name_id to the name ID, and data_source or data_source_v2 to the name of the table or table version:

 1SELECT
 2  v1.name_id
 3  ,v1.amperity_id
 4  ,v2.amperity_id
 5FROM (
 6  SELECT DISTINCT
 7    name_id
 8    ,amperity_id
 9  FROM data_source_v2
10) AS v1
11FULL OUTER JOIN (
12  SELECT DISTINCT
13    name_id
14    ,amperity_id
15  FROM data_source
16) AS v2
17ON v1.name_id = v2.name_id
18WHERE v1.amperity_id <> v2.amperity_id
19LIMIT 100

Inspect Names

Use any of the following SELECT statements to inspect name IDs. Update name_id to the name ID, and data_source or data_source_v2 to the name of the table or table version:

1SELECT COUNT(DISTINCT name_id)
2FROM data_source
1SELECT COUNT(DISTINCT name_id)
2FROM data_source_v2
1SELECT COUNT(DISTINCT name_id)
2FROM data_source
3WHERE name_id IN (
4  SELECT name_id
5  FROM data_source_v2
6)
1SELECT COUNT(DISTINCT name_id)
2FROM data_source_v2
3WHERE name_id NOT IN (
4  SELECT name_id
5  FROM data_source
6)

Multiple Amperity IDs

Use any of the following SELECT statements to look for foreign keys that have multiple Amperity IDs. Update fk_field_name to the name of a foreign key, and data_source, data_source_1 or data_source_2 to the name of the table:

 1SELECT
 2 fk_field_name
 3 ,COUNT(DISTINCT amperity_id)
 4FROM data_source_1
 5GROUP BY 1
 6HAVING COUNT(DISTINCT amperity_id) > 1
 7
 8SELECT
 9  fk_field_name
10  ,COUNT(DISTINCT amperity_id)
11FROM data_source_2
12GROUP BY 1
13HAVING COUNT(DISTINCT amperity_id) > 1

Different Amperity IDs

Use any of the following SELECT statements to look for foreign keys that have different Amperity IDs. Update fk_field_name to the name of a foreign key, and data_source, data_source_1 or data_source_2 to the name of the table:

 1SELECT
 2  v1.fk_field_name
 3  ,v1.amperity_id
 4  ,v2.amperity_id
 5FROM (
 6  SELECT DISTINCT
 7    fk_field_name
 8    ,amperity_id
 9  FROM data_source_2
10  )
11AS v1 FULL OUTER JOIN (
12  SELECT DISTINCT
13    fk_field_name
14    ,amperity_id
15  FROM data_source_1
16) AS v2
17ON v1.fk_field_name = v2.fk_field_name
18WHERE v1.amperity_id <> v2.amperity_id
19LIMIT 100

Inspect keys between tables

Use any of the following SELECT statements to inspect foreign keys between tables. Update fk_field_name to the name of a foreign key, and data_source, data_source_1 or data_source_2 to the name of the table:

1SELECT
2  COUNT(DISTINCT fk_field_name)
3FROM data_source
1SELECT
2  COUNT(DISTINCT fk_field_name)
3FROM data_source_1
4WHERE fk_field_name IN (
5  SELECT fk_field_name
6  FROM data_source_2
7)
1SELECT
2  COUNT(DISTINCT fk_field_name)
3FROM data_source_1
4WHERE fk_field_name NOT IN (
5  SELECT fk_field_name
6  FROM data_source_2
7)
1SELECT
2  COUNT(DISTINCT fk_field_name)
3FROM data_source_2
4WHERE fk_field_name NOT IN (
5  SELECT fk_field_name
6  FROM data_source_1
7)