查询 - 错误代码1111 - 无效使用组功能

时间:2016-03-01 19:08:28

标签: mysql sql

为什么会出现此错误?在运行查询时?为什么我不能按周分组?

HAVING子句中的问题是什么?

Select

CASE 

When 
min(date(trip.pick_up_time)) >= date(timestampadd(day,-7,now()))
and min(date(trip.pick_up_time)) <= date(timestampadd(day,0,now()))
Then 'Week 1'


When 
min(date(trip.pick_up_time)) >= date(timestampadd(day,-14,now()))   
and min(date(trip.pick_up_time)) < date(timestampadd(day,-7,now()))
Then  'Week 2'

When 
min(date(trip.pick_up_time)) >= date(timestampadd(day,-21,now()))   
and min(date(trip.pick_up_time)) < date(timestampadd(day,-14,now()))

Then  'Week 3'

When

min(date(trip.pick_up_time)) >= date(timestampadd(day,-29,now()))   
and min(date(trip.pick_up_time)) < date(timestampadd(day,-21,now()))
Then 'Week 4'

END AS WEEK

from

trip

WHERE

min(date(trip.pick_up_time)) > date(timestampadd(day,-28,now()))    
and min(date(trip.pick_up_time)) < date(timestampadd(day,0,now()))

group by Week HAVING Week is not NULL

1 个答案:

答案 0 :(得分:0)

我担心你必须在GROUP BY中使用整个CASE语句 像:

select 
  case when 1=1 then 1
       when 2=2 then 2
  end as result
from table
group by 
  case when 1=1 then 1
       when 2=2 then 2
  end