加入来自相同 table 的多个 Sql 语句
Join Multiple Sql statements from the same table
关于这个话题我读了很多post,但我还不满意。
我有一个名为 ticket
的 table,其中包含以下列
TicketID | AirlineID | PassengerID | TicketPrice | TicketVolume | DestinationCountry | ExitCountry | TicketDate`
我有多个查询,例如
SELECT AVG(TicketPrice)
FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2016-01-01' and '2016-12-31'
GROUP BY TicketDate
SELECT AVG(TicketPrice)
FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2017-01-01' and '2017-12-31'
GROUP BY TicketDate
请问我如何加入两个查询以并排形成另一个table
+--------------------------------++-----------
| AirlineID || Average Ticket Price 2016/2017|
+--------------------------------++-----------
他们实际上是更多的查询。
如果要将结果合并为一个 table,请使用 UNION。
SELECT {...}
UNION
SELECT {...}
SELECT 语句必须return 相同的列
只需使用CASE
即可实现:
试试这个:
SELECT
AirlineID,
AVG(CASE WHEN TicketDate BETWEEN '2016-01-01' AND '2016-12-31' THEN TicketPrice END),
AVG(CASE WHEN TicketDate BETWEEN '2017-01-01' AND '2017-12-31' THEN TicketPrice END)
FROM ticket
WHERE
TicketPrice BETWEEN 552 AND 1302 AND
AirlineID = 1
GROUP BY
AirlineID, TicketDate;
SQL
SELECT
(SELECT AVG(TicketPrice) FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2016-01-01' and '2016-12-31'
GROUP BY TicketDate) as Col1,
(SELECT AVG(TicketPrice) FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2017-01-01' and '2017-12-31'
GROUP BY TicketDate) as Col2
这是我的解决方案,可能不是 OP 想要的输出,但我认为输出更好,也不需要硬编码日期
SELECT AirlineID, extract(YEAR from TicketDate) as year, AVG(TicketPrice)
FROM ticket
WHERE TicketPrice between 552 and 1302
GROUP BY AirLineID, year
关于这个话题我读了很多post,但我还不满意。
我有一个名为 ticket
的 table,其中包含以下列
TicketID | AirlineID | PassengerID | TicketPrice | TicketVolume | DestinationCountry | ExitCountry | TicketDate`
我有多个查询,例如
SELECT AVG(TicketPrice)
FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2016-01-01' and '2016-12-31'
GROUP BY TicketDate
SELECT AVG(TicketPrice)
FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2017-01-01' and '2017-12-31'
GROUP BY TicketDate
请问我如何加入两个查询以并排形成另一个table
+--------------------------------++-----------
| AirlineID || Average Ticket Price 2016/2017|
+--------------------------------++-----------
他们实际上是更多的查询。
如果要将结果合并为一个 table,请使用 UNION。
SELECT {...}
UNION
SELECT {...}
SELECT 语句必须return 相同的列
只需使用CASE
即可实现:
试试这个:
SELECT
AirlineID,
AVG(CASE WHEN TicketDate BETWEEN '2016-01-01' AND '2016-12-31' THEN TicketPrice END),
AVG(CASE WHEN TicketDate BETWEEN '2017-01-01' AND '2017-12-31' THEN TicketPrice END)
FROM ticket
WHERE
TicketPrice BETWEEN 552 AND 1302 AND
AirlineID = 1
GROUP BY
AirlineID, TicketDate;
SQL
SELECT
(SELECT AVG(TicketPrice) FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2016-01-01' and '2016-12-31'
GROUP BY TicketDate) as Col1,
(SELECT AVG(TicketPrice) FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2017-01-01' and '2017-12-31'
GROUP BY TicketDate) as Col2
这是我的解决方案,可能不是 OP 想要的输出,但我认为输出更好,也不需要硬编码日期
SELECT AirlineID, extract(YEAR from TicketDate) as year, AVG(TicketPrice)
FROM ticket
WHERE TicketPrice between 552 and 1302
GROUP BY AirLineID, year