您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

平均每小时平均转置SQLite行和列

平均每小时平均转置SQLite行和列

sqlite没有PIVOT函数,但是您可以将聚合函数CASE表达式结合使用,以将行变成列:

select param,
  avg(case when time = '00' then param_val end) AvgHour0Val,
  avg(case when time = '00' then breach_count end) AvgHour0Count,
  avg(case when time = '01' then param_val end) AvgHour1Val,
  avg(case when time = '01' then breach_count end) AvgHour1Count,
  avg(case when time = '02' then param_val end) AvgHour2Val,
  avg(case when time = '02' then breach_count end) AvgHour2Count
from
(
  select param,
    strftime('%H', date_time) time,
    param_val,
    breach_count
  from param_vals_breaches
  where queue = 'a'
) src
group by param;

参见带有演示的SQL Fiddle

SQLServer 2022/1/1 18:50:04 有498人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶