34,576
社区成员
发帖
与我相关
我的任务
分享
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([year] VARCHAR(4),[month] int)
Insert #T
select '2018',6 union all
select '2017',12 union all
select '2018',4 union all
select '2018',6 union all
select '2018',6 union all
select '2018',3 union all
select '2018',3
Go
--测试数据结束
SELECT year,
t.month
FROM
(
SELECT *,
DENSE_RANK() OVER (ORDER BY year DESC, month DESC) rn
FROM #T
) t
WHERE rn = 1;
SELECT TOP 1
year,
month,
c.name,
c.id
FROM UserInfo c
WHERE c.age IS NOT NULL
ORDER BY c.year desc,
c.month desc,
c.regtime DESC;
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(
[year] INT,
[month] INT
)
INSERT INTO t VALUES (2018,6);
INSERT INTO t VALUES (2017,12);
INSERT INTO t VALUES (2018,4);
SELECT [year],[month] FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [year] DESC,[month] desc) AS rid,* FROM t
) AS t1
WHERE t1.rid=1;
/*
year month
2018 6
*/