Вам не нужна таблица PROD здесь. В моей схеме SH у меня нет PROD_ID = 5, поэтому я использовал 13.
select * from (
select t.calendar_month_name, t.day_number_in_week,
sum(s.amount_sold) amount_sold
from sales s
join times t on t.time_id = s.TIME_ID
where
s.prod_id = 13 and
t.calendar_year = 2000 and
t.calendar_quarter_number = 2
group by t.calendar_month_name, t.day_number_in_week
having sum(s.amount_sold) > 1
)
pivot(sum(amount_sold) for day_number_in_week in (
1 as MONDAY,
2 as TUESDAY,
3 as WEDNESDAY,
4 as THURSDAY,
5 as FRIDAY,
6 as SATURDAY,
7 as SUNDAY
))
order by 1;
CALENDAR_ MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY
--------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
April 20792,32 35724,12 43189,77 16688,08 11436,9 49960,37
June 27037,04 16809,54 11504,03 37709,26 23923,83 35908,33
May 123982,43 18773,74 5283,56 21167,23
Если бы не было фильтра HAVING, вам бы не понадобилось GROUP BY во встроенном представлении, потому что неявной группировки предложения PIVOT будет достаточно:
select * from (
select t.calendar_month_name, t.day_number_in_week,
s.amount_sold
from sales s
join times t on t.time_id = s.TIME_ID
where
s.prod_id = 13 and
t.calendar_year = 2000 and
t.calendar_quarter_number = 2
)
pivot(sum(amount_sold) for day_number_in_week in (
1 as MONDAY,
2 as TUESDAY,
3 as WEDNESDAY,
4 as THURSDAY,
5 as FRIDAY,
6 as SATURDAY,
7 as SUNDAY
))
order by 1;
С уважением,
Рагу Эштон