簡體   English   中英

oracle查詢以檢索按月和年分組的最大值的日期

[英]oracle query to retrieve the date of the highest value which are grouped by month and year

數據與此類似。

在此處輸入圖片說明

在這里,我需要通過按月和年對它們進行分組來檢索最高金額,但是我無法獲取最高金額的確切日期。

我嘗試的最高金額的代碼是...

SELECT ACCT_ID                     AS ACCOUNT_ID,
  COUNT(TRANS_AMOUNT)              AS NUMBER_OF_TRANSACTION,
  Sum(Trans_Amount)                As Transaction_Amount,
  To_Char( Trans_Date, 'MON-YYYY') As Month_Date,
  Max(Trans_Amount)               As Highest
From Trans_Amt
group by ACCT_ID, To_Char( Trans_Date, 'MON-YYYY');

這可行,但是我無法在此處檢索日期。 如果我嘗試輸入日期,則會收到“不是按變量分組”錯誤...

按帳戶和月份分組(我在月份級別使用了trunc)。 選擇max(amount)作為數量,並首先使用keep density_rank來獲取最大數量的日期:

SELECT 
    ACCT_ID   AS ACCOUNT_ID,
    trunc(Trans_Date, 'MM') As Month_Date,
    max(trans_date)  keep (dense_rank first order by trans_amount desc) as trans_date
    Max(Trans_Amount) As Highest
FROM Trans_Amt
GROUP BY 
   ACCT_ID, trunc(Trans_Date, 'MM');
with t as (
  select 1000 ACCT_ID, to_date('11-JAN-2000', 'dd-MM-yyyy') TRANS_DATE, 201 TRANS_AMOUNT from dual union all
  select 1000, to_date('22-JAN-2000', 'dd-MM-yyyy'), 209 from dual union all
  select 1000, to_date('31-JAN-2000', 'dd-MM-yyyy'), 4504 from dual union all
  select 1000, to_date('10-FEB-2000', 'dd-MM-yyyy'), 487 from dual union all
  select 1001, to_date('10-FEB-2000', 'dd-MM-yyyy'), 4287 from dual union all
  select 1001, to_date('17-FEB-2000', 'dd-MM-yyyy'), 4501 from dual union all
  select 1001, to_date('22-FEB-2000', 'dd-MM-yyyy'), 1209 from dual union all
  select 1000, to_date('22-FEB-2000', 'dd-MM-yyyy'), 4550 from dual union all
  select 1001, to_date('23-FEB-2000', 'dd-MM-yyyy'), 120 from dual union all
  select 1001, to_date('26-FEB-2000', 'dd-MM-yyyy'), 245 from dual union all
  select 1000, to_date('28-FEB-2000', 'dd-MM-yyyy'), 4500 from dual union all
  select 1000, to_date('08-MAR-2000', 'dd-MM-yyyy'), 256 from dual union all
  select 1001, to_date('08-MAR-2000', 'dd-MM-yyyy'), 2561 from dual union all
  select 1000, to_date('24-MAR-2000', 'dd-MM-yyyy'), 987 from dual union all
  select 1001, to_date('24-MAR-2000', 'dd-MM-yyyy'), 75000 from dual union all
  select 1000, to_date('31-MAR-2000', 'dd-MM-yyyy'), 1100 from dual union all
  select 1001, to_date('31-MAR-2000', 'dd-MM-yyyy'), 11000 from dual union all
  select 1001, to_date('04-APR-2000', 'dd-MM-yyyy'), 4287 from dual union all
  select 1000, to_date('04-APR-2000', 'dd-MM-yyyy'), 487 from dual union all
  select 1001, to_date('12-APR-2000', 'dd-MM-yyyy'), 1209 from dual union all
  select 1001, to_date('14-APR-2000', 'dd-MM-yyyy'), 1092 from dual union all
  select 1001, to_date('20-APR-2000', 'dd-MM-yyyy'), 1245 from dual union all
  select 1000, to_date('20-APR-2000', 'dd-MM-yyyy'), 7500 from dual union all
  select 1000, to_date('22-APR-2000', 'dd-MM-yyyy'), 1205 from dual union all
  select 1000, to_date('26-APR-2000', 'dd-MM-yyyy'), 245 from dual
)
select
  *
from
  t t
where
  not exists(
    select
      *
    from
      t n
    where
      trunc(t.TRANS_DATE, 'mm') = trunc(n.TRANS_DATE, 'mm')
      and n.TRANS_AMOUNT > t.TRANS_AMOUNT
      and t.ACCT_ID = n.ACCT_ID
  )
order by
  t.TRANS_DATE, t.ACCT_ID

輸出量

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM