Sqlite Query, Group_Concat 的多个左连接和嵌套查询

Posted

技术标签:

【中文标题】Sqlite Query, Group_Concat 的多个左连接和嵌套查询【英文标题】:Sqlite Query, Group_Concat of multiple left joins and nested queries 【发布时间】:2021-02-21 13:10:21 【问题描述】:

我无法从查询中得到想要的结果,你能帮帮我吗?

表 1:住宿

id   detail
1    single room
2    double room
3    triple room
4    family room
5    child

表 2:价格

id  amount  accomodationid
1   10      1
2   20      2
3   30      3
4   40      4
5   50      5
6   110     1
7   120     2
8   130     3
9   140     4
10  150     5

表 3:更新

id   date         priceid
1    2021-01-01   1
2    2021-01-01   2
3    2021-01-01   3
4    2021-01-01   4
5    2021-01-01   5
6    2021-02-02   6
7    2021-02-02   7
8    2021-02-02   8
9    2021-02-02   9
10   2021-02-02   10
11   2021-03-03   1
12   2021-03-03   2
13   2021-03-03   3
14   2021-03-03   4
15   2021-03-03   5

结果应该是:将所有住宿价格相同的日期分组,并在一个列中包含这些价格和住宿。

DepartureDates         | AccomodationPrices
2021-01-01, 2021-03-03 | single room 10, double room 20, triple room 30, family room 40, child 50
2021-02-02             | single room 110, double room 120, triple room 130, family room 140, child 150

这里有一些表格代码,感谢您的帮助!

CREATE TABLE accomodations (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, detail VARCHAR);
INSERT INTO accomodations (id, detail) VALUES (1, 'single room');
INSERT INTO accomodations (id, detail) VALUES (2, 'double room');
INSERT INTO accomodations (id, detail) VALUES (3, 'triple room');
INSERT INTO accomodations (id, detail) VALUES (4, 'family room');
INSERT INTO accomodations (id, detail) VALUES (5, 'child');

CREATE TABLE prices (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, amount DECIMAL (10, 2), accomodationId INTEGER);
INSERT INTO prices (id, amount, accomodationId) VALUES (1, 10, 1);
INSERT INTO prices (id, amount, accomodationId) VALUES (2, 20, 2);
INSERT INTO prices (id, amount, accomodationId) VALUES (3, 30, 3);
INSERT INTO prices (id, amount, accomodationId) VALUES (4, 40, 4);
INSERT INTO prices (id, amount, accomodationId) VALUES (5, 50, 5);
INSERT INTO prices (id, amount, accomodationId) VALUES (6, 110, 1);
INSERT INTO prices (id, amount, accomodationId) VALUES (7, 120, 2);
INSERT INTO prices (id, amount, accomodationId) VALUES (8, 130, 3);
INSERT INTO prices (id, amount, accomodationId) VALUES (9, 140, 4);
INSERT INTO prices (id, amount, accomodationId) VALUES (10, 150, 5);

CREATE TABLE depdates (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date VARCHAR, priceId INTEGER);
INSERT INTO depdates (id, date, priceId) VALUES (1, 2021-01-01, 1);
INSERT INTO depdates (id, date, priceId) VALUES (2, 2021-01-01, 2);
INSERT INTO depdates (id, date, priceId) VALUES (3, 2021-01-01, 3);
INSERT INTO depdates (id, date, priceId) VALUES (4, 2021-01-01, 4);
INSERT INTO depdates (id, date, priceId) VALUES (5, 2021-01-01, 5);
INSERT INTO depdates (id, date, priceId) VALUES (6, 2021-02-02, 6);
INSERT INTO depdates (id, date, priceId) VALUES (7, 2021-02-02, 7);
INSERT INTO depdates (id, date, priceId) VALUES (8, 2021-02-02, 8);
INSERT INTO depdates (id, date, priceId) VALUES (9, 2021-02-02, 9);
INSERT INTO depdates (id, date, priceId) VALUES (10, 2021-02-02, 10);
INSERT INTO depdates (id, date, priceId) VALUES (11, 2021-03-03, 1);
INSERT INTO depdates (id, date, priceId) VALUES (12, 2021-03-03, 2);
INSERT INTO depdates (id, date, priceId) VALUES (13, 2021-03-03, 3);
INSERT INTO depdates (id, date, priceId) VALUES (14, 2021-03-03, 4);
INSERT INTO depdates (id, date, priceId) VALUES (15, 2021-03-03, 5);

【问题讨论】:

【参考方案1】:

加入表格,按表格pricesid分组,然后使用group_concat()聚合函数和group_concat()窗口函数:

select distinct
       group_concat(distinct d.date) DepartureDates, 
       group_concat(group_concat(distinct a.detail || ' ' || p.amount)) over (partition by group_concat(distinct d.date)) AccomodationPrices
from accomodations a
inner join prices p on p.accomodationid = a.id
inner join depdates d on d.priceid = p.id
group by p.id 

请参阅demo。 结果:

> DepartureDates        | AccomodationPrices                                                       
> :-------------------- | :------------------------------------------------------------------------
> 2021-01-01,2021-03-03 | single room 10,double room 20,triple room 30,family room 40,child 50     
> 2021-02-02            | single room 110,double room 120,triple room 130,family room 140,child 150

【讨论】:

【参考方案2】:

我认为您需要两个级别的聚合:

select group_concat(date), deail_amounts
from (select date, group_concat(detail_amount) as detail_amounts
      from (select dd.date, (a.detail || ' ' || p.amount) as detail_amount
            from depdates dd join
                 price p
                 on dd.priceid = p.id join
                 accommodations a
                 on p.accommodationid = a.id
            order by dd.date, detail_amount
           ) d
      group by date
     ) d
group by detail_amounts;

请注意,子查询执行显式order by。我不认为 SQLite 在 group_concat() 中支持 order by

【讨论】:

以上是关于Sqlite Query, Group_Concat 的多个左连接和嵌套查询的主要内容,如果未能解决你的问题,请参考以下文章

警告:sqlite_query() 期望参数 1 是资源,给定字符串 [重复]

sqlite query用法

Android SQLite:哪个查询(“query”或“rawQuery”)更快?

使用哪种方法在 SQLite 中搜索大量记录 QueryAsync 或 Query

QtSql:绑定不会改变使用 SQLite 的查询

Sqlite Query, Group_Concat 的多个左连接和嵌套查询