My question might be similar to these questions:
- MySQL group by with ordering/priority of another column
- Grouping by Column with Dependence on another Column
- MySQL GROUP BY with preference
Table Example:
source zip phone street city created_at
==================================================================
a 11111 11111 Flatlands null 2015-01-01
b 11111 11111 Flatlands Avenue New York 2015-01-01
c 11111 11111 Ave Flatlands New York 2015-01-01
a 22222 22222 Favory New York 2015-01-01
b 22222 22222 Favory Avenue New York 2017-12-12
c 22222 22222 Ave Favory New York 2015-01-01
b 33333 33333 Sixteenth Washington 2015-01-01
c 33333 33333 st. 16th null 2015-01-01
c 44444 44444 st. West Land null 2015-01-01
Let's say I have a table with information about places in different cities. The information is gathered from 3 different sources: a
, b
, and c
.
zip
and phone
fields uniquely identify the location, so the rows in the DB can be grouped by these fields.
I need to merge information about various locations from various sources selecting the best value based on the set of rules for street
and city
columns.
The rules are:
- For each group prioritize non empty values of
street
andcity
over null values. - In each group prioritize values of
street
andcity
columns froma
andb
sources overc
source (weight(a
) = weight(b
) > weight(c
)) if these columns are not empty. - For sources
a
andb
prioritize column values from rows which have latestcreated_at
timestamp.
Here is the result I want to receive:
zip phone street city
====================================
11111 11111 Flatlands New York
22222 22222 Favory Avenue New York
33333 33333 Sixteenth Washington
44444 44444 st. West Land null
Here is a DB Fiddle to play with.
I'm not sure this is possible to achieve with SQL and maybe my best option is to switch to NoSQL DB + imperative processing task. Or just use some tool to extract information from the DB and then process it.
P.S. This is a simplified example.