检索每月和每年薪水最高的行

时间:2016-04-05 11:12:38

标签: sql oracle oracle11g oracle-sqldeveloper

我需要这样做,但我不知道。我有这张桌子。

SSN | SALARY  |   MONTH YEAR
1234   1881,33     01     2008
8762   2578        01     2008
8726   2183,6475   01     2008
2321   1745,8525   01     2008
3123   1639,2      01     2008
1934   2572        01     2008

是否有可能从所有年份的几个月中选择薪水较高的人?在这种情况下,2008年1月是8762。

3 个答案:

答案 0 :(得分:1)

您可以使用class Sale < ActiveRecord::Base belongs_to :shop end

keep

答案 1 :(得分:1)

SELECT *
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER ( PARTITION BY year_month
                             ORDER BY money DESC ) AS rn
  FROM   (
    SELECT A.ssn,
           SUM(B.WAGE)- SUM(B.SALARY/(8*20)) AS money,
           TRUNC( rep_date, 'MM' ) AS year_month
    FROM   REP_LINES A 
           INNER JOIN COSTS B
           ON (    A.JOB=B.CAT_NUM
               and B.YEAR = EXTRACT( YEAR FROM A.REP_DATE ) )
    GROUP BY A.ssn,
           TRUNC( rep_date, 'MM' )
  ) t
)
WHERE  rn = 1;

答案 2 :(得分:0)

您可以尝试:

SELECT 
  MONTH
  , YEAR
  , FIRST_VALUE(SALARY) OVER (PARTITION BY MONTH,YEAR ORDER BY SALARY DESC)
  , FIRST_VALUE(SSN) OVER (PARTITION BY MONTH,YEAR ORDER BY SALARY DESC)
FROM your_table;