简体   繁体   English

从子查询Oracle中选择Max

[英]Selecting Max from subquery Oracle

I'm using Oracle and trying to find the maximum transaction count (and associated date) for each station. 我正在使用Oracle,并尝试查找每个工作站的最大事务计数(及相关日期)。

This is the code I have but it returns each transaction count and date for each station rather than just the maximum. 这是我的代码,但是它返回每个站点的每个事务计数和日期,而不仅仅是返回最大值。 If I take the date part out of the outer query it returns just the maximum transaction count for each station, but I need to know the date of when it happened. 如果我将日期部分从外部查询中删除,则它仅返回每个工作站的最大事务计数,但是我需要知道它发生的日期。 Does anyone know how to get it to work? 有谁知道如何使它工作?

Thanks! 谢谢!

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

This image shows the results I currently get vs the ones I want: 此图显示了我当前获得的结果与我想要的结果:

在此处输入图片说明

What your query does is this: 您查询的内容是这样的:

  1. Subquery: Get one record per station_id, station_name and date. 子查询:每个station_id,station_name和日期获取一条记录。 Count the transactions for each such combination. 计算每个此类组合的交易次数。
  2. Main query: Get one record per station_id, station_name and date. 主要查询:每个station_id,station_name和日期获取一条记录。 (We already did that, so it doesn't change anything.) (我们已经这样做了,所以它什么都没有改变。)
  3. Order the records by transaction count. 按事务计数排序记录。

This is not what you want. 这不是您想要的。 What you want is one result row per station_id, station_name, so in your main query you should have grouped by these only, excluding the date: 您想要的是每个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;

An alternative would be not to group by in the main query again, for actually you already have the desired records already and only want to remove the others. 另一种选择是不要再次在主查询中进行分组,因为实际上您已经有了所需的记录,只想删除其他记录。 You can do this by ranking the records in the subquery, ie give them row numbers, with #1 for the best record per station (this is the one with the highest count). 您可以通过对子查询中的记录进行排名来实现此目的,即为它们提供行号,其中#1表示每个工作站的最佳记录(这是计数最高的记录)。 Then dismiss all others and you are done: 然后解雇所有其他人,您就完成了:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM