SQL - 使用sum但可选择使用行的默认值

时间:2013-09-18 22:09:57

标签: mysql sql

给出表格

asset
  col - id
date_sequence
  col - date
daily_history
  col - date
  col - num_error_seconds
  col - asset_id
historical_event
  col - start_date
  col - end_date
  col - asset_id

我正在尝试计算给定时间范围内所有资产的所有每日num_error_seconds,以便按天显示“错误百分比”。问题是如果有一个涉及资产的history_event,其end_date超出了sql查询范围,则应忽略daily_history,并且该资产应使用默认值86400秒(error_seconds的一天)

我没有使用historical_event的查询是:

select ds.date, 
  IF(count(dh.time) = 0, 
    100, 
    100 - (100*sum(dh.num_error_seconds) / (86400 * count(*)))
  ) percent
  from date_sequence ds 
  join asset a
  left join daily_history dh on dh.date = ds.date and dh.asset_id=a.asset_id
  where ds.date >= in_start_time and ds.date <= in_end_time
  group by ds.thedate;

在此基础上构建超出了我的SQL知识。由于聚合函数,我不能简单地为与end_date超出in_end_time的事件关联的每个资产注入86400秒。

Sample Data
Asset
1
2

Date Sequence
2013-09-01
2013-09-02
2013-09-03
2013-09-04

Daily History
2013-09-01, 1400, 1
2013-09-02, 1501, 1
2013-09-03, 1420, 1
2013-09-04, 0, 1
2013-09-01, 10000, 2
2013-09-02, 20000, 2
2013-09-03, 30000, 2
2013-09-04, 40000, 2

Historical Event
start_date, end_date, asset_id
2013-09-03 12:01:03, 2014-01-01 00:00:00, 1

我希望看到这些样本数据的时间是这些资产出错的时间的百分比

2013-09-01 => 100 - (100*(1400 + 10000))/(86400*2)
2013-09-02 => 100 - (100*(1501 + 20000))/(86400*2)
2013-09-03 => 100 - (100*(1420 + 30000))/(86400*2)
2013-09-04 => 100 - (100*(0 + 40000))/(86400*2)

除了:有一个历史事件应该优先考虑。它发生在9/3并且是开放式的(将来有一个结束日期,所以计算将变为:

2013-09-01 => 100 - (100*(1400 + 10000))/(86400*2)
2013-09-02 => 100 - (100*(1501 + 20000))/(86400*2)
2013-09-03 => 100 - (100*(86400 + 30000))/(86400*2)
2013-09-04 => 100 - (100*(86400 + 40000))/(86400*2)
如果历史事件在'in_end_time'之前有一个start_date而在in_end_time之后有一个end_time

,那么

资产1的num_error_seconds会被一整天的错误秒覆盖

这可以在一个查询中完成吗?或者我是否需要使用初始查询来暂存数据?

1 个答案:

答案 0 :(得分:1)

我认为你是这样的:

Select
    ds.date,
    100 - 100 * Sum(
        case
            when he.asset_id is not null then 86400 -- have a historical_event
            when dh.num_error_seconds is null then 0 -- no daily_history record
            else dh.num_error_seconds
        end
    ) / 86400 / count(a.id) as percent -- need to divide by number of assets
From
    date_sequence ds
        cross join
    asset a
        left outer join
    daily_history dh 
        on a.id = dh.asset_id and
           ds.date = dh.date
        left outer join (
            select distinct -- avoid counting multiple he records
                asset_id
            from
                historical_event he
            Where
                he.end_date > in_end_time
        ) he
        on a.id = he.asset_id    
Where
    ds.date >= in_start_time and
    ds.date <= in_end_time -- I'd prefer < here
Group By
    ds.date

Example Fiddle