SELECT anon_1.id_streets_id_np AS anon_1_id_streets_id_np,
anon_1.id_streets_id_street AS anon_1_id_streets_id_street,
anon_1.id_streets_name_street AS anon_1_id_streets_name_street,
anon_1.id_streets_type_street AS anon_1_id_streets_type_street,
anon_1.id_nps_id_region AS anon_1_id_nps_id_region,
anon_1.id_nps_id_atu AS anon_1_id_nps_id_atu,
anon_1.id_nps_id_selsov AS anon_1_id_nps_id_selsov,
anon_1.id_nps_id_np AS anon_1_id_nps_id_np,
anon_1.id_nps_name_np AS anon_1_id_nps_name_np,
anon_1.id_nps_type_np AS anon_1_id_nps_type_np,
anon_1.id_nps_okato AS anon_1_id_nps_okato,
anon_1.id_nps_oktmo AS anon_1_id_nps_oktmo,
anon_1.id_streets_history_id_np AS anon_1_id_streets_history_id_np,
anon_1.id_streets_history_id_street AS anon_1_id_streets_history_id_street,
anon_1.id_streets_history_id_row AS anon_1_id_streets_history_id_row,
anon_1.id_streets_history_name_street AS anon_1_id_streets_history_name_street,
anon_1.id_streets_history_type_street AS anon_1_id_streets_history_type_street,
anon_1.id_streets_history_code_reason AS anon_1_id_streets_history_code_reason,
anon_1.id_streets_history_code_detail AS anon_1_id_streets_history_code_detail,
anon_1.id_streets_history_creater AS anon_1_id_streets_history_creater,
anon_1.id_streets_history_createddate AS anon_1_id_streets_history_createddate,
anon_1.id_streets_history_updater AS anon_1_id_streets_history_updater,
anon_1.id_streets_history_updateddate AS anon_1_id_streets_history_updateddate,
anon_1.id_streets_history_recordcomment AS anon_1_id_streets_history_recordcomment,
anon_1.id_streets_history_sourcecomment AS anon_1_id_streets_history_sourcecomment,
anon_1.modify_reason_code_reason AS anon_1_modify_reason_code_reason,
anon_1.modify_reason_code_detail AS anon_1_modify_reason_code_detail,
anon_1.modify_reason_comment_reason AS anon_1_modify_reason_comment_reason
FROM (SELECT id_streets.id_np AS id_streets_id_np,
id_streets.id_street AS id_streets_id_street,
id_streets.name_street AS id_streets_name_street,
id_streets.type_street AS id_streets_type_street,
id_nps.id_region AS id_nps_id_region,
id_nps.id_atu AS id_nps_id_atu,
id_nps.id_selsov AS id_nps_id_selsov,
id_nps.id_np AS id_nps_id_np,
id_nps.name_np AS id_nps_name_np,
id_nps.type_np AS id_nps_type_np,
id_nps.okato AS id_nps_okato,
id_nps.oktmo AS id_nps_oktmo,
id_streets_history.id_np AS id_streets_history_id_np,
id_streets_history.id_street AS id_streets_history_id_street,
id_streets_history.id_row AS id_streets_history_id_row,
id_streets_history.name_street AS id_streets_history_name_street,
id_streets_history.type_street AS id_streets_history_type_street,
id_streets_history.code_reason AS id_streets_history_code_reason,
id_streets_history.code_detail AS id_streets_history_code_detail,
id_streets_history.creater AS id_streets_history_creater,
id_streets_history.createddate AS id_streets_history_createddate,
id_streets_history.updater AS id_streets_history_updater,
id_streets_history.updateddate AS id_streets_history_updateddate,
id_streets_history.recordcomment AS id_streets_history_recordcomment,
id_streets_history.sourcecomment AS id_streets_history_sourcecomment,
modify_reason.code_reason AS modify_reason_code_reason,
modify_reason.code_detail AS modify_reason_code_detail,
modify_reason.comment_reason AS modify_reason_comment_reason
FROM id_streets
JOIN id_nps ON id_nps.id_np = id_streets.id_np
LEFT OUTER JOIN id_streets_history ON (
ID_Streets.id_np=ID_Streets_history.id_np
AND ID_Streets.id_street=ID_Streets_history.id_street)
LEFT OUTER JOIN modify_reason ON (
ID_Streets_history.code_reason=Modify_reason.code_reason
AND ID_Streets_history.code_detail=Modify_reason.code_detail)
GROUP BY id_streets.id_np, id_streets.id_street, id_nps.id_region, id_nps.id_atu, id_nps.id_selsov, id_nps.id_np, id_streets_history.id_np, id_streets_history.id_street, id_streets_history.id_row, modify_reason.code_reason, modify_reason.code_detail
UNION DISTINCT SELECT
id_nps.id_region AS id_nps_id_region, id_nps.id_atu AS id_nps_id_atu,
id_nps.id_selsov AS id_nps_id_selsov, id_nps.id_np AS id_nps_id_np,
id_nps.name_np AS id_nps_name_np, id_nps.type_np AS id_nps_type_np,
id_nps.okato AS id_nps_okato, id_nps.oktmo AS id_nps_oktmo,
id_streets_history.id_np AS id_streets_history_id_np,
id_streets_history.id_street AS id_streets_history_id_street,
id_streets_history.id_row AS id_streets_history_id_row,
id_streets_history.name_street AS id_streets_history_name_street,
id_streets_history.type_street AS id_streets_history_type_street,
id_streets_history.code_reason AS id_streets_history_code_reason,
id_streets_history.code_detail AS id_streets_history_code_detail,
id_streets_history.creater AS id_streets_history_creater,
id_streets_history.createddate AS id_streets_history_createddate,
id_streets_history.updater AS id_streets_history_updater,
id_streets_history.updateddate AS id_streets_history_updateddate,
id_streets_history.recordcomment AS id_streets_history_recordcomment,
id_streets_history.sourcecomment AS id_streets_history_sourcecomment,
modify_reason.code_reason AS modify_reason_code_reason,
modify_reason.code_detail AS modify_reason_code_detail,
modify_reason.comment_reason AS modify_reason_comment_reason,
NULL AS id_streets_id_np,
NULL AS id_streets_id_street,
NULL AS id_streets_name_street,
NULL AS id_streets_type_street
FROM id_streets_history
LEFT OUTER JOIN id_nps ON id_nps.id_np = id_streets_history.id_np
LEFT OUTER JOIN modify_reason ON (
ID_Streets_history.code_reason=Modify_reason.code_reason
AND ID_Streets_history.code_detail=Modify_reason.code_detail)
GROUP BY id_nps.id_region, id_nps.id_atu, id_nps.id_selsov, id_nps.id_np, id_streets_history.id_np, id_streets_history.id_street, id_streets_history.id_row, modify_reason.code_reason, modify_reason.code_detail
)AS anon_1