I need a script to create a table . The problem is my select for creating the table contains equal values in some rows. I need to use distinct for these columns. The other columns' values can come from any matching row.
My current result table has data like this:
| CITY | STREET | STREET_NUM | VAL_X | VAL_Y |
------------------------------------------------------------------
| CityA | Street abc | 5 | 11.5 | 0.5 |
| CityA | Street abc | 5 | 15.4 | 1.8 |
| CityA | Street abc | 5 | 12.4 | 2.8 |
| CityB | Street xyz | 18 | 5.4 | 1.9 |
| CityB | Street xyz | 18 | 8.4 | 1.1 |
| CityC | Street klm | 55 | 9.6 | 0.8 |
But I need data like this:
| CITY | STREET | STREET_NUM | VAL_X | VAL_Y |
------------------------------------------------------------------
| CityA | Street abc | 5 | 11.5 | 0.5 |
| CityB | Street xyz | 18 | 5.4 | 1.9 |
| CityC | Street klm | 55 | 9.6 | 0.8 |
For columns city, street and street_num I need to apply distinct. val_x and val_y should be used anyone, for example first of that group with same city, street and street_num.
Can you give me advice how to edit this script?
Best Answer
"VAL_X" and "VAL_Y" chosen through some aggregate function
You should consider using
GROUP BY
for the columns whose values you consider that should be "distinct" (as a group), and, for the rest of columns, choose an appropriate aggregate function (for instance,MIN
):If you need to put together values from several tables,
UNION ALL
of them before youGROUP BY
:Using always "VAL_X" and "VAL_Y" from same row, using a WINDOW
If you need to make sure your values are always from the same row, the best way is to use a
WINDOW
in your query:PARTITION BY "CITY", "STREET", "STREET_NUM"
andORDER BY "VAL_X", "VAL_Y"
, and choose the first row of every partition.You can do this with two steps:
1) Add the
row_num()
to every partition:2) At this point, choose only the rows
WHERE rn=1
(andORDER
them, if necessary):The result is:
You can see the example at SQLFiddle