unionを使用して3つのクエリの数を合計しようとしています。これらのクエリには、groupby句も含まれています。以下は私が書いた私の質問です:
select
extract(year from start_date),
extract(month from start_date),
APPLICATION_TYPE,
sum(TOTAL) as Overall_TOTAL
from (
select
extract(year from A.start_date) as Start_Year,
extract(month from A.start_date) as Start_Month,
A.APPLICATION_TYPE as Type,
count(A.TRANSACTION_NUMBER) as Total
from lnr_application A
where
A.START_DATE >= to_date('&sdate','DD/MM/YYYY')
and A.START_DATE <= to_date('&edate','DD/MM/YYYY')
and A.permission_type = 'HRW'
and A.status_cd in ('AP')
group by extract(year from start_date), extract(month from start_date), A.APPLICATION_TYPE
union all
select
extract (year from A.tstamp) as Start_Year,
extract (month from A.tstamp) as Start_Month,
A.application_type as Type,
count(A.transaction_number) as Total
from lnr_application A
where
A.permission_type = 'HRW'
and A.status_cd in ('RF')
and trunc(A.tstamp) >= to_date ('&sdate','dd/mm/yyyy')
and trunc(A.tstamp) <= to_date ('&edate','dd/mm/yy')
group by extract (year from A.tstamp), extract (month from A.tstamp), A.application_type
union all
select
extract (year from A.tstamp) as Start_Year,
extract (month from A.tstamp) as Start_Month,
A.application_type as Type,
count(A.transaction_number) as Total
from lnr_application A
where
A.permission_type = 'HRW'
and A.status_cd in ('CL')
and trunc(A.tstamp) >= to_date ('&sdate','dd/mm/yyyy')
and trunc(A.tstamp) <= to_date ('&edate','dd/mm/yy')
group by extract (year from A.tstamp), extract (month from A.tstamp), A.application_type
) tmp
group by extract(year from start_date), extract(month from start_date), APPLICATION_TYPE
order by extract(year from start_date), extract(month from start_date), APPLICATION_TYPE
クエリを実行するStart_Date
と、無効な識別子であるエラーメッセージが表示されます。合計コンポーネントを上から削除すると、つまり3つのクエリすべてを結合すると、次の結果が得られます。
2011 7 A 627
2011 7 A 21
2011 7 A 1
2011 7 C 1585
2011 7 C 1
2011 7 I 1
2011 7 I 154
2011 7 I 3
以下のように、それぞれの年、月、およびアプリケーションタイプのカウントの合計を合計したいと思います。
2011 7 A 649
2011 7 C 1586
2011 7 I 158
誰か助けてくれませんか?