选中此示例。
DECLARE @temp TABLE
(
Y SMALLINT,
M TINYINT,
D TINYINT,
PRIMARY KEY CLUSTERED (Y DESC, M DESC, D DESC) WITH(IGNORE_DUP_KEY=ON)
)
INSERT INTO @temp (Y, M, D)
VALUES
(2015, 10, 28), (2015, 10, 29),
(2015, 10, 29), (2015, 10, 30),
(2015, 10, 30), (2015, 10, 30),
(2015, 11, 4), (2015, 11, 5),
(2015, 11, 9), (2015, 11, 19),
(2016, 1 , 31), (2016, 4 , 1)
; with cte as
(
select cast (cast (y as varchar(15)) +'-' + cast (m as varchar(15)) + '-' + cast (m as varchar(15)) as date) dt , y , m, d
from @temp
)
select top 1 y,m,d from cte order by dt desc
delete @temp where y = '2016'
; with cte as
(
select cast (cast (y as varchar(15)) +'-' + cast (m as varchar(15)) + '-' + cast (m as varchar(15)) as date) dt , y , m, d
from @temp
)
select top 1 y,m,d from cte order by dt desc
你的意思是你想每年的最新日期? –
为什么不以desc命令? –