Sql 块中的WHERE语句在它们之间使用UNION ALL?按错误分组

Sql 块中的WHERE语句在它们之间使用UNION ALL?按错误分组,sql,Sql,我的代码中的这个错误有一些问题 Expected tokens [EOF]. Found token [GROUP] (Line: 61, Column: 1) 我所做的就是查询语句之间的并集。我的代码看起来像这样 Select vehicles, SUM(passengers) FROM ( SELECT "Toyota" As vehicles, count(distinct uid) As passengers FROM vehicle_tab

我的代码中的这个错误有一些问题

Expected tokens [EOF]. Found token [GROUP] (Line: 61, Column: 1)
我所做的就是查询语句之间的并集。我的代码看起来像这样

Select vehicles,
SUM(passengers) 
FROM 

(
  SELECT 
  "Toyota" As vehicles,
      count(distinct uid) As passengers
        FROM vehicle_table_1
        WHERE timestamp > '2018-12-31 23:59:59' AND model in ('Land Cruiser','C-HR')

UNION ALL

(SELECT
  "Land Rover" As vehicles,
     COUNT (DISTINCT uid) As passengers
        FROM vehicle_table_2
        WHERE timestamp > '2018-12-31 23:59:59') 

UNION ALL

(SELECT 
  "Jeep" As vehicles,
    count(distinct uid) As passengers
      FROM  vehicle_table_3
      WHERE submitted_timestamp > '2018-12-31 23:59:59')

GROUP BY 1


为什么我错误地得到了这个组?在这些块中不可能有where语句吗?谢谢你的帮助。谢谢

您应该将联合放在它自己的子查询中:

Select  vehicles,
        SUM(passengers) 
FROM    (
         SELECT "Toyota" As vehicles,
                count(distinct uid) As passengers
         FROM   vehicle_table_1
         WHERE  timestamp > '2018-12-31 23:59:59' AND model in ('Land Cruiser','C-HR')

        UNION ALL

        SELECT   "Land Rover" As vehicles,
                 COUNT (DISTINCT uid) As passengers
        FROM     vehicle_table_2
        WHERE    timestamp > '2018-12-31 23:59:59'

        UNION ALL

        SELECT "Jeep" As vehicles,
               count(distinct uid) As passengers
        FROM   vehicle_table_3
        WHERE  submitted_timestamp > '2018-12-31 23:59:59'
    ) AS sel

GROUP BY vehicles

您应该将联合放在它自己的子查询中:

Select  vehicles,
        SUM(passengers) 
FROM    (
         SELECT "Toyota" As vehicles,
                count(distinct uid) As passengers
         FROM   vehicle_table_1
         WHERE  timestamp > '2018-12-31 23:59:59' AND model in ('Land Cruiser','C-HR')

        UNION ALL

        SELECT   "Land Rover" As vehicles,
                 COUNT (DISTINCT uid) As passengers
        FROM     vehicle_table_2
        WHERE    timestamp > '2018-12-31 23:59:59'

        UNION ALL

        SELECT "Jeep" As vehicles,
               count(distinct uid) As passengers
        FROM   vehicle_table_3
        WHERE  submitted_timestamp > '2018-12-31 23:59:59'
    ) AS sel

GROUP BY vehicles

您不需要
分组依据
。子查询已经是单独的行。您还可以简化日期比较:

select 'Toyota' As vehicles,
       count(distinct uid) as passengers
from vehicle_table_1
where timestamp >= '2019-01-01' and
      model in ('Land Cruiser', 'C-HR')
union all
select 'Land Rover' As vehicles,
       count(distinct uid) As passengers
from vehicle_table_2
where timestamp >= '2019-01-01'
union all
select 'Jeep' As vehicles,
       count(distinct uid) As passengers
from vehicle_table_3
where submitted_timestamp > '2019-01-01';

如果要控制最终结果集的顺序,请使用
按车辆排序

您不需要
分组依据
。子查询已经是单独的行。您还可以简化日期比较:

select 'Toyota' As vehicles,
       count(distinct uid) as passengers
from vehicle_table_1
where timestamp >= '2019-01-01' and
      model in ('Land Cruiser', 'C-HR')
union all
select 'Land Rover' As vehicles,
       count(distinct uid) As passengers
from vehicle_table_2
where timestamp >= '2019-01-01'
union all
select 'Jeep' As vehicles,
       count(distinct uid) As passengers
from vehicle_table_3
where submitted_timestamp > '2019-01-01';

如果您想控制最终结果集的顺序,请使用
按车辆排序

您需要为每个使用计数()选择一个分组。今天的提示:为所有品牌提供一个通用车辆表。您有1个
多于
它的假表名和变量lol您需要为每个使用计数()选择一个GROUP BY。今天的提示:为所有品牌提供一个通用车辆表。您有1个
多于
它的假表名和变量lol