Я создаю фиктивное хранилище данных и пытаюсь объединить 2 отдельных представления, каждое представление работает нормально само по себе.Однако при объединении в моей группе есть что-то, что приводит к неверным данным.
create view CentreSizing as
select CentreID, Name,
case
when Capacity between 0 and 199 then 'Small'
when Capacity between 200 and 900 then 'Medium'
else 'Large'
end as Size
from Centre;
create view sizeQuantities as
select cs.Size, count(*) as "Occurences" from
CentreSizing cs
group by cs.Size;
create view AvgSpendings2018 as
select cs.Size, monthname(d.DateStamp) as "Month", sum(e.Cost)/quant.Occurences as
"AverageExpense"
from Expense e join CentreSizing cs on e.CentreID = cs.CentreID
join DateTime d on e.DateTimeID = d.DateTimeID
join sizeQuantities quant on quant.Size = cs.Size
where YEAR(d.DateStamp) = 2018
group by cs.Size, MONTH(d.DateStamp);
create view TotalSpendings2018 as
select e.CentreID, c.Name, cs.Size , monthname(d.DateStamp) as "Month", sum(e.Cost) as
"TotalExpense" from Expense e
left join DateTime d on e.DateTimeID = d.DateTimeID
join Centre c on c.CentreID = e.CentreID
join CentreSizing cs on c.CentreID = cs.CentreID
where year(d.DateStamp) = 2018
group by month(d.DateStamp), e.CentreID;
Все создания представлений работают, как и ожидалось, самостоятельно, однако, когда я пытаюсь объединитьрезультаты, показывающие общие расходы за месяц, по сравнению со средним значением для центров аналогичного размера, результаты не такие, как ожидалось.
select ts.CentreID, ts.Name, ts.Size, ts.Month, avg.AverageExpense
from TotalSpendings2018 ts join AvgSpendings2018 avg on
ts.Size = avg.Size
Group by ts.CentreID, ts.Size, ts.Month;
| CentreID | Name | Size | Month | AverageExpense
|
+----------+-------------------------------------+--------+-----------+----------------
+
| 1 | Queen Alexandra Hospital | Large | April | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | August | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | December | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | February | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | January | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | July | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | June | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | March | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | May | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | November | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | October | 1490.0000
|
| 1 | Queen Alexandra Hospital | Large | September | 1490.0000
|
| 2 | Southampton General Hospital | Large | April | 1490.0000
|
| 2 | Southampton General Hospital | Large | August | 1490.0000
|
| 2 | Southampton General Hospital | Large | December | 1490.0000
|
| 2 | Southampton General Hospital | Large | February | 1490.0000
|
| 2 | Southampton General Hospital | Large | January | 1490.0000
|
| 2 | Southampton General Hospital | Large | July | 1490.0000
|
| 2 | Southampton General Hospital | Large | June | 1490.0000
|
| 2 | Southampton General Hospital | Large | March | 1490.0000
|
| 2 | Southampton General Hospital | Large | May | 1490.0000
|
| 2 | Southampton General Hospital | Large | November | 1490.0000
|
| 2 | Southampton General Hospital | Large | October | 1490.0000
|
| 2 | Southampton General Hospital | Large | September | 1490.0000
|
| 3 | St Richard's Hospital | Medium | April | 445.0000
|
| 3 | St Richard's Hospital | Medium | August | 445.0000
|
| 3 | St Richard's Hospital | Medium | December | 445.0000
|