2012-07-02 159 views
0

我是SQL的初學者,他製作存儲過程的目標是獲取指定的時間範圍和時間間隔,並返回按時間間隔分組的時間範圍內的數據。例如,兩週時間範圍和2天時間間隔的輸入將給出7個數據點的性能。在switch case語句中使用DatePart

爲了進行分組,我使用間隔單位和日期部分的開關大小寫來提取分組。但是,我的程序不會編譯。這裏是我的代碼:

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是聚合函數的一部分,因爲它是一個開關箱內?我不確定。

編輯:我應該包括我的Microsoft SQL Server 2005年

回答

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 
+0

啊,對。對於愚蠢的問題抱歉。謝謝,您的幫助非常感謝。 – disambiguator

相關問題