Проблема во второй части вашего запроса: столбец year
должен быть либо сгруппирован в предложении group by
, либо агрегирован в операторе select
:
Другие столбцы (qtr
, Area
, industry
и ownership
) могут быть пропущены, поскольку вы предоставляете для них явные значения, но столбец year` берется из таблицы и должен обрабатываться соответствующим образом.
Решение 1
Группа year
столбец (добавление group by (year)
в конце запроса):
select year, qtr, area, industry, ownership, employment
from indstry
where ( area = '32001' or area = '32005' or area = '32007' or area =
'32009' or area = '32011' or area = '32013' or area ='32015' or area =
'32017' or area = '32019' or area = '32021' or area = '32023' or area =
'32027' or area = '32033' ) and year ='2016'
union all
select year, NULL as qtr, '99999' as Area, NULL as industry, NULL as
ownership, sum (employment) as employment
From industry
where ( area = '32001' or area = '32005' or area = '32007' or area =
'32009' or area = '32011' or area = '32013' or area ='32015' or area =
'32017' or area = '32019' or area = '32021' or area = '32023' or area =
'32027' or area = '32033' ) and year ='2016'
group by (year)
Решение 2
Столбец агрегированного года с max
, min
, avg
и т. Д. (Например, изменение year
на max(year)
в операторе select
второй части вашего запроса):
select year, qtr, area, industry, ownership, employment
from indstry
where ( area = '32001' or area = '32005' or area = '32007' or area =
'32009' or area = '32011' or area = '32013' or area ='32015' or area =
'32017' or area = '32019' or area = '32021' or area = '32023' or area =
'32027' or area = '32033' ) and year ='2016'
union all
select max(year), NULL as qtr, '99999' as Area, NULL as industry, NULL as
ownership, sum (employment) as employment
From industry
where ( area = '32001' or area = '32005' or area = '32007' or area =
'32009' or area = '32011' or area = '32013' or area ='32015' or area =
'32017' or area = '32019' or area = '32021' or area = '32023' or area =
'32027' or area = '32033' ) and year ='2016'