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..c3f41de --- /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].[MILITARY_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].[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 [mgra] + ORDER BY [mgra] +END \ No newline at end of file