0

我是 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

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

消息 8120,级别 16,状态 1,第 7 行列“OrderInfoCustom.CreateDate”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

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

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

4

1 回答 1

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
于 2012-07-02T15:51:51.230 回答