我正在创建一个虚拟数据仓库,并尝试加入2个单独的视图,每个视图都可以正常工作。但是,当合并时,group by语句中似乎有一些东西导致数据不正确。
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;
所有视图创建都按预期的方式工作,但是,当我尝试合并结果以显示每月的总支出时,与类似规模的中心的平均支出相比,结果并不符合预期。< / p>
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
|
答案 0 :(得分:1)
视图TotalSpendings2018在GROUP BY子句中缺少c.Name(如果每个中心都有唯一的名称,那应该没问题)。尽管与大多数其他RDBMS不同,mysql允许这样做,但它易于出错,并且不是良好的编码习惯。
关于查询:
我对您的查询的期望值:
select
ts.CentreID,
ts.Name,
ts.Size,
ts.Month,
avg.AverageExpense,
ts.TotalExpense
from
TotalSpendings2018 ts
join AvgSpendings2018 avg
on ts.Size = avg.Size
and ts.Month = avg.Month