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

Sqlite Query, Group_Concat of multiple left joins and nested queries

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

Table 1: 住宿

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

Table 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

Table 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);

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

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

加入table,按tablepricesid分组,然后使用group_concat()聚合函数和group_concat()window函数:

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