How can I calculate the median of values in SQLite?

前端 未结 5 2199
旧巷少年郎
旧巷少年郎 2020-12-09 02:59

I\'d like to calculate the median value in a numeric row. How can I do that in SQLite 4?

5条回答
  •  不思量自难忘°
    2020-12-09 03:38

    There is a log table with timestamp, label, and latency. We want to see the latency median value of each label, grouped by timestamp. Format all latency value to 15 char length with leading zeroes, concatenate it, and cut half positioned value(s).. there is the median.

    select L, --V, 
           case when C % 2 = 0 then
           ( substr( V, ( C - 1 ) * 15 + 1, 15) * 1 + substr( V, C * 15 + 1, 15) * 1 ) / 2
           else
            substr( V, C * 15 + 1, 15) * 1
           end as MEDST
    from (
        select L, group_concat(ST, "") as V, count(ST) / 2 as C
        from (
            select label as L, 
                   substr( timeStamp, 1, 8) * 1 as T, 
                   printf( '%015d',latency) as ST
            from log
            where label not like '%-%' and responseMessage = 'OK'
            order by L, T, ST ) as XX
        group by L
        ) as YY
    

提交回复
热议问题