我无法从查询中获得所需的结果,您能帮我吗?
表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);
答案 0 :(得分:1)
加入表,按表id
的{{1}}分组,然后使用prices
聚合函数和group_concat()
窗口函数:
group_concat()
请参见demo。
结果:
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
答案 1 :(得分:0)
我认为您需要两个聚合级别:
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在order by
中支持group_concat()
。