我想在SQL中获得第二个条目
Consgno Name Entrydatetime
111 A 01/03/2017 10:10:15
111 A 01/03/2017 10:20:15
111 A 01/03/2017 11:10:20
222 B 02/03/2017 10:10:25
222 B 02/03/2017 11:10:36
333 C 06/03/2017 10:10:25
333 C 07/03/2017 10:10:12
444 D 04/03/2017 10:10:41
444 D 04/03/2017 01:10:20
444 D 06/03/2017 10:10:32
One Consgno进入了不止一次。 我想这样输出:
Consgno Name Entrydatetime
111 A 01/03/2017 10:20:15
222 B 02/03/2017 11:10:36
333 C 07/03/2017 10:10:12
444 D 04/03/2017 01:10:20
答案 0 :(得分:1)
sql server / oracle / Postgres:
with CTE as
(
select MyTable.*, row_number() over(partition by consgno order by entrydatetime) as rn
from MyTable
)
select *
from CTE
where rn = 2
答案 1 :(得分:1)
(仅限工作,如果您的数据库支持窗口分析功能)
如果Entrydatetime
可能会重复同一Consgno
,并且您想要第二个date-time wise
,那么您可以使用:
select * from (
select MyTable.*,
dense_rank() over(partition by consgno order by entrydatetime) as rnk,
row_number() over(partition by consgno,entrydatetime order by entrydatetime) as rn
from MyTable
) t
where rnk = 2 and rn = 1
(请注意,如果consgno
每行都有相同的Entrydatetime
,则此查询根本不会返回consgno
。)
答案 2 :(得分:-1)
在查询中使用OFFSET命令,例如
SELECT DISTINCT Consgno,Name,Entrydatetime FROM tbl_test ORDER BY
Consgno
LIMIT 1 OFFSET 1;
此示例选择第2行。 Consgno必须是DISTINCT
答案 3 :(得分:-2)
您可以参考我的查询。希望能帮忙,我的朋友:))
<div ng-reflect-inner-h-t-m-l="<p>Open the left-side menu </p><p> Tap on <strong>My places</strong> and add yours.</p>">
<p>Open the left-side menu </p>
<p> Tap on <strong>My places</strong> and add yours.</p>
</div>