To ensure the mpg_won column is created in lowercase within your column_list, you can modify the special_column Common Table Expression (CTE) to convert the COLUMN_NAME to lowercase using the LOWER() function. This ensures that when MPG_WON is included in the final column_list, it appears as mpg_won.
Here's the updated SQL query with the necessary changes:
WITH first_950_columns AS (
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DS_DEMO'
AND TABLE_NAME = 'OPPORTUNITIES_DATASET'
ORDER BY ORDINAL_POSITION
LIMIT 950
),
special_column AS (
SELECT LOWER(COLUMN_NAME) AS COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DS_DEMO'
AND TABLE_NAME = 'OPPORTUNITIES_DATASET'
AND COLUMN_NAME = 'MPG_WON'
),
combined_columns AS (
SELECT COLUMN_NAME FROM first_950_columns
UNION -- Ensures `mpg_won` is included without duplication
SELECT COLUMN_NAME FROM special_column
)
SELECT LISTAGG('"' || COLUMN_NAME || '"', ', ') AS column_list
FROM combined_columns;
Explanation of Changes:
-
Convert MPG_WON to Lowercase in special_column:
special_column AS (
SELECT LOWER(COLUMN_NAME) AS COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DS_DEMO'
AND TABLE_NAME = 'OPPORTUNITIES_DATASET'
AND COLUMN_NAME = 'MPG_WON'
),
- The
LOWER(COLUMN_NAME) function converts MPG_WON to mpg_won.
- Renaming the column using
AS COLUMN_NAME ensures consistency in the combined_columns CTE.
-
Preserve Case for Other Columns:
- The
first_950_columns CTE remains unchanged, preserving the original case for the first 950 columns.
-
Combine Columns Without Duplication:
- The
UNION operation ensures that mpg_won is included in the combined_columns without duplicating it if it's already part of the first 950 columns.
-
Generate the Comma-Separated column_list:
SELECT LISTAGG('"' || COLUMN_NAME || '"', ', ') AS column_list
FROM combined_columns;
- This concatenates all column names into a single comma-separated string, with each column name enclosed in double quotes.
Additional Considerations:
This modification ensures that mpg_won appears in lowercase in your final column_list while maintaining the integrity of the other column names.