Этот тип запроса свидетельствует о плохой конструкции таблицы.
В краткосрочной перспективе вы можете написать небольшую программу, которая выполнит за вас повторяющуюся работу.Вот пример в Ruby.
#!/usr/bin/env ruby
tables = [
'global_sales',
'regional_sales',
'local_sales'
]
def table2column(table)
return table.split('_')
.map { |word| word[0].upcase!; word }
.join(" ")
end
puts tables.map { |table|
column = table2column(table)
"select item, price, '#{column}' from #{table}\n"
}.join("union all\n")
Предположим, что у нас все продажи классифицированы из-за разницы в позициях, предлагаемых для продажи, в зависимости от местоположения.
Ваш дизайн стола может быть улучшен.Если все остальные данные равны, все продажи могут храниться в одной таблице с самым узким местоположением.
create table sales (
id integer primary key,
item integer references items(id),
price numeric(10,2) not null,
location integer references locations(id)
);
create table locations (
id integer primary key,
name text,
... etc ...
);
И таблица для указания, в каком регионе находится каждое местоположение.
create table regions (
id integer primary key,
name text,
... etc ...
);
create table regional_locations (
id integer primary key,
location integer references locations(id),
region integer references regions(id)
);
Тогда получить глобальные продажи очень просто.
select item, price from sales;
И продажи для одного региона могут быть.
select item, price, r.name, l.name
from sales s
-- Get the region for each location
join regional_locations rl on rl.location = s.location
-- Get the region name
and regions r on rl.region = r.id
-- Get the location name
and locations l on s.location = l.id
where rl.region = ?
Для обратной совместимости каждая старая таблица становится представлением.Например ...
create view global_sales
select id, item, price from sales;