夫婦,可能會給你一些想法的Oracle查詢。我不確定這些分析函數是否可用於Sybase。基本上你需要按類別對查詢進行分區,然後得到該分類中的最後一行 - 可能是MAX(row_time)。可使用許多分析功能是:ROW_NUMBER(),RANK(),DENSE_RANK()...
-- Selects sal within dept with Rank = 2 --
SELECT ename, sal, sal_rank
FROM (SELECT ename, sal, DENSE_RANK() OVER (PARTITION BY deptno ORDER BY sal ASC) sal_rank FROM scott.emp)
WHERE sal_rank <= 2
/
-- MIN/MAX sal in each dept --
SELECT empno, deptno, sal,
MIN(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) "Lowest",
MAX(sal) KEEP (DENSE_RANK LAST ORDER BY sal) OVER (PARTITION BY deptno) "Highest"
FROM scott.emp
ORDER BY deptno, sal
/
來源
2013-01-11 21:44:29
Art
@ user1949158這裏有兩個答案..所以你可以運行兩個,看看哪一個工作,如果兩個工程,然後你檢查'解釋'計劃...... :) – bonCodigo
感謝這就是我一直在尋找。 – Zanam