1

我传递了两个日期,需要按这两个日期的结果分组,但是因为我得到错误而不能这样做

消息 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
4

2 回答 2

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)

但我猜在这种情况下这是不必要的。

于 2013-02-26T16:48:24.093 回答
0

我不知道你为什么在这里使用group by and having子句。我认为您可以将 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
于 2013-02-26T16:51:48.747 回答