从子查询Oracle中选择Max

时间:2016-03-03 15:32:57

标签: sql oracle

我正在使用Oracle并尝试查找每个工作站的最大事务计数(和相关日期)。

这是我的代码,但它返回每个站的每个事务计数和日期,而不仅仅是最大值。如果我从外部查询中取出日期部分,它只返回每个站的最大事务计数,但我需要知道它发生的时间。有谁知道如何让它发挥作用?

谢谢!

SELECT STATION_ID, STATION_NAME, MAX(COUNTTRAN), TESTDATE
FROM 
(
    SELECT COUNT(TRANSACTION_ID) AS COUNTTRAN, STATION_ID, 
           STATION_NAME, TO_CHAR(TRANSACTION_DATE, 'HH24') AS TESTDATE
    FROM STATION_TRANSACTIONS 
    WHERE COUNTRY = 'GB' 
    GROUP BY STATION_ID, STATION_NAME, TO_CHAR(TRANSACTION_DATE, 'HH24')
)
GROUP BY STATION_ID, STATION_NAME, TESTDATE
ORDER BY MAX(COUNTTRAN) DESC

此图显示了我目前获得的结果与我想要的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

您的查询的作用是:

  1. 子查询:每个station_id,station_name和date获取一条记录。计算每个此类组合的交易。
  2. 主查询:每个station_id,station_name和日期获取一条记录。 (我们已经这样做了,所以它不会改变任何东西。)
  3. 按交易次数订购记录。
  4. 这不是你想要的。你想要的是每个station_id,station_name的一个结果行,所以在你的主查询中你应该只按这些分组,不包括日期:

    select  
      station_id, 
      station_name, 
      max(counttran) as maxcount,
      max(testdate) keep (dense_rank last over order by counttran) as maxcountdate
    from 
    (
      select 
        count(transaction_id) as counttran, 
        station_id, 
        station_name, 
        to_char(transaction_date, 'hh24') as testdate
      from station_transactions 
      where country = 'GB' 
      group by station_id, station_name, to_char(transaction_date, 'hh24')
    )
    group by station_id, station_name;
    

    另一种方法是不再在主查询中进行分组,因为实际上您已经拥有了所需的记录,只想删除其他记录。您可以通过对子查询中的记录进行排名来实现此目的,即为它们提供行号,其中#1表示每个站的最佳记录(这是计数最高的记录)。然后解雇所有其他人,你就完成了:

    select station_id, station_name, counttran, testdate
    from 
    (
      select 
        count(transaction_id) as counttran,
        row_number() over(partition by station_id order by count(transaction_id) desc) as rn
        station_id, 
        station_name, 
        to_char(transaction_date, 'hh24') as testdate
      from station_transactions 
      where country = 'GB' 
      group by station_id, station_name, to_char(transaction_date, 'hh24')
    )
    where rn = 1;