在switch case语句中使用DatePart

时间:2012-07-02 15:42:27

标签: sql switch-statement datepart

我是SQL的初学者,它创建一个存储过程,目标是获取指定的时间范围和间隔,并返回按时间间隔分组的时间范围内的数据。例如,两周时间范围和2天间隔的输入将提供7个数据点。

为了使分组我正在使用区间单元和日期部分的开关案例来提取它以进行分组。但是,我的程序不会编译。这是我的代码:

declare @interval int
declare @unit varchar(6)

set @interval=1
set @unit='year'

select
    case @unit
        when 'year' then datepart(year, CreateDate)/@interval*@interval
        when 'month' then datepart(month, CreateDate)/@interval*@interval
        when 'week' then datepart(week, CreateDate)/@interval*@interval
        when 'day' then datepart(day, CreateDate)/@interval*@interval
        when 'hour' then datepart(hour, CreateDate)/@interval*@interval
        when 'minute' then datepart(minute, CreateDate)/@interval*@interval
        when 'second' then datepart(second, CreateDate)/@interval*@interval
    end
from OrderInfoCustom
group by
    case @unit
        when 'year' then datepart(year, CreateDate)/@interval
        when 'month' then datepart(month, CreateDate)/@interval
        when 'week' then datepart(week, CreateDate)/@interval
        when 'day' then datepart(day, CreateDate)/@interval
        when 'hour' then datepart(hour, CreateDate)/@interval
        when 'minute' then datepart(minute, CreateDate)/@interval
        when 'second' then datepart(second, CreateDate)/@interval
    end

这拒绝编译,给出了这个错误(多次):

  

Msg 8120,Level 16,State 1,Line 7 Column   'OrderInfoCustom.CreateDate'在选择列表中无效,因为它   不包含在聚合函数或GROUP BY中   子句。

似乎编译器无法识别CreateDate是聚合函数的一部分,因为它在switch-case中?我不确定。

编辑:我应该包括我在Microsoft SQL Server 2005上。

1 个答案:

答案 0 :(得分:1)

是的,您正在按表达式进行分组,因此您无法在选择中使用该表达式的一部分。

如果使用完全相同的表达式,它应该有效:

select
  case @unit
    when 'year' then datepart(year, CreateDate)/@interval
    when 'month' then datepart(month, CreateDate)/@interval
    when 'week' then datepart(week, CreateDate)/@interval
    when 'day' then datepart(day, CreateDate)/@interval
    when 'hour' then datepart(hour, CreateDate)/@interval
    when 'minute' then datepart(minute, CreateDate)/@interval
    when 'second' then datepart(second, CreateDate)/@interval
  end * @interval
from OrderInfoCustom
group by
  case @unit
    when 'year' then datepart(year, CreateDate)/@interval
    when 'month' then datepart(month, CreateDate)/@interval
    when 'week' then datepart(week, CreateDate)/@interval
    when 'day' then datepart(day, CreateDate)/@interval
    when 'hour' then datepart(hour, CreateDate)/@interval
    when 'minute' then datepart(minute, CreateDate)/@interval
    when 'second' then datepart(second, CreateDate)/@interval
  end

您还可以使用子查询:

select y * @interval
from (
  select
    case @unit
      when 'year' then datepart(year, CreateDate)/@interval
      when 'month' then datepart(month, CreateDate)/@interval
      when 'week' then datepart(week, CreateDate)/@interval
      when 'day' then datepart(day, CreateDate)/@interval
      when 'hour' then datepart(hour, CreateDate)/@interval
      when 'minute' then datepart(minute, CreateDate)/@interval
      when 'second' then datepart(second, CreateDate)/@interval
    end as y
  from OrderInfoCustom
) x
group by y