Как отмечалось в комментарии выше, я, вероятно, не понимаю вашу модель - поэтому вот простая для начала.
Теперь, если я хочу, чтобы еженедельные продажи за календарный год 2010
select
CalendarYearWeek
, sum(SalesAmount)
from factSales as f
join dimDate as d on d.DateKey = f.DateKey
where Year = 2010
group by CalendarYearWeek
CalendarYearWeek
- это столбец в dimDate, varchar (8), например, '2010-w03', Year
- это целочисленный столбец вdimDate тоже.
Не уверен, что это близко к тому, что вы искали, но может быть началом.
РЕДАКТИРОВАТЬ
dimDate также имеет следующие столбцы:
WeekNumberInEpoch , целое число - увеличивает увеличиваетсяначиная с какой-то эпохи в прошлом.Все строки в dimDate за одну и ту же неделю имеют одинаковое значение WeekNumberInEpoch.
DayOfWeek , varchar (10) - 'воскресенье', 'понедельник', ...
DayNumberInWeek , целое число - 1-7
Использует CTE, должно работать с последними версиями PostgreSQL, SQL Server, Oracle, DB2.Для других вы можете упаковать CTE (q_00) в подзапрос.
-- for week to previous week
with
q_00 as (
select
WeekNumberInEpoch
, sum(SalesAmount) as Amount
from factSale as f
join dimDate as d on d.DateKey = f.DateKey
where CalendarYear = 2010
group by WeekNumberInEpoch
)
select
a.WeekNumberInEpoch
, a.Amount as ThisWeekSales
, b.Amount as LastWeekSales
, a.Amount - b.Amount as Difference
from q_00 as a
join q_00 as b on b.WeekNumberInEpoch = a.WeekNumberInEpoch - 1
order by a.WeekNumberInEpoch desc ;
-- for day of week to day of previous week
-- monday to monday, tuesday to tuesday, ...
with
q_00 as (
select
WeekNumberInEpoch
, DayOfWeek
, sum(SalesAmount) as Amount
from factSale as f
join dimDate as d on d.DateKey = f.DateKey
where CalendarYear = 2010
group by WeekNumberInEpoch, DayOfWeek
)
select
a.WeekNumberInEpoch
, a.DayOfWeek
, a.Amount as ThisWeekSales
, b.Amount as LastWeekSales
, a.Amount - b.Amount as Difference
from q_00 as a
join q_00 as b on (b.WeekNumberInEpoch = a.WeekNumberInEpoch - 1
and b.DayOfWeek = a.DayOfWeek)
order by a.WeekNumberInEpoch desc, a.DayOfWeek ;
-- Sliding by day and day difference (= 7)
with
q_00 as (
select
DayNumberInEpoch
, FullDate
, DayOfWeek
, sum(SalesAmount) as Amount
from factSale as f
join dimDate as d on d.DateKey = f.DateKey
where CalendarYear = 2010
group by DayNumberInEpoch, FullDate, DayOfWeek
)
select
a.FullDate as ThisDay
, a.DayOfWeek as ThisDayName
, a.Amount as ThisDaySales
, b.FullDate as PreviousPeriodDay
, b.DayOfWeek as PreviousDayName
, b.Amount as PreviousPeriodDaySales
, a.Amount - b.Amount as Difference
from q_00 as a
join q_00 as b on b.DayNumberInEpoch = a.DayNumberInEpoch - 7
order by a.FullDate desc ;