在SQLITE3中选择一年中最常见的月份?

时间:2014-09-17 04:23:15

标签: sql sqlite

我有一张如下表 -

year        month       frequency 
----------  ----------  ----------
2501        04          33        
2501        03          911       
2503        12          377       
2503        11          3956      
2503        10          1409      
2503        07          161       
2503        06          66        
2504        03          46        

如何获得每年最频繁的月份

year        month       frequency 
----------  ----------  ----------      
2501        03          911            
2503        11          3956           
2504        03          46        

2 个答案:

答案 0 :(得分:1)

可能的解决方案是使用join

select t1.* 
from t t1 join (select year, max(freq) freq from t group by year) t2
on t1.year = t2.year and t1.freq = t2.freq

SQLFiddle

答案 1 :(得分:1)

以下查询提供了您的解决方案。 SQLFiddle here.

select year, month, max(frequency) frequency
from mytable 
group by year

更新:
您的所需输出包含year, month, frequency列,其中frequency是每年频率的最大值。因此max(frequency) frequency将别名frequency分配给查询输出,以使其符合您的要求。如果没有别名,列将为year, month, max(frequency)。这是wiki on SQL aliases

相关问题