I am using AWS Redshift. I have a table that looks like this:
Id key value1 value2 value3
1 xxx A NULL NULL
1 xxx NULL B NULL
2 uuu NULL NULL C
If the id is repeating, the entire row will be the same expect value1, value2 and value3. For these three columns, only one will be filled at a time. How can I merge the these rows so that my final result looks like this?
Id key value1 value2 value3
1 xxx A B NULL
2 uuu NULL NULL C
If there’s a value for these 3 columns, I want to use it. Otherwise, I want to let the value be NULL.
When I try this:
Select
id, key, a, b,
max(value1) as value1,
max(value2) as value1,
max(value3) as value3
from my_table group by id, key, a, b
I get this error, probably due to the NULLs.
failed to find conversion function from "unknown" to text
What else could I try to achieve my desired result?
Edit:
with test as (
id,
key,
'business_vault' as a,
b
from my_table
)
select
a
from test group by a
2
Answers
You do not have columns
a
norb
, as presented thase are values. Now you could select those but would have to literal values by enclosing then in single quotes (‘).. But I do not think that is what you are after. I think you get what you want by just removing them. So: (see demo)If we add columns a and b to your sample data in a way that those columns have different values in all rows then your sample data would be:
… you can get the expected result (from the question) simply by using Max() aggregate function and Group By id, key …
… however, if you need the a and b columns included in the resultset (and a and b have different values for the same id and key) then there will be no reduction of duplicate id rows. If you need to select the a and b columns in an aggregated query then those columns (a, b) should be either aggregated or part of group by clause. …
… the question is what you want to do with the data in columns a and b when you reduce number of rows to show id, key combination just once. One option could be use analytic (window) functions to create a list of a and b column values – STRING_AGG() Over(), and Max() Over() for values 1,2,3 showing just Distinct rows …
… as a specific context – if columns a and b have the same value for the same id, key combination then the first query from this answer could be widened with a and b column in Select and Group By clauses still getting reduced duplicate rows …
See the fiddle here.