2

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

これまでのところ、日付ごとにグループ化されているようなものがありますが、月ごとの最大日を選択したいだけです。

4

4 に答える 4

2

上記のセットに基づいて作成したいくつかのサンプル データに基づいて、いくつかのサンプルを作成しました。これらは必ずしも同じではないため、各月の最後の値と最大値のどちらが必要かわかりません。そのため、両方の基本的なクエリを作成しました。

declare @table table
(
date date,
person varchar(10),
balance int
)

insert into @table
select '01-15-12', 'A', 79
union all
select '01-23-12', 'c', 150
union all
select '01-17-12', 'A', 65
union all
select '02-23-12', 'c', 150
union all
select '02-15-12', 'A', 70
union all
select '03-23-12', 'c', 15
union all
select '03-15-12', 'A', 705
union all
select '03-28-12', 'c', 150
union all
select '04-15-12', 'A', 700
union all
select '04-23-12', 'c', 150;

-- Average of max balance in month
with maxMonth as
(
  select year = year(date)
    , month = month(date)
    , person, monthMaxBalanace = max(balance)
  from @table
  where date between '01-17-2012' and getdate()
  group by year(date), month(date), person
)
select person, maxInMonthAverage = avg(monthMaxBalanace)
from maxMonth
group by person;

または、各月の最後の残高を使用する必要がある場合は、クエリを変更できます。

-- Average of last balance in month
with lastInMonth as
(
  select year = year(date)
    , month = month(date)
    , person, balance
    , monthRank = row_number() over (partition by year(date), month(date), person order by date desc)
  from @table
  where date between '01-17-2012' and getdate()
),
lastRows as
(
  select * from lastInMonth where monthRank = 1
)
select person, lastInMonthAverage = avg(balance)
from lastRows
group by person;

サンプル クエリ (つまり、1 月 17 日以降) に基づくと、結果は同じですが、15 日の値を含めると、2 つのクエリのロジックが異なるため、わずかに異なります。

ここに画像の説明を入力

于 2013-01-23T13:58:05.063 に答える
1

これにより、各月および各人の月の最終日の行が取得されます..

select V.[date]
  ,V.person
  ,V.balance
from (  select [person]
          ,[date]
          ,max([date]) over(partition by person,datediff(mm,0,[date])) as [max_date]
          ,balance
    from @table
)V
where V.[date]=V.max_date

これにより、期間中のすべての月の平均が取得されます

select V.person
,SUM(balance)/12 as avgbal_as_u_calc
,AVG(balance) as average_balance
from (  select [person]
          ,[date]
          ,max([date]) over(partition by person,datediff(mm,0,[date])) as [max_date]
          ,balance
    from @table
)V
where V.[date]=V.max_date
group by V.person
于 2013-01-23T15:28:16.427 に答える
0

このようなものをお探しですか?クエリはグループ化を解除して、月ごとのデータも表示できます。ここでは、ユーザーごとの月の各最大日付ごとにバランスをとり、合計して年間平均を出します。

クエリ:

select x.person, sum(p.balance) maxtotal,
sum(p.balance) /12 average
from (select max(date) mxdt,
      month(Date) as month,
      person
from person
group by month(Date), person) x
join person p
on p.person = x.person
and p.date = x.mxdt
group by x.person;

| PERSON | MAXTOTAL | AVERAGE |
-------------------------------
|      A |     1540 |     128 |
|      c |      600 |      50 |
于 2013-01-23T13:58:45.707 に答える
-2

これはOracleクエリです。それはあなたのテーブルに基づいていません。すべてのデータは、いわばオンザフライです。内側のクエリは、月番号ごとに日付を提供します(例:01、02 ...)。これが必要な場合、外側のクエリは1か月あたりのMAX値を取得し、質問を正しく理解しています。日付を月番号に変換し、それでグループ化するだけです。グループ化する人を追加できます。これがお役に立てば幸いです。

SELECT curr_mo, MAX(some_val) 
  FROM
   (-- 10 weeks starting with '01-15-2012' --
     SELECT to_char(to_date('01-15-2012', 'mm-dd-yyyy')+LEVEL*7, 'mm') curr_mo 
      , MOD(LEVEL, 2)+3 some_val
       FROM dual
     CONNECT BY LEVEL <= 10 
   )
  GROUP BY curr_mo
 /

 Inner query output:

 CURR_MO    SOME_VAL
 -------------------
  01         4
  01         3
  02         4
  02         3
  .....

  Output - MAX per each mo:

  CURR_MO   MAX(SOME_VAL)
  ------------------------
  01         4
  02         4
  03         4
  ....
于 2013-01-23T14:18:29.550 に答える