Totals と呼ばれるこのようなデータベース テーブルがあり、1 人 1 人あたり 1 か月あたりの最大日付を選択しようとしています。
Date Person Balance
01-15-12 A 79
01-23-12 c 150
01-17-12 A 65
02-23-12 c 150
02-15-12 A 70
03-23-12 c 15
03-15-12 A 705
03-28-12 c 150
04-15-12 A 700
04-23-12 c 150
このテーブルを、ABC のような人だけを含む #bal という名前の一時テーブルに結合しています ...etc だから、毎月、残高を合計して平均残高を見つけることができるように、1 人あたりの月ごとの最大行が必要です。一人あたりの月。
create table #bal
(
person bigint,
avgbal decimal,
mxdate datetime
)
insert into #bal(person,avgbal,mxdate)
select
b.person,
(sum(s.BAL)/12) as avgbal,
max(date) as mxdate
from #bal b
inner join TOTALS s on (b.person=s.person)
where DATE between '01-17-2012' and getdate()
group by b.person
これまでのところ、日付ごとにグループ化されているようなものがありますが、月ごとの最大日を選択したいだけです。