SQL总结季度产生的太阳能
我有一个具有10年太阳能电池板生成数据的MySQL表(每10分钟,生成为零)。
我不知道如何构建一个查询,该查询每年将为我提供四个值,每个值代表当年每个季度的总数。
我的源表(DTP)架构看起来像这样:
#, Field, Type, Null, Key, Default, Extra
1, 'PWR', 'decimal(5,3)', 'NO', '', NULL, ''
2, 'idDTP', 'int(11)', 'NO', 'PRI', NULL, 'auto_increment'
3, 'DT', 'datetime', 'NO', '', NULL, ''
一个例子我可能会指向正确的方向(请记住,这部分是从源中修改的):
SELECT Year,SUM(Quarter1) AS Quarter1,SUM(Quarter2) AS Quarter2,SUM(Quarter3) AS Quarter3,SUM(Quarter4) AS Quarter4
FROM
(
SELECT YEAR(DT) AS 'Year' ,
Quarter1 = CASE(DATEPART(q, DTP.DT))
WHEN 1 THEN SUM(DTP.DT)
ELSE 0
END,
Quarter2 = CASE(DATEPART(q, DTP.DT))
WHEN 2 THEN SUM(DTP.DT)
ELSE 0
END,
Quarter3 = CASE(DATEPART(q, DTP.DT))
WHEN 3 THEN SUM(DTP.DT)
ELSE 0
END,
Quarter4 = CASE(DATEPART(q, DTP.DT))
WHEN 4 THEN SUM(DTP.DT)
ELSE 0
END
FROM DTP LEFT JOIN PWR ON DTP.DT = Customers.CustomerID
LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID
GROUP BY CompanyName, YEAR(OrderDate), DATEPART(q, OrderDate)
)C
GROUP BY CompanyName,Year
我开始尝试将其转换为我的场景,但没有理解它,我已经完成了一半(假设这甚至在正确的行上)混乱,我不确定如何修复它。任何解释的提示或链接都将受到欢迎 - 火焰,更少。 ; OP
如果我有错误的论坛,请礼貌地指出合适的论坛 - 在堆栈搜索列表中找不到更好的选择。
谢谢 源数据的第一年(2月-MAR)的小样本
'160851', '2012-02-29 08:00:00', '0.030'
'160852', '2012-02-29 08:10:00', '0.066'
'160853', '2012-02-29 08:20:00', '0.072'
'160854', '2012-02-29 08:30:00', '0.060'
'160855', '2012-02-29 08:40:00', '0.090'
'160856', '2012-02-29 08:50:00', '0.102'
'160857', '2012-02-29 09:00:00', '0.084'
'160858', '2012-02-29 09:10:00', '0.132'
'160859', '2012-02-29 09:20:00', '0.144'
'160860', '2012-02-29 09:30:00', '0.138'
'160861', '2012-02-29 09:40:00', '0.150'
'160862', '2012-02-29 09:50:00', '0.174'
'160863', '2012-02-29 10:00:00', '0.174'
'160864', '2012-02-29 10:10:00', '0.162'
我无法输入价值数年的数据,因为毫无疑问,它超过了允许的计数,但它以类似的方式进行。
没有有意义的输出,因为我在接近可行的代码之类的任何东西之前就离开了深度。
I have a single MySQL table with 10 years of solar panel generation data (every 10 minutes, where generation is > zero).
I have no idea how to construct a query that will give me four values per year row, each value representing the summed total for each quarter in that year.
My source table (DTP) schema looks like this:
#, Field, Type, Null, Key, Default, Extra
1, 'PWR', 'decimal(5,3)', 'NO', '', NULL, ''
2, 'idDTP', 'int(11)', 'NO', 'PRI', NULL, 'auto_increment'
3, 'DT', 'datetime', 'NO', '', NULL, ''
One example I though might point me in the right direction looks like this (bear in mind this is partly modified from the source):
SELECT Year,SUM(Quarter1) AS Quarter1,SUM(Quarter2) AS Quarter2,SUM(Quarter3) AS Quarter3,SUM(Quarter4) AS Quarter4
FROM
(
SELECT YEAR(DT) AS 'Year' ,
Quarter1 = CASE(DATEPART(q, DTP.DT))
WHEN 1 THEN SUM(DTP.DT)
ELSE 0
END,
Quarter2 = CASE(DATEPART(q, DTP.DT))
WHEN 2 THEN SUM(DTP.DT)
ELSE 0
END,
Quarter3 = CASE(DATEPART(q, DTP.DT))
WHEN 3 THEN SUM(DTP.DT)
ELSE 0
END,
Quarter4 = CASE(DATEPART(q, DTP.DT))
WHEN 4 THEN SUM(DTP.DT)
ELSE 0
END
FROM DTP LEFT JOIN PWR ON DTP.DT = Customers.CustomerID
LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID
GROUP BY CompanyName, YEAR(OrderDate), DATEPART(q, OrderDate)
)C
GROUP BY CompanyName,Year
I started trying to convert it to my scenario but without understanding it, I've landed myself in a half finished (assuming this is even on the right lines) mess and I'm not sure how to fix it. Any well explained hints or links would be welcome - flames, less so. ;oP
If I've got the wrong forum, please politely point out the right one - couldn't see a better alternative in the Stack-Echange list.
Thanks
Small sample from first year of source data (Feb-Mar)
'160851', '2012-02-29 08:00:00', '0.030'
'160852', '2012-02-29 08:10:00', '0.066'
'160853', '2012-02-29 08:20:00', '0.072'
'160854', '2012-02-29 08:30:00', '0.060'
'160855', '2012-02-29 08:40:00', '0.090'
'160856', '2012-02-29 08:50:00', '0.102'
'160857', '2012-02-29 09:00:00', '0.084'
'160858', '2012-02-29 09:10:00', '0.132'
'160859', '2012-02-29 09:20:00', '0.144'
'160860', '2012-02-29 09:30:00', '0.138'
'160861', '2012-02-29 09:40:00', '0.150'
'160862', '2012-02-29 09:50:00', '0.174'
'160863', '2012-02-29 10:00:00', '0.174'
'160864', '2012-02-29 10:10:00', '0.162'
I can't enter a years worth of data, as it unsurprisingly exceeds the allowed count but it proceeds in a similar vein.
There's no meaningful output because I got out of my depth well before I'd approached anything like viable code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义新的四分之一列,名为
yq
。用
“ 2021-q4”
和“ 2022-q1”
之类的值填充它。现在您的问题很简单。
只需计算
sum(...)
使用
组成的组
。Define a new year-quarter column, named
yq
.Fill it with values like
"2021-q4"
and"2022-q1"
.Now your problem is simple.
Just compute
SUM( ... )
with
GROUP BY yq
.您缺少一些桌子,但我将它们保留在子查询中。
款项将显示所有公司和年份的所有Quaters的数字。
外部查询,将它们总结成几年,而无需公司。
由于您需要更多的列,您可以从comapanies订单或订单详细信息中添加它们,然后将它们添加到Inner
选择
中,然后还将它们添加外部,为他们按订单或其他情况下的订单和订单和订单进行审查
You are missing some tables, but i kept them in the subquery.
the subwquery, would show the numbers for all quaters , for every company and year.
The outer query, would, sum them up into years without companies..
As you need more columns you can add them from comapanies orders or order details and add them in the inner
SELECT
and then add them also in the outer, to group them for them examle by orders or suchWithout the customer and orders