卫星表中有来自不同来源的重复卫星。
╔═════════╦══════════╦═══════╦═════════╦════════════╦══════════╦════════╗
║ _sat_id ║ name ║ norad ║ intldes ║ un_country ║ operator ║ source ║
╠═════════╬══════════╬═══════╬═════════╬════════════╬══════════╬════════╣
║ 1 ║ ISS ║ 25544 ║ 98067A ║ null ║ Frank ║ s_t ║
║ 2 ║ Int stat ║ 25544 ║ 98067A ║ null ║ null ║ ucs ║
║ 3 ║ zarya ║ 0 ║ 98067A ║ US ║ John ║ nasa ║
║ 4 ║ iss ║ 25544 ║ 98067A ║ Sovjet ║ John ║ celes ║
║ 5 ║ ISS ║ 25544 ║ null ║ Russia ║ null ║ other ║
╚═════════╩══════════╩═══════╩═════════╩════════════╩══════════╩════════╝
如何按照优先级列表在MySQL中合并(而不是组concat)? 例如优先级列表:
NASA
所以合并的行将包含所有 1.s_t数据, 所有空值都是2.ucs数据, 如果仍然有空,那么3.celes数据等。
我尝试使用以下MySQL查询:
SELECT
group_concat(`sources`) as sources,
max(`_sat_id`) as _sat_id,
max(`off_name`) as off_name,
max(`norad`) as norad,
max(`intldes`) as intldes,
max(`un_reg_country`) as un_reg_country,
max(`operator_country`) as operator_country,
max(`operator`) as operator,
max(`contractor_country`) as contractor_country,
max(`contractor`) as contractor,
max(`users`) as users,
max(`contact_info`) as contact_info,
max(`operational_status`) as operational_status,
max(`application`) as application,
max(`period`) as period,
max(`has_propulsion`) as has_propulsion,
max(`power`) as power,
max(`dry_mass`) as dry_mass,
max(`orbit_class`) as orbit_class,
max(`orbit_type`) as orbit_type,
max(`expected_life_time`) as expected_life_time,
max(`decay_date`) as decay_date,
max(`longitude`) as longitude,
max(`perigee`) as perigee,
max(`apogee`) as apogee,
max(`eccentricity`) as eccentricity,
max(`inclination`) as inclination,
max(`launch_date`) as launch_date,
max(`launch_mass`) as launch_mass,
max(`launch_site`) as launch_site,
max(`launch_vehic`) as launch_vehic,
max(`description`) as description,
group_concat(`comments`) as comments
FROM satellite
GROUP BY intldes
但有了这个,我不知道哪一行获得优先权。
我也尝试过使用Java,但每500行需要40秒......
提前......
答案 0 :(得分:1)
这将返回优先级最佳匹配:
SELECT
intldes,
coalesce(max(case when source = 's_t' then `_sat_id` end)
,max(case when source = 'ucs' then `_sat_id` end)
,max(case when source = 'celes' then `_sat_id` end)
,max(case when source = 'nasa' then `_sat_id` end)) as _sat_id,
coalesce(max(case when source = 's_t' then `off_name ` end)
,max(case when source = 'ucs' then `off_name ` end)
,max(case when source = 'celes' then `off_name ` end)
,max(case when source = 'nasa' then `off_name ` end)) as off_name,
...
FROM satellite
GROUP BY intldes
大量的剪切和粘贴和修改,可能不是很有效。但希望这是一次性的工作。
答案 1 :(得分:1)
我相信类似以下查询的内容可以解决问题:
SELECT
COALESCE(s_t.off_name, ucs.off_name, celes.off_name, nasa.off_name) AS off_name
FROM (SELECT intldes FROM satellite GROUP BY intldes) all
LEFT JOIN satellite s_t ON all.intldes = s_t.intldes AND s_t.source = 's_t'
LEFT JOIN satellite ucs ON all.intldes = ucs.intldes AND ucs.source = 'ucs'
LEFT JOIN satellite celes ON all.intldes = celes.intldes AND celes.source = 'celes'
LEFT JOIN satellite nasa ON all.intldes = nasa.intldes AND nasa.source = 'nasa';
我只展示了一个属性作为示例,您可以为其余属性扩展它。
答案 2 :(得分:0)
不完全确定结果应该是什么样子,将其放入 - 可能会有所帮助 - ROW_NUMBER函数会有帮助吗?比如ROW_NUMBER() in MySQL然后根据您的优先级选择行?