Skip to content

Commit

Permalink
Merge pull request #349 from coderxio/jrlegrand/gtin
Browse files Browse the repository at this point in the history
Create GTINs mart
  • Loading branch information
jrlegrand authored Jan 16, 2025
2 parents ba12771 + a5d3c5b commit 9feb666
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions dbt/sagerx/models/marts/ndc/gtins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
-- gtins.sql

with

ndcs as (

select
ndc11,
replace(ndcpackagecode, '-', '') as ndc10,
ndcpackagecode as ndc,
concat(
'003',
replace(ndcpackagecode,'-', '')
) as gtin13,
concat(
'3',
replace(ndcpackagecode,'-', '')
) as gtin11,
concat(
'03',
split_part(ndcpackagecode, '-', 1)
) as gs1_company_prefix

from {{ ref('stg_fda_ndc__ndcs') }}

),

digits as (

-- split the 13-digit number into individual digits
select
ndc,
position,
substring(gtin13 from position for 1)::int as digit
from ndcs,
generate_series(1, 13) as position

),

products as (

-- apply the alternating multiplication rule
select
*,
case
when position % 2 = 1
then digit * 3
else digit * 1
end as product
from digits

),

sums as (

-- sum of the products of each digit
select
ndc,
sum(product) as sum
from products
group by ndc

),

check_digits as (

-- round the sum to the nearest 10 and subtract the sum
select
ndc,
ceil(sum / 10.0) * 10 - sum as check_digit
from sums

),

gtin14s as (

-- concatenate the gtin13 and check_digit
select
ndc11,
ndc10,
ndcs.ndc,
concat(
gtin13,
check_digit
) as gtin14,
concat(
gtin11,
check_digit
) as gtin12,
gs1_company_prefix
from ndcs
left join check_digits
on check_digits.ndc = ndcs.ndc

)

select * from gtin14s

0 comments on commit 9feb666

Please sign in to comment.