\connect postgresql_book
\cd /postgresql_up_and_running_4e_code_data/raw/ACSST1Y2024.S2502

DROP TABLE IF EXISTS staging.lu_acs_columns;
CREATE TABLE staging.lu_acs_columns(column_name text, label text);
\copy staging.lu_acs_columns FROM ACSST1Y2024.S2502-Column-Metadata.csv CSV HEADER

DROP TABLE IF EXISTS staging.acs_data_raw;
SELECT 'CREATE TABLE staging.acs_data_raw(' 
      || string_agg( quote_ident( lower(column_name)  )
      || ' text', ',') || ');'
FROM staging.lu_acs_columns
\gexec
ALTER TABLE staging.acs_data_raw ADD COLUMN unknown text;

\copy staging.acs_data_raw FROM ACSST1Y2024.S2502-Data.csv WITH (format 'csv', HEADER)

CREATE SCHEMA IF NOT EXISTS census;
DROP TABLE IF EXISTS census.acs_data;
SELECT 'CREATE TABLE census.acs_data AS 
  SELECT ' 
      || string_agg( 'NULLIF(' || quote_ident( lower(column_name) ) || ', ''N'') ' 
      || CASE WHEN column_name IN('GEO_ID', 'NAME') THEN '::text' ELSE '::numeric' END 
      || ' AS ' 
      || quote_ident( lower(column_name) ) , ',' ) || '
 FROM staging.acs_data_raw
 WHERE geo_id <> ''Geography'';'
FROM staging.lu_acs_columns
\gexec