如何解决更改Mysql SQL命令的输出样式分组
如何在此sql命令上创建过滤器视图?
select t1.ean,t2.date,t2.hour,(t1.consumption *
(case when type = 'B' then type_b_coefficient
when type = 'A' and region = 'EAST' then type_a_east_coefficient
when type = 'A' and region = 'WEST' then type_a_west_coefficient
else 1
end) *
(case when category = 11 then cat_11_coefficient
when category = 21 then cat_21_coefficient
else 1
end)
) as net_consumption
from table1 t1 join
table2 t2
on t2.date = t1.from_date
现在查询显示EAN /小时/天/消耗量。我需要表演 EAN /天 天 以及DAY和Hour。
如果我通过查询更改GROUP会得出错误的数学结果。在此命令中,所有数学运算都很好
过滤器含义:
现在查询显示如下输出:
ean day hour net_consumption
1234 2020-01-01 1 1.12312312
1234 2020-01-01 2 0.23038382
2838 2020-01-01 1 2.3043043
以及如何处理此视图-
ean day net_consumption
1234 2020-01-01 10.12312312
5678 2020-01-01 8.12312312
2838 2020-01-01 5.22392229
或-
day net_consumption
2020-01-01 120.12312312
2020-01-02 68.12312312
2020-01-03 30.22392229
如果我使用GROUP BY执行此操作,则会得出错误的数学结果(例如,如果我手动进行计数,则错误的计数消耗会低得多。第一个查询有效)
解决方法
参与JOIN
的汇总可能比预期的大。
这就是发生的事情。
- 将这些表结合起来,以[逻辑地]创建具有适当行组合的临时表。
- 聚合是基于此[可能更大]的临时表计算的。
- 然后
GROUP BY
缩小行数。
通常,解决数字膨胀(以及性能下降)的方法是首先用尽可能少的列和尽可能少的表进行聚合和分组。然后做
SELECT ...
FROM ( that query to get aggregates ) AS x
JOIN table(s) ON ... -- to get other columns
,
您应该尝试以下类似操作
Select
ean,date,Sum(net_consumption) as net_consumption
from
(
select
t1.ean,t2.date,t2.hour,t1.consumption *
case when type = 'B' then type_b_coefficient
when type = 'A' and region = 'EAST' then type_a_east_coefficient
when type = 'A' and region = 'WEST' then type_a_west_coefficient
else 1
end *
case when category = 11 then cat_11_coefficient
when category = 21 then cat_21_coefficient
else 1
end
as net_consumption
from table1 t1 join
table2 t2
on t2.date = t1.from_date
)T
group by ean,date
或
Select
date,t1.consumption *
case when type = 'B' then type_b_coefficient
when type = 'A' and region = 'EAST' then type_a_east_coefficient
when type = 'A' and region = 'WEST' then type_a_west_coefficient
else 1
end *
case when category = 11 then cat_11_coefficient
when category = 21 then cat_21_coefficient
else 1
end
as net_consumption
from table1 t1 join
table2 t2
on t2.date = t1.from_date
)T
group by date
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。