Sqlite 获取合并的两个表列的平均值

Sqlite get the average of combined two tables columns

我有以下问题,我不确定如何解决。我有以下数据库模式:

我想获得两个 table 的两列的中位数,按用户标识、年份和月份过滤。使用两个单独的查询来执行此操作,我将执行以下操作:

select avg(heartRate), avg (bodyWater) from table1 where user1d = 1234 and month = 1 and year = 2018

select avg(stepCount), avg (distance), avg (calories), avg (sleep) from table2 where user1d = 1234 and month = 1 and year = 2018

然后我会将结果合并到代码中。

我想用一个查询完成同样的事情,但有一个问题。如果一个 table return 没有行,我想 return 默认值 0,在这种特殊情况下。我不能使用 ifnull 子句,因为它只在有任何行 returned 时有效,如果没有找到一个 table 的行则无效。所以我的问题是: 这是我应该使用的有效方法,还是无法完成,我应该坚持两个查询并在代码中加入值?

基于下表(扩展以适应无行处理):-

查询:-

SELECT avghr, avgbw, avgsc, avgdist, avgc, avgslp FROM 
    (SELECT avg(heartrate) AS avghr, avg(bodywater) AS avgbw
        FROM tbl1 where userid = 1234 AND year = 2018 AND month = 1),
    (SELECT avg(stepcount) AS avgsc, avg(distance) AS avgdist, avg(calories) AS avgc, avg(sleep) AS avgslp 
        FROM tbl2 where userid = 1234 AND year = 2018 AND month = 1)

结果:-

第 2 个月:-

SELECT avghr, avgbw, avgsc, avgdist, avgc, avgslp FROM 
    (SELECT avg(heartrate) AS avghr, avg(bodywater) AS avgbw
        FROM tbl1 where userid = 1234 AND year = 2018 AND month = 2),
    (SELECT avg(stepcount) AS avgsc, avg(distance) AS avgdist, avg(calories) AS avgc, avg(sleep) AS avgslp 
        FROM tbl2 where userid = 1234 AND year = 2018 AND month = 2)

结果:-

第 3 个月:-

SELECT avghr, avgbw, avgsc, avgdist, avgc, avgslp FROM 
    (SELECT avg(heartrate) AS avghr, avg(bodywater) AS avgbw
        FROM tbl1 where userid = 1234 AND year = 2018 AND month = 3),
    (SELECT avg(stepcount) AS avgsc, avg(distance) AS avgdist, avg(calories) AS avgc, avg(sleep) AS avgslp 
        FROM tbl2 where userid = 1234 AND year = 2018 AND month = 3)

结果:-

如果你想要 0 而不是空值,那么可以使用以下长篇大论的 SQL :-

SELECT avghr, avgbw, avgsc, avgdist, avgc, avgslp, userid, year, month FROM 
    (SELECT 
        CASE 
            WHEN avg(heartrate) IS NULL THEN 0
            ELSE avg(heartrate)
        END AS avghr,
        CASE
            WHEN avg(bodywater) IS NULL THEN 0
            ELSE avg(bodywater)
        END AS avgbw
        FROM tbl1 where userid = 1234 AND year = 2018 AND month = 2),
    (SELECT
        CASE
            WHEN avg(stepcount) IS NULL THEN 0
            ELSE avg(stepcount)
        END AS avgsc,
        CASE
            WHEN avg(distance) IS NULL THEN 0
            ELSE avg(distance)
        END AS avgdist,
        CASE
            WHEN avg(calories) IS NULL THEN 0
            ELSE avg(calories)
        END AS avgc,
        CASE 
            WHEN avg(sleep) IS NULL THEN 0
            ELSE avg(sleep)
        END AS avgslp,
        userid, year, month 
            --???? not really needed 
            -- (if not used remove cols userid, year, month from primary/outer select)
            -- as well as from here.
        FROM tbl2 where userid = 1234 AND year = 2018 AND month = 2)

这将导致(对于第 2 个月,注释 SQL 被保存到一个视图因此 VIEW):-

备注

  • Table 名称已更改为方便而不是 table1tbl1 被使用,同样而不是 table2tbl2被使用。
  • 列名 userid 被用来代替 user1d
  • 生成的游标将根据结果具有列名(avghr、avgbw 等)
  • 屏幕截图来自 SQLite Manager,数据根据列类型进行颜色编码,红色 = NULL,绿色 = INTEGER,深绿色 = REAL,蓝色 = TEXT