Sqlite得到两个表组合

时间:2018-01-21 13:30:29

标签: android sqlite android-sqlite full-outer-join

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

enter image description here 我想得到两个表的两列的中位数,按用户ID,年和月过滤。通过两个单独的查询执行此操作,我将执行以下操作:

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

enter image description here

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

我想用单个查询完成相同的操作,但有一个问题。如果一个表没有返回任何行,我希望在这种特殊情况下返回默认值0。我不能使用ifnull子句,因为它仅在返回任何行时才有效,而不是在没有为一个表找到行的情况下。所以我的问题是: 这是我应该使用的有效方法,或者这不能完成,我应该坚持两个查询并加入代码中的值?

1 个答案:

答案 0 :(得分:2)

基于以下表格(扩展以满足无行处理): -

enter image description here

enter image description here

查询: -

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)

导致: -

enter image description here

第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)

导致: -

enter image description here

第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)

导致: -

enter image description here

如果您想要0而不是null,则可以使用以下longwinded 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): -

enter image description here

注释

  • 表格名称已更改为方便而不是 table1 tbl1 ,同样使用 table2 tbl2 被使用了。
  • 使用列名 userid 代替 user1d
  • 结果游标将根据结果具有列名(avghr,avgbw等)。
  • 屏幕截图来自SQLite Manager,数据根据列类型进行颜色编码,按照Red = NULL,Green = INTEGER,DARKER GREEN = REAL,Blue = TEXT