2013-02-26 62 views
1

我傳遞的兩個日期,需要GROUP BY的結果通過這兩個日期,但不能這樣做,因爲我得到的錯誤如何通過外部引用或其他方式進行分組?

消息164,級別15,狀態1,行24每個GROUP BY表達式必須 包含至少一個不是外部引用的列。

查詢如下:

declare @sd datetime ='2012-07-01 00:00:00.000' , 
     @ed datetime ='2012-09-30 00:00:00.000' ; 

select @sd,@ed, 
     count(i.id)as count, 
     sum(case when oi.rating <50 then 1 else 0 end) as unfav, 
     sum(case when oi.Rating =50 then 1 else 0 end) as neu, 
     sum(case when oi.Rating >50 then 1 else 0 end) as fav, 
     avg(oi.Rating)as 'Av Rating' 
     from Items i (nolock) 
     inner join ItemOrganisations oi (nolock) on i.ID= oi.ItemID 
     inner join Lookup_ItemTypes it (nolock) on it.ID = i.ItemTypeID 

     inner join Batches b (nolock) on b.ID=i.BatchID 
     inner join Lookup_ItemStatus lis (nolock) on lis.ID = i.StatusID 
     inner join Lookup_BatchStatus lbs (nolock) on lbs.ID = b.StatusID 
     inner join Lookup_BatchTypes bt on bt.id = b.Typeid 

     where lbs.Name = 'Completed by Analyst' or lbs.Name='Delivered/Imported into Neptune Online' 
     and lis.Name = 'Complete' 
     and i.IsRelevant = 1 
     and bt.Name = 'Live' 
     group by @sd,@ed 
     having i.OverrideDate between @sd and @ed 

如果我這個不羣是結果我得到這是錯誤的:

2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 1 0 0 1 55 
2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 7 1 0 1 50 
2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 7 1 0 0 20 
2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 1 0 0 0 NULL 
2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 8 1 0 6 66 
2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 1 1 0 0 10 
2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 1 0 0 1 55 
2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 7 1 0 1 50 
2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 7 1 0 0 20 
2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 1 0 0 0 NULL 
2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 8 1 0 6 66 
2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 1 1 0 0 10 
2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 1 0 0 1 55 
2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 7 1 0 1 50 
2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 7 1 0 0 20 
2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 1 0 0 0 NULL 
2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 8 1 0 6 66 
2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 1 1 0 0 10 
2011-04-01 00:00:00.000 2011-04-30 00:00:00.000 1 0 0 1 55 
2011-04-01 00:00:00.000 2011-04-30 00:00:00.000 7 1 0 1 50 
2011-04-01 00:00:00.000 2011-04-30 00:00:00.000 7 1 0 0 20 
+1

沒有理由這樣做,請解釋一下你的結果期望與您進行分組的結果邏輯 – jazzytomato 2013-02-26 16:48:59

+0

請參閱edit.thanks – Xerxes 2013-02-26 16:53:46

+0

確定這些結果來自同一個查詢?你如何在前兩欄的「@ sd,@ ed」中得到不同的結果? – Kaf 2013-02-26 17:03:53

回答

2

您的查詢,如寫的,是試圖通過不斷的表達進行分組。我認爲你可以將最後兩行改爲:

and i.OverrideDate between @sd and @ed 

group by聲明是不必要的。你只返回一行,所以所有的行將被聚合。

如果您需要包括group by出於某種原因(如這被自動生成的代碼),那麼你可以使用一個技巧,如:

group by (case when OverrideDate = OverrideDate then @sd end), 
     (case when OverrideDate = OverrideDate then @ed end) 

,但我想這是在這個unncessary案件。

+0

+1的情況下,當*技巧,我不知道:)(但它在這裏看起來沒用) – jazzytomato 2013-02-26 16:52:58

+0

當使用case語句時,我仍然得到外部引用錯誤。 – Xerxes 2013-02-26 16:59:37

+0

@xerxes。 。 。其實,我認爲你需要在其中有一個變量引用,現在我看看我實際使用它的代碼。 – 2013-02-26 17:08:30

0

我不知道爲什麼你在這裏使用group byhaving條款。我認爲你可以簡化你的where子句;

where lbs.Name in ('Completed by Analyst', 
        'Delivered/Imported into Neptune Online') 
     and lis.Name = 'Complete' 
     and bt.Name = 'Live' 
     and i.IsRelevant = 1 
     and i.OverrideDate between @sd and @ed 
相關問題