mysql - 是否可以将 2 个具有重复数据的 View 连接起来?

标签 mysql sql

我正在创建一个虚拟数据仓库并尝试加入 2 个单独的 View ,每个 View 都可以单独工作。但是,当组合起来时,我的 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;

所有 View 创建都按预期工作,但是当我尝试合并结果以显示每月总费用时,与类似规模的中心的平均费用相比,结果并不如预期。

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 
|

最佳答案

查看 TotalSpendings2018 在 GROUP BY 子句中缺少 c.Name(如果每个中心都有唯一的名称,那应该没问题)。虽然与大多数其他 RDBMS 不同,mysql 允许这样做,但这很容易出错并且不是良好的编码实践。

关于查询:

  • 在我看来,您实际上不需要 GROUP BY,因为没有使用聚合函数(否则:GROUP BY 中缺少 ts.Name 和 avg.AverageExpense)
  • 根据 View 的描述,我还猜测您缺少“月份”的联接条件,因为此列在两个 View 中都可用
  • 为什么不在输出中显示 TotalExpense?我本以为这就是您想要做的(否则,您不需要加入)

我(希望受过教育)对您的查询的猜测:

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

关于mysql - 是否可以将 2 个具有重复数据的 View 连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53729309/

相关文章:

php - 如何进行查询以找到字符串中的特定文本并抓取其旁边的部分?

mysql - Like 查询中的规范

sql - 如何跨多个数据库查找不为空的特定表(大多数/所有)

mysql - 如何过滤JSON - mysql中的两个属性?

java - hibernate sql查询

mysql - 可以引用另一个表作为表中的一行吗?

mysql - 合并/同步两个 Joomla 数据库 - OSX

php - 有没有 MySQLOData 的文档?

sqlite3 : how to select row's presented only in first table (with other table join)

sql - mySQL order by clause奇怪的问题