SQL遇到问题,缺乏知识。如何将这两个查询合并在一起?就此而言,每个查询都可以正常工作,需要做些什么才能使这两个查询返回一行(一起工作)?
查询1
SELECT
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (01, 02, 03) THEN 1 ELSE 0 END) AS a_I,
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (04, 05, 06) THEN 1 ELSE 0 END) as a_II,
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (07, 08, 09) THEN 1 ELSE 0 END) as a_III,
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (10, 11, 12) THEN 1 ELSE 0 END) as a_IV,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (01, 02, 03) THEN 1 ELSE 0 END) AS b_I,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (04, 05, 06) THEN 1 ELSE 0 END) as b_II,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (07, 08, 09) THEN 1 ELSE 0 END) as b_III,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (10, 11, 12) THEN 1 ELSE 0 END) as b_IV,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (01, 02, 03) THEN 1 ELSE 0 END) as total_I,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (04, 05, 06) THEN 1 ELSE 0 END) as total_II,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (07, 08, 09) THEN 1 ELSE 0 END) as total_III,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (10, 11, 12) THEN 1 ELSE 0 END) as total_IV,
SUM(CASE WHEN YEAR(flight_date)='2019' AND flight_user=1 THEN 1 ELSE 0 END) as a_total,
SUM(CASE WHEN YEAR(flight_date)='2019' AND flight_user=2 THEN 1 ELSE 0 END) as b_total
FROM flight_data;
查询2
SELECT
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(touchdown_time, takeoff_time)))) FROM flight_data WHERE YEAR(flight_date)='2019' AND flight_user=1) AS a_total_time,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(touchdown_time, takeoff_time)))) FROM flight_data WHERE YEAR(flight_date)='2019' AND flight_user=2) AS b_total_time;
答案 0 :(得分:1)
这将起作用,但是会返回什么,我也不知道您想从中得到什么,我也不知道...因此,如果您给我们提供更多数据,那么我可以提供更多帮助...干杯!还有一件事,这将在MySQL中起作用,我们不知道您是否使用MySQL,因此也请标记数据库。谢谢!
SELECT
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (01, 02, 03) THEN 1 ELSE 0 END) AS a_I,
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (04, 05, 06) THEN 1 ELSE 0 END) as a_II,
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (07, 08, 09) THEN 1 ELSE 0 END) as a_III,
SUM(CASE WHEN flight_user=1 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (10, 11, 12) THEN 1 ELSE 0 END) as a_IV,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (01, 02, 03) THEN 1 ELSE 0 END) AS b_I,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (04, 05, 06) THEN 1 ELSE 0 END) as b_II,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (07, 08, 09) THEN 1 ELSE 0 END) as b_III,
SUM(CASE WHEN flight_user=2 AND YEAR(flight_date)='2019' AND MONTH(flight_date) in (10, 11, 12) THEN 1 ELSE 0 END) as b_IV,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (01, 02, 03) THEN 1 ELSE 0 END) as total_I,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (04, 05, 06) THEN 1 ELSE 0 END) as total_II,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (07, 08, 09) THEN 1 ELSE 0 END) as total_III,
SUM(CASE WHEN YEAR(flight_date)='2019' AND MONTH(flight_date) in (10, 11, 12) THEN 1 ELSE 0 END) as total_IV,
SUM(CASE WHEN YEAR(flight_date)='2019' AND flight_user=1 THEN 1 ELSE 0 END) as a_total,
SUM(CASE WHEN YEAR(flight_date)='2019' AND flight_user=2 THEN 1 ELSE 0 END) as b_total,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(touchdown_time, takeoff_time)))) FROM flight_data WHERE YEAR(flight_date)='2019' AND flight_user=1) AS a_total_time,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(touchdown_time, takeoff_time)))) FROM flight_data WHERE YEAR(flight_date)='2019' AND flight_user=2) AS b_total_time
FROM flight_data;