0

次のクエリを作成しましたが、日付で正しくグループ化されておらず、実際にはGROUP BY.

select i.overridedate,
       month(i.Overridedate), 
       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 i.overridedate, Month(i.OverrideDate)
    having i.OverrideDate between '2012-07-01 00:00:00.000' and '2012-09-30 00:00:00.000' 

ご覧のとおり、7 か月目のデータを表す最後の 2 行は、次のようにグループ化されていません。

NULL       NULL             1     0     1     0     1     55
2013-01-03 00:00:00.000     1     1     1     0     0     10
2012-05-28 00:00:00.000     5     7     1     0     1     50
2012-06-01 00:00:00.000     6     7     1     0     0     20
2012-07-10 00:00:00.000     7     1     0     0     0     NULL
2012-07-11 00:00:00.000     7     8     1     0     6     66
4

1 に答える 1

3

group by正しいです。なぜオーバーライド日を含めるのですか?

おそらくあなたの選択は次のようになるはずです:

select min(i.overridedate), month(i.Overridedate),
. . . 
group by month(i.Overridedate)

そして、私は年を含めるように言うコメントに強く同意します:

select min(i.overridedate), year(i.Overridedate), month(i.Overridedate),
. . . 
group by year(i.Overridedate), month(i.Overridedate)
于 2013-02-26T16:17:23.473 に答える