获取每个ID的HIGHEST和SECOND HIGHEST值(SQL)

时间:2019-02-06 23:10:54

标签: sql ms-access highest

SQL的新手。在Access 2016中。在不同设备(EQUIP1,EQUIP2,EQUIP3)的表中,我希望在查询中提供最后维护日期和倒数第二个维护日期。

我搜索的许多修补程序都没有考虑按ID(在我的情况下为EQUIP#)进行分组

我所拥有的:

TABLE Maintenance
equipment      Date 
    1        1/1/2019
    1        1/2/2019
    1        1/3/2019
    2        2/1/2019
    2        2/2/2019
    2        2/3/2019

我需要什么:

      QUERY LATESTDATES
 equipment     NewDate      PreviousDate 
    1         1/3/2019       1/2/2019  
    2         2/3/2019       2/2/2019    

编辑:谢谢!有点了解语法,但这是我的最终解决方案:

SELECT [a1].equipment, NewDate, Max([b].Date) as PreviousDate

FROM
(SELECT equipment,Max(Date) as NewDate
FROM Maintenance AS [A]
GROUP BY equipment) AS [a1]

INNER JOIN Maintenance AS [b]
ON [b].equipment= [a1].equipment AND [b].Date <> [a1].NewDate
GROUP BY [a1].equipment, [a1].NewDate

Desired Result

4 个答案:

答案 0 :(得分:0)

这是我的解决方案,它可能不是最干净的,但可以在任何SQL中使用。

import React from 'react';
import ReactDOM from 'react-dom';
import Player from './Player';

class App extends React.Component {

    state = {
        players: 2,
        score: 0
    }

    resetBoard = () => {
        this.setState( { score: 0} )
    }

    render() {
        return(
            <div>
                <Player score={this.state.score} name={(this.state.players <= 2 ? "Team One" : "Player One")}/>

                <button onClick={this.resetBoard}>Reset Board</button>
            </div>
        );
    }
}
ReactDOM.render(<App />, document.getElementById('root'));

import React from 'react';


export default class Player extends React.Component {

    state = {
        score: this.props.score
    }

    updateScoreFive = () => {
        return this.setState({score: this.state.score + 5})
    }

    componentDidUpdate() {
        if (this.state.score >= 150) {
            console.log('winner')
        }
    }

    render(props) {
        return (
            <div>
            <h1>{this.props.name}</h1>
            <p>{this.state.score}</p>
            <button onClick={this.updateScoreFive}>5</button>
            </div>
        )
    }
}

答案 1 :(得分:0)

您可以尝试以下方法:

WITH T1
AS ( SELECT   EQP, MAX(Date) COL2
     FROM     dbo.T_TEST
     GROUP BY EQP )
SELECT   B.EQP, B.Date, MAX(A.Date)
FROM     dbo.T_TEST A
         JOIN T1 B ON B.EQP = A.EQP
WHERE    A.Date < B.Date
GROUP BY B.EQP, B.Date;

或者如果Access不支持CTE

SELECT   B.EQP, B.Date, MAX(A.Date)
FROM     dbo.T_TEST A
         JOIN ( SELECT   EQP, MAX(Date) COL2
                FROM     dbo.T_TEST
                GROUP BY EQP ) B ON B.EQP = A.EQP
WHERE    A.Date < B.Date
GROUP BY B.EQP, B.Date;

答案 2 :(得分:0)

要访问-尝试

Select x.EQP, Max(x.Date) as NewDate , Max(y.Date) as PreviousDate from Maintenance as x 
INNER JOIN Maintenance as y ON x.EQP = y.EQP where x.Date > y.Date
group by x.EQP

答案 3 :(得分:0)

如何用窗口功能更好地解决以上问题? 我的代码不优雅

select * from
(
select programno  , programdate, a.drk_date--, case when drk_date =1 then programDate 
from
(
select  programNo , programdate, DENSE_RANK() over (partition by programNo order by programdate desc) drk_date 
from program
)a
where a.drk_date <=2
)b
inner join
(
select programno  , programdate, drk_date--, case when drk_date =1 then programDate 
from
(
select  programNo , programdate, DENSE_RANK() over (partition by programNo order by programdate desc) drk_date 
from program
)c
where c.drk_date <=2
)d 
on b.programNo = d.programNo and b.drk_date < d.drk_date