2008-09-17 28 views
19

我想找到一個列查詢找到列

+1

太泛型我認爲:指定至少在哪個DBMS上... – ila 2008-09-17 07:14:21

+1

什麼數據庫?我不認爲這個問題有很好的「通用」解決方案。 – 2008-09-17 07:12:15

回答

9

的第二,3rd..nth最大值你可以列進行排序爲降序格式,然後只獲得第n行的值的第n個最大值。

編輯::

更新爲每個評論的請求。 警告完全未經測試!

SELECT DOB FROM (SELECT DOB FROM USERS ORDER BY DOB DESC) WHERE ROWID = 6 

上面的東西應該適用於Oracle ...您可能必須先讓語法正確!

+0

你能提供一個代碼片段嗎?我嘗試了你的建議,但我無法從第n行獲得價值。 – 2011-09-19 10:21:31

+0

使用`ROWID`既不安全也不切實際。以[tag:sqlite]爲例。 `ROWID`可以連續生成也可以不連續生成。此外,「ROWID」的順序可能與請求列的順序不匹配。 – Quirk 2016-04-15 19:12:00

5

沒有指定哪個數據庫,MySQL的,你可以做

SELECT column FROM table ORDER BY column DESC LIMIT 7,10; 

將跳過第7,然後讓你接下來的十年最高。

+0

如果你正在使用mysql,這將不會在oracle(或者我相信的mssql)中工作 – 2008-09-17 07:16:27

+0

好點,澄清它 – Pieter 2008-09-17 07:22:43

0

在SQL Server中,只是做:

select distinct top n+1 column from table order by column desc 

然後扔掉的第一個值,如果你不需要它。

3

同樣的,你可能需要修復您的數據庫,但如果你想在有潛在價值複製的數據集上2個值,你會想要做一組,以及:

SELECT column 
FROM table 
WHERE column IS NOT NULL 
GROUP BY column 
ORDER BY column DESC 
LIMIT 5 OFFSET 2; 

會跳過前兩個,然後會讓你接下來的五個最高。

4

純SQL(注意:我建議使用特定於您的DBMS的SQL功能,因爲它可能更有效)。這會讓你獲得第n + 1的最大值(最小,翻轉<)。如果您有重複項,請將其計爲COUNT(DISTINCT VALUE)。

select id from table order by id desc limit 4 ; 
+------+ 
| id | 
+------+ 
| 2211 | 
| 2210 | 
| 2209 | 
| 2208 | 
+------+ 


SELECT yourvalue 
    FROM yourtable t1 
WHERE EXISTS(SELECT COUNT(*) 
       FROM yourtable t2 
       WHERE t1.id  <> t2.id 
        AND t1.yourvalue < t2.yourvalue 
       HAVING COUNT(*) = 3) 


+------+ 
| id | 
+------+ 
| 2208 | 
+------+ 
1

以下是Oracle的一種方法。這個例子獲得了第9個最高值。只需用包含你正在查找的位置的綁定變量替換9即可。

select created from (
    select created from (
     select created from user_objects 
     order by created desc 
     ) 
     where rownum <= 9 
     order by created asc 
    ) 
    where rownum = 1 

如果您想要第n個唯一值,您可以在最內層的查詢塊上添加DISTINCT。

0

2005 SQL:

SELECT col1 from 
    (select col1, dense_rank(col1) over (order by col1 desc) ranking 
    from t1) subq where ranking between 2 and @n 
0

另外一個用於Oracle使用分析功能:

select distinct col1 --distinct is required to remove matching value of column 
from 
(select col1, dense_rank() over (order by col1 desc) rnk 
    from tbl 
) 
where rnk = :b1 
0

尋找答案時自己,這似乎爲SQL Server的工作只是翻出了這個問題2005(來自Blorgbeard's solution派生):

SELECT MIN(q.col1) FROM (
    SELECT 
     DISTINCT TOP n col1 
     FROM myTable 
     ORDER BY col1 DESC 
) q; 

有效地,這是一個SELECT MIN(q.someCol) FROM someTable q,通過SELECT DISTINCT...查詢檢索表格的前n個。

22

考慮下面的員工表和一列工資。

 
+------+ 
| Sal | 
+------+ 
| 3500 | 
| 2500 | 
| 2500 | 
| 5500 | 
| 7500 | 
+------+ 

以下查詢將返回第N個最大元素。

select SAL from EMPLOYEE E1 where 
(N - 1) = (select count(distinct(SAL)) 
      from EMPLOYEE E2 
      where E2.SAL > E1.SAL) 

例如,需要第二最大值時,

select SAL from EMPLOYEE E1 where 
    (2 - 1) = (select count(distinct(SAL)) 
       from EMPLOYEE E2 
       where E2.SAL > E1.SAL) 
 
+------+ 
| Sal | 
+------+ 
| 5500 | 
+------+ 
+2

一個簡單的解決方案必須在所有數據庫上工作!很好的思想! :) – Sterex 2012-07-25 07:40:22

+0

很好的回答!我之前遇到過這個問題,並且對於如何解決它而不是特定於供應商而感到困惑:)一種可能的缺點是它爲每一行做了一個子查詢。 – gaboroncancio 2015-03-20 16:36:12

0
select sal,ename from emp e where 
2=(select count(distinct sal) from emp where e.sal<=emp.sal) or 
3=(select count(distinct sal) from emp where e.sal<=emp.sal) or 
4=(select count(distinct sal) from emp where e.sal<=emp.sal) order by sal desc; 
1
Select max(sal) 
from table t1 
where N (select max(sal) 
     from table t2 
     where t2.sal > t1.sal) 

要找到第N個最大SAL。

1
SELECT * FROM tablename 
WHERE columnname<(select max(columnname) from tablename) 
order by columnname desc limit 1 
0

的MySQL:

select distinct(salary) from employee order by salary desc limit (n-1), 1; 
-1

表員工

salary 
1256 
1256 
2563 
8546 
5645 

您可以通過此查詢

select salary 
from employee 
where salary=(select max(salary) 
       from employee 
       where salary <(select max(salary) from employee)); 

找到第二個最大值您發現該查詢第三最大值

select salary 
from employee 
where salary=(select max(salary) 
       from employee 
       where salary <(select max(salary) 
           from employee 
           where salary <(select max(salary)from employee))); 
0

答案: 頂部第二:

select * from (select * from deletetable where rownum <=2 order by rownum desc) where rownum <=1 
3

(表名稱=學生,列名=標記)

select * from(select row_number() over (order by mark desc) as t,mark from student group by mark) as td where t=4 
0

(表名=學生,的ColumnName =標記):

select * 
from student 
where mark=(select mark 
      from(select row_number() over (order by mark desc) as t, 
       mark 
       from student group by mark) as td 
      where t=2) 
2

您可以使用fo找到列的第n個最大值llowing查詢:

SELECT * FROM TableName a WHERE 
    n = (SELECT count(DISTINCT(b.ColumnName)) 
    FROM TableName b WHERE a.ColumnName <=b.ColumnName); 
0

我認爲下面的查詢將工作在的Oracle SQL恰到好處......我已經測試了它自己..

信息與此相關的查詢:此查詢使用兩個名爲表employeedepartment與命名員工列:dept_id(通用於員工和部門)name(員工姓名),salary

和列所屬表:dept_id(普通員工表以及),dept_name

SELECT 
    tab.dept_name,MIN(tab.salary) AS Second_Max_Sal FROM (
    SELECT e.name, e.salary, d.dept_name, dense_rank() over (partition BY d.dept_name   ORDER BY e.salary) AS rank FROM department d JOIN employee e USING (dept_id)) tab 
WHERE 
    rank BETWEEN 1 AND 2 
GROUP BY 
    tab.dept_name 

感謝

0
Select min(fee) 
from fl_FLFee 
where fee in (Select top 4 Fee from fl_FLFee order by 1 desc) 

漲跌家數四連N.

0

可以簡化這樣

SELECT MIN(Sal) FROM TableName 
WHERE Sal IN 
(SELECT TOP 4 Sal FROM TableName ORDER BY Sal DESC) 

如果薩爾包含重複的值,則使用此

SELECT MIN(Sal) FROM TableName 
WHERE Sal IN 
(SELECT distinct TOP 4 Sal FROM TableName ORDER BY Sal DESC) 

4將第n個值它可以任何最高值如5或6個等。

1

這是查詢獲得第n最高從colomn把n = 0第二高,n = 1第三等等......

SELECT * FROM TableName 
WHERE ColomnName<(select max(ColomnName) from TableName)-n order by ColomnName desc limit 1; 
0

簡單的SQL查詢獲取僱員詳細信息誰在表Employee表中有第N MAX Salary

sql> select * from Employee order by salary desc LIMIT 1 OFFSET <N - 1>; 

考慮表結構爲:

員工( ID [INT主鍵的auto_increment], 名稱[VARCHAR(30)], 薪水[INT]);

例子:

如果您需要在上表中,然後第3 MAX工資,查詢將是:

sql> select * from Employee order by salary desc LIMIT 1 OFFSET 2; 

同理:

如果你需要8 MAX薪水在上表中查詢即可:

sql> select * from Employee order by salary desc LIMIT 1 OFFSET 7; 

注: 當你有得到第NMAX價值,你應該給OFFSET(N - 1)

像這樣,你可以在薪水升序的情況下做同樣的操作。

1
select column_name from table_name 
    ordered by column_name desc limit n-1,1; 

,其中n = 1,2,3,...第n個最大值。

0

在PostgreSQL中,從Employee表中查找第N個最大工資。

SELECT * FROM Employee WHERE salary in 
(SELECT salary FROM Employee ORDER BY salary DESC LIMIT N) 
ORDER BY salary ASC LIMIT 1;