From 027d93f92cdb45a24292b5fbef1fb76eaa2d4333 Mon Sep 17 00:00:00 2001 From: Bryce Burdge Date: Thu, 21 May 2026 09:05:57 -0700 Subject: [PATCH 1/2] #242 add in military employment --- python/employment.py | 37 ++++++++++++++++++++- python/tests.py | 2 +- sql/employment/get_military_employment.sql | 38 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 sql/employment/get_military_employment.sql diff --git a/python/employment.py b/python/employment.py index 72f011c..e88bff0 100644 --- a/python/employment.py +++ b/python/employment.py @@ -274,6 +274,29 @@ def _get_jobs_inputs(year: int) -> dict[str, pd.DataFrame]: "year": year, }, ) + # Get military employment data and append to control_totals + with open(utils.SQL_FOLDER / "employment/get_military_employment.sql") as file: + jobs_inputs["military_emp"] = pd.read_sql_query( + sql=sql.text(file.read()), + con=con, + params={ + "run_id": utils.RUN_ID, + "year": year, + }, + ) + + military_control_totals = ( + jobs_inputs["military_emp"] + .groupby(["run_id", "year", "industry_code", "metric"], as_index=False)[ + "value" + ] + .sum() + )[["run_id", "year", "industry_code", "metric", "value"]] + + jobs_inputs["control_totals"] = pd.concat( + [jobs_inputs["control_totals"], military_control_totals], + ignore_index=True, + ) return jobs_inputs @@ -311,12 +334,20 @@ def _validate_jobs_inputs(jobs_inputs: dict[str, pd.DataFrame]) -> None: null={}, ) tests.validate_data( - "QCEW control totals", + "Military employment data", + jobs_inputs["military_emp"], + row_count={"key_columns": {"mgra"}}, + negative={}, + null={}, + ) + tests.validate_data( + "Jobs control totals", jobs_inputs["control_totals"], row_count={"key_columns": {"industry_code"}}, negative={}, null={}, ) + def _create_jobs_output( @@ -341,6 +372,10 @@ def _create_jobs_output( _distribute_self_emp_to_mgra( jobs_inputs["B24080"], jobs_inputs["xref_bg_to_mgra"] ), + # Include military employment at MGRA level + jobs_inputs["military_emp"][ + ["run_id", "year", "mgra", "industry_code", "value"] + ], ], ignore_index=True, ).sort_values(by=["mgra", "industry_code"]) diff --git a/python/tests.py b/python/tests.py index 30ecd10..d52de99 100644 --- a/python/tests.py +++ b/python/tests.py @@ -41,7 +41,7 @@ # and 722 3-digit naics code. Self employment data does not natively have a # naics code so it is therefore assigned to 'SE' as the naics codes are being # treated as strings. - "industry_code": 22, + "industry_code": 23, }, "series": { 15: { diff --git a/sql/employment/get_military_employment.sql b/sql/employment/get_military_employment.sql new file mode 100644 index 0000000..6fc2b26 --- /dev/null +++ b/sql/employment/get_military_employment.sql @@ -0,0 +1,38 @@ +/* +This query grabs the Military Active Duty (Job) Data and assigns counts to MGRA15. +This will assign 0 to MGRAs where there is no Military Jobs +*/ + +-- Initialize parameters ----------------------------------------------------- +DECLARE @run_id INTEGER = :run_id; +DECLARE @year INTEGER = :year; + + +-- Send error message if no data exists -------------------------------------- +IF NOT EXISTS ( + SELECT TOP (1) * + FROM [ws].[fact].[MILILTARY_EMPLOYMENT] + WHERE [year] = @year +) +BEGIN + THROW 50000, 'Military Active duty data does not exist.', 1; +END +ELSE +BEGIN + +-- Get MGRA Military Active Duty Counts ------------------------------------- + SELECT + @run_id AS [run_id], + @year AS [year], + [mgra], + 'MIL' AS [industry_code], + 'jobs' AS [metric], + COALESCE(SUM([site_active_duty]), 0) AS [value] + FROM [inputs].[mgra] + LEFT OUTER JOIN [ws].[fact].[MILILTARY_EMPLOYMENT] + ON [MILILTARY_EMPLOYMENT].[shape].STWithin([mgra].[shape]) = 1 + AND [run_id] = @run_id AND [year] = @year + WHERE [run_id] = @run_id + GROUP BY [run_id], [year], [mgra] + ORDER BY [year], [mgra] +END \ No newline at end of file From c35a0398b0fe078ae827978158b54b14cfc24280 Mon Sep 17 00:00:00 2001 From: Bryce Burdge Date: Thu, 21 May 2026 09:43:55 -0700 Subject: [PATCH 2/2] #242 small changes and spelling fix after copilot pr feedback --- sql/employment/get_military_employment.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sql/employment/get_military_employment.sql b/sql/employment/get_military_employment.sql index 6fc2b26..c3f41de 100644 --- a/sql/employment/get_military_employment.sql +++ b/sql/employment/get_military_employment.sql @@ -11,7 +11,7 @@ DECLARE @year INTEGER = :year; -- Send error message if no data exists -------------------------------------- IF NOT EXISTS ( SELECT TOP (1) * - FROM [ws].[fact].[MILILTARY_EMPLOYMENT] + FROM [ws].[fact].[MILITARY_EMPLOYMENT] WHERE [year] = @year ) BEGIN @@ -29,10 +29,10 @@ BEGIN 'jobs' AS [metric], COALESCE(SUM([site_active_duty]), 0) AS [value] FROM [inputs].[mgra] - LEFT OUTER JOIN [ws].[fact].[MILILTARY_EMPLOYMENT] - ON [MILILTARY_EMPLOYMENT].[shape].STWithin([mgra].[shape]) = 1 + LEFT OUTER JOIN [ws].[fact].[MILITARY_EMPLOYMENT] + ON [MILITARY_EMPLOYMENT].[shape].STWithin([mgra].[shape]) = 1 AND [run_id] = @run_id AND [year] = @year WHERE [run_id] = @run_id - GROUP BY [run_id], [year], [mgra] - ORDER BY [year], [mgra] + GROUP BY [mgra] + ORDER BY [mgra] END \ No newline at end of file