mysql 按小时聚合多个列
快速浏览了一下,却找不到我的问题了。所以我希望这里有人能提供帮助。 我有一张大表,其结构与下面类似。
日期时间 |入站用户 ID |出站用户 ID |客户用户 ID |成本|分钟 |帐号
我试图将上述数据按小时分组,并将结果存储在另一个具有与下面类似结构的表中。
日期 |小时从 |小时至 |总和 |总成本 |出站用户 ID |客户用户 ID |帐号
我已经编写了以下代码/查询来执行此操作,但它不太正确。基本上,该组并不是对所有数据进行汇总。例如,当查看新创建的表时,我拥有所有 OutboundUserID 数据,但没有 CustomerUserID 数据。
for(int i=0;i <=23;i++)
{
for(int j=1 ; j <=31 ; j++)
{
String Time = String.format("%02d", i);
query = "INSERT DELAYED INTO correlated_calls_hourly " +
" SELECT DateOnly,HourFrom,HourTo,DURATION,CALLCOUNT,`Price Group ID`,INBOUNDCOST,INBOUNDREVENUE,`InboundCustomerID`,OUTBOUNDCOST,OUTBOUNDREVENUE,`Outbound Customer ID`,`Customer ID`,`Account Number`,`Service` FROM" +
" ("+
" SELECT "+
/* snip sum(),case and date/time part of query */
" GROUP BY DateOnly,`InboundCustomerID`,`OutboundCustomerID`,`CustomerID`,`AccountNumber`,`PriceGroupID`,`Service`" +
" )a ";
}
}
我已经采取了创建三个不同表的步骤,以按 OutboundUserID/InboundUserID/CustomerUserID 分别汇总数据。但如果我能让这张汇总表正常工作,那就优雅多了。
如果有人能帮助我想出一种通过单个 sql 查询获得所需结果的方法,我将不胜感激。
预先非常感谢
艾伦
Had a quick look through and couldn't find my question again. So I'm hoping someone here can help.
I have a large table with a similar structure to the below.
DateTime | InboundUserID | OutboundUserID | CustomerUserID | Cost | Mins | Account Number
I'm trying to group the above data into hourly chunks and store the result in another table with a similar structure to below.
Date | HourFrom | HourTo | SumMins | SumCost | OutboundUserID | CustomerUserID | Account Number
I've written the following code / query in order to do this but it's not quite right. Basically the group by means not all the data gets summarised. For instance when looking at the newly created table I have all the OutboundUserID data, but none of the CustomerUserID data.
for(int i=0;i <=23;i++)
{
for(int j=1 ; j <=31 ; j++)
{
String Time = String.format("%02d", i);
query = "INSERT DELAYED INTO correlated_calls_hourly " +
" SELECT DateOnly,HourFrom,HourTo,DURATION,CALLCOUNT,`Price Group ID`,INBOUNDCOST,INBOUNDREVENUE,`InboundCustomerID`,OUTBOUNDCOST,OUTBOUNDREVENUE,`Outbound Customer ID`,`Customer ID`,`Account Number`,`Service` FROM" +
" ("+
" SELECT "+
/* snip sum(),case and date/time part of query */
" GROUP BY DateOnly,`InboundCustomerID`,`OutboundCustomerID`,`CustomerID`,`AccountNumber`,`PriceGroupID`,`Service`" +
" )a ";
}
}
I've already taken the step of creating three different tables to summarise the data separately by OutboundUserID/ InboundUserID / CustomerUserID. But it would be a lot more elegant if I could just get this one summarised table working correctly.
If anyone could help me think of a way to get the desired result via a single sql query I'd be greatly appreciative.
Many thanks in advance
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西会起作用吗?
转换函数将日期时间转换为日期。您可能需要对其进行一些调整以适应您的 RDBMS,但我看不出有任何理由不起作用。
Would something like this work?
The convert function is convert the datetime to just a date. You'd probably need to tweak it a little to suit your RDBMS but I can't see any reason why that wouldn't work.
经过一点灵感后,我意识到解决方案是显而易见的。将列连接在一起并对连接的列进行分组。
CONCAT(InboundUserID,'',OutboundUserID,'',CustomerUserID) as uniqueKey
按唯一键分组。
After a little inspiration I realised the solution was obvious. Concat the columns together and do a group by on the concatenated column.
CONCAT(InboundUserID,'',OutboundUserID,'',CustomerUserID) as uniqueKey
GROUP BY uniqueKey.