APPROACH #1
If you never delete from table1
, table2
, or table3
STEP #1
Using a Cartesian Join, generate a script that makes individual inserts
SQL="SELECT CONCAT('INSERT IGNORE INTO keymap VALUES"
SQL="${SQL} (',col1,',',col2,',',col3,');') FROM"
SQL="${SQL} (SELECT col1 FROM table1) A,"
SQL="${SQL} (SELECT col2 FROM table2) B,"
SQL="${SQL} (SELECT col3 FROM table3) C;"
read -s pw ; echo ${pw} | less
DB=mydb
echo "SET foreign_key_checks = 0;" > All_Key_Combinations.sql
mysql -uroot -p${pw} -D${DB} -ANe"${SQL}" >> All_Key_Combinations.sql
STEP #2
Review the script and make sure it's OK
head All_Key_Combinations.sql
echo
tail All_Key_Combinations.sql
echo
sleep 15
less All_Key_Combinations.sql
STEP 3
Execute All_Key_Combinations.sql
DB=mydb
mysql -uroot -p${pw} -D${DB} < All_Key_Combinations.sql
APPROACH #2
If you ever delete from table1
, table2
, or table3
STEP #1
Using a Cartesian Join, generate a script that makes individual inserts
SQL="SELECT CONCAT('INSERT INTO keymap VALUES"
SQL="${SQL} (',col1,',',col2,',',col3,');') FROM"
SQL="${SQL} (SELECT col1 FROM table1) A,"
SQL="${SQL} (SELECT col2 FROM table2) B,"
SQL="${SQL} (SELECT col3 FROM table3) C;"
read -s pw ; echo ${pw} | less
DB=mydb
echo "SET foreign_key_checks = 0;" > All_Key_Combinations.sql
echo "TRUNCATE TABLE keymap;" >> All_Key_Combinations.sql
mysql -uroot -p${pw} -D${DB} -ANe"${SQL}" >> All_Key_Combinations.sql
STEP #2
Review the script and make sure it's OK
head All_Key_Combinations.sql
echo
tail All_Key_Combinations.sql
echo
sleep 15
less All_Key_Combinations.sql
STEP 3
Execute All_Key_Combinations.sql
DB=mydb
mysql -uroot -p${pw} -D${DB} < All_Key_Combinations.sql
GIVE IT A TRY !!!
I've used this data as start point:
create table t
(
target_link_id int,
reversed_target_link boolean,
source_geometry_id int,
reliability double precision
);
insert into t values
(750, true, 6156655, 96.90882243480968),
(750, true, 6156654, 96.3385845004076),
(759, false, 299963, 97.88820671302537),
(759, false, 299962, 90.81686447596023),
(759, false, 4339973, 83.83639328022115),
(981, true, 304625, 94.25089913251661),
(981, true, 304624, 96.521137788089),
(981, true, 304626, 94.21941014522639),
(981, true, 304621, 91.93785338486143),
(981, true, 304622, 95.27831743865612);
If you add a row_number() to your previous query you can get the desired result in this way:
with ct as
(
select
target_link_id,
reversed_target_link,
source_geometry_id,
reliability,
row_number() over (partition by target_link_id order by reliability desc) rn
from
t
)
select
target_link_id,
reversed_target_link,
source_geometry_id,
reliability,
from
ct
where
rn = 1;
target_link_id | reversed_target_link | source_geometry_id | reliability | rn
-------------: | :------------------- | -----------------: | :--------------- | -:
750 | t | 6156655 | 96.9088224348097 | 1
759 | f | 299963 | 97.8882067130254 | 1
981 | t | 304624 | 96.521137788089 | 1
db<>fiddle here
Best Answer
Disclaimer, everything is nullable in your tables so
IS NULL
predicates may give false positives.Your query comes a long way, all you need is to add certain predicates:
1 and 2.
3 .