Last active
December 12, 2019 01:07
-
-
Save adamantnz/78aeeec9691c895d7a8f22e30e90b0d1 to your computer and use it in GitHub Desktop.
Update AWS Cost and Usage Report Columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dynamically creates Redshift "alter column" SQL for AWS "Cost and Usage Reports" based on the "tagmapping" data | |
Note: Redshift column names are case-sensitive so the first query renames the duplicate usertags (if any) | |
*/ | |
update awsbillingYYYYMM_tagmapping | |
set usertag = b.usertag || '_' || cast(sequence as varchar) | |
from awsbillingYYYYMM_tagmapping a | |
inner join ( | |
select row_number() over ( | |
partition by lower(usertag) order by usertag desc | |
) as sequence | |
,usertag | |
from awsbillingYYYYMM_tagmapping | |
) b on a.usertag = b.usertag | |
and sequence <> 1 | |
; | |
select | |
listagg('alter table awsbillingYYYYMM rename column ' || remappedusertag || ' to "' || usertag,'"; \n') | |
within group (order by usertag) || '";' | |
from awsbillingYYYYMM_tagmapping | |
; | |
/* | |
alter table awsbillingYYYYMM rename column userTag0 to "aws:user"; | |
alter table awsbillingYYYYMM rename column userTag1 to "aws:group"; | |
alter table awsbillingYYYYMM rename column userTag2 to "aws:product"; | |
...etc | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment