【问题标题】:SQL group by several fieldsSQL 按几个字段分组
【发布时间】:2020-07-27 01:13:06
【问题描述】:

为什么会出现这个错误?

"代码: 43, e.displayText() = DB::Exception: 非法类型 (Enum8('' = 0, 'Manhattan' = 1, 'Bronx' = 2, 'Brooklyn' = 3, 'Queens' = 4, 'Staten Island' = 5)) 的 2 个函数参数和 (version 20.3.4.10 (official build))"

select pickup_boroname as region, toHour(pickup_datetime) as time_, count(payment_type_) as pay_count
from datasets.trips_mergetree
where pickup_date between '2011-01-01' and '2011-12-31' and
      pickup_boroname != 0
group by time_ and region
order by pay_count desc;

【问题讨论】:

  • 表中有出租车数据:pickup_boroname - 城市区域(enum8) Pickup_date - 行程数据;pickup_datetime - 行程数据和时间;我无法按时间和地区计算付款:
  • 表格有,而不是字段。
  • 如果你在 where 子句中写:pickup_boroname != '' 而不是picking_boroname != 0 是否有效?还有这些列的列类型是什么?
  • @Sourcery,它是 enum8 : "" - 0(空字符串),'Manhattan' - 1 等等
  • 如果有人有同样的问题,错误是因为group by time_ and region,我必须写group by time_ , region

标签: mysql sql database clickhouse


【解决方案1】:

Group by 通常不会采用布尔表达式。我想你想要:

select pickup_boroname as region, toHour(pickup_datetime) as time_, 
       count(payment_type_) as pay_count
from datasets.trips_mergetree
where pickup_date between '2011-01-01' and '2011-12-31' and
      pickup_boroname <> 0
group by time_, region
order by pay_count desc;

我将!= 替换为&lt;&gt;,但也可能存在问题,具体取决于pickup_boroname 的类型。您可能打算:

pickup_boroname is not null

【讨论】:

  • pickup_boroname 的类型为 Enum8('' = 0, 'Manhattan' = 1, 'Bronx' = 2, 'Brooklyn' = 3, 'Queens' = 4, 'Staten Island' = 5) ) 的 2 个函数参数和 (version 20.3.4.10 (official build))",即picking_boroname != 0 是必要的,以忽略区域不是空字符串的行程(枚举中的空字符串对应于 0)。我试过你的决定,但它没有帮助
  • @iamhdq 。 . .你用pickup_boroname is not null了吗?
  • 是的,我认为问题不在这里,因为如果我只写 where pickup_date between '2011-01-01' and '2011-12-31' 而没有 "pickup_boroname != 0" ,错误是一样的
【解决方案2】:

这是一个错误。尝试最新的稳定版本 20.3.5.21

SELECT
    pickup_boroname AS region,
    toHour(pickup_datetime) AS time_,
    count(payment_type_) AS pay_count
FROM trips_mergetree
WHERE ((pickup_date >= '2011-01-01') AND (pickup_date <= '2011-12-31')) AND (pickup_boroname != 0)
GROUP BY
    time_,
    region
ORDER BY pay_count DESC

Ok.

0 rows in set. Elapsed: 0.002 sec.

【讨论】:

  • 非常感谢!我在 Ubuntu 中更新了 datagrip,似乎没有修复错误,但在 Windows 中它可以工作。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多