mysql - 连接引用表后SUM的结果问题

时间:2018-06-05 11:01:18

标签: mysql

早上好,

我遇到了查询问题。我试图在UTC时间范围内对列进行求和。当我仅使用确切的UTC值查询源表时,它正在按预期工作。一旦我添加了一个连接,一个调度表添加了一些结果不正确的详细信息。下面是我正在使用的查询和表结构。任何帮助将不胜感激。

查询1:

select logid,SUM(ti_availtime) as "Total Avail TIme" 
from status
where logid = 12345 
and  status.starttime_utc 
between 1528070400 and 1528102800
group by logid

logid   Total Avail Time
12345   20106

状态表的表结构:

Date    Logid   Starttime_utc   Ti_availtime    Interval
2018-06-04  12345   1528070400  106 30
2018-06-04  12345   1528070401  5000    30
2018-06-04  12345   1528070402  5000    30
2018-06-04  12345   1528102800  10000   30

注意:UTC值(1528070401,1528070402)只是示例*

时间表的表格结构:

Date    Logid   Name    Starttime_utc   Stoptime_utc
2018-06-04  12345   John Smith  1528070400  1528102800

查询2:

select schedule.Analyst, SUM(status.ti_availtime) 
from status
join schedule on status.logid = schedule.logid
where schedule.logid = 30120 and  status.starttime_utc between schedule.starttime_utc and schedule.starttime_utc 
group by vqschedule.row_date

结果:

Analyst Total Avail Time
John Smith  1351027

2 个答案:

答案 0 :(得分:2)

完全同意@jspcal,在第一个查询中还有一件事是你在第二个项目进度计划中运行status.logid。分析,我只是写完整的查询

select status.logid, SUM(status.ti_availtime) 
from status
join schedule on status.logid = schedule.logid
where schedule.logid = 30120 and  status.starttime_utc between schedule.starttime_utc and schedule.stoptime_utc
group by status.logid

答案 1 :(得分:0)

第一个查询按status.logid分组,而第二个分组按schedule.row_date分组。如果在结束时引用schedule.stoptime_utc

status.starttime_utc between schedule.starttime_utc and schedule.starttime_utc