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 and/or data_source_v2 to the name of the table and/or table version:

 1SELECT
 2  name_id
 3  ,COUNT(DISTINCT amperity_id)
 4FROM
 5  data_source
 6GROUP BY 1
 7HAVING COUNT(DISTINCT amperity_id) > 1
 8
 9SELECT
10  name_id
11  ,COUNT(DISTINCT amperity_id)
12FROM
13  data_source_v2
14GROUP BY 1
15HAVING 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 and/or data_source_v2 to the name of the table and/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
10    data_source_v2)
11  AS v1
12FULL OUTER JOIN (
13  SELECT DISTINCT
14    name_id
15    ,amperity_id
16  FROM
17    data_source)
18  AS v2
19ON v1.name_id = v2.name_id
20WHERE v1.amperity_id <> v2.amperity_id
21LIMIT 100

Inspect Names

Use any of the following SELECT statements to inspect name IDs. Update name_id to the name ID, and data_source and/or data_source_v2 to the name of the table and/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 and/or data_source_2 to the name of the table:

 1SELECT
 2 fk_field_name
 3 ,COUNT(DISTINCT amperity_id)
 4FROM
 5  data_source_1
 6GROUP BY 1
 7HAVING COUNT(DISTINCT amperity_id) > 1
 8
 9SELECT
10  fk_field_name
11  ,COUNT(DISTINCT amperity_id)
12FROM
13  data_source_2
14GROUP BY 1
15HAVING 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 and/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
10    data_source_2
11  )
12AS v1 FULL OUTER JOIN (
13  SELECT DISTINCT
14    fk_field_name
15    ,amperity_id
16  FROM
17    data_source_1
18  )
19AS v2 ON v1.fk_field_name = v2.fk_field_name
20WHERE v1.amperity_id <> v2.amperity_id
21LIMIT 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 and/or data_source_2 to the name of the table:

1SELECT
2  COUNT(DISTINCT fk_field_name)
3FROM
4  data_source
1SELECT
2  COUNT(DISTINCT fk_field_name)
3FROM
4  data_source_1
5WHERE fk_field_name IN (
6  SELECT fk_field_name
7  FROM data_source_2
8)
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)