Sql 从具有重复项的日期列中提取最近的N个日期
我在SQL Server数据库中有一个数据表,其中包含日期列,如下所示。“日期”列中有许多重复项Sql 从具有重复项的日期列中提取最近的N个日期,sql,sql-server,Sql,Sql Server,我在SQL Server数据库中有一个数据表,其中包含日期列,如下所示。“日期”列中有许多重复项 date 0 2018-08 1 2018-09 2 2018-09 3 2018-09 4 2018-10 5 2018-09 6 2018-08 7 2018-07 ........... 我需要从列中提取最近的两个日期(或更多)。 以下SQL代码起作用: SELECT TOP 2 date FROM (SELECT DISTINCT date FRO
date
0 2018-08
1 2018-09
2 2018-09
3 2018-09
4 2018-10
5 2018-09
6 2018-08
7 2018-07
...........
我需要从列中提取最近的两个日期(或更多)。
以下SQL代码起作用:
SELECT TOP 2 date
FROM (SELECT DISTINCT date FROM myTbl) t
ORDER BY date DESC
然而,我觉得必须有更好的方法来做到这一点。有谁知道更好/更快的方法来实现这一点吗?您可以:
SELECT DISTINCT TOP 2 date
FROM myTbl
ORDER BY date DESC
您只需执行以下操作:
SELECT DISTINCT TOP 2 date
FROM myTbl
ORDER BY date DESC
您不需要子查询:
SELECT DISTINCT TOP (2) date
FROM myTbl
ORDER BY date DESC;
我总是把和TOP
搞混,所以你也可以写:
SELECT TOP (2) date
FROM myTbl
GROUP BY date
ORDER BY date DESC;
您不需要子查询:
SELECT DISTINCT TOP (2) date
FROM myTbl
ORDER BY date DESC;
我总是把和TOP
搞混,所以你也可以写:
SELECT TOP (2) date
FROM myTbl
GROUP BY date
ORDER BY date DESC;
作为另一个选项,您可以使用
OFFSET
和FETCH
子句是orderby
子句的选项。它们允许您限制查询返回的行数
create table MyTable (Id int, dtDate Varchar(10))
Insert into MyTable Values
(0, '2018-08'),
(1, '2018-09'),
(2, '2018-09'),
(3, '2018-09'),
(4, '2018-10'),
(5, '2018-09'),
(6, '2018-08'),
(7, '2018-07)')
SELECT
Distinct dtDate
FROM
MyTable
ORDER BY
dtDate desc
OFFSET 0 ROWS
FETCH FIRST 2 ROWS ONLY;
输出如下所示
dtDate
--------
2018-10
2018-09
作为另一个选项,您可以使用
OFFSET
和FETCH
子句是orderby
子句的选项。它们允许您限制查询返回的行数
create table MyTable (Id int, dtDate Varchar(10))
Insert into MyTable Values
(0, '2018-08'),
(1, '2018-09'),
(2, '2018-09'),
(3, '2018-09'),
(4, '2018-10'),
(5, '2018-09'),
(6, '2018-08'),
(7, '2018-07)')
SELECT
Distinct dtDate
FROM
MyTable
ORDER BY
dtDate desc
OFFSET 0 ROWS
FETCH FIRST 2 ROWS ONLY;
输出如下所示
dtDate
--------
2018-10
2018-09
样本数据
create table #MyTable (Id int, dtDate Varchar(10))
Insert into #MyTable Values
(0, '2018-08'),
(1, '2018-09'),
(2, '2018-09'),
(3, '2018-09'),
(4, '2018-10'),
(5, '2018-09'),
(6, '2018-08'),
(7, '2018-07')
您可以避免使用
DISTINCT
,尤其是在涉及大量数据且用于复杂查询的情况下
最重要的是,您以错误的方式存储数据。请使用合适的数据类型存储数据
;With CTE as
(
select *
,dateadd(second,id,(concat(dtDate,'-01') ))dtDate1
from #MyTable
)
,CTE1 as
(
select dtDate,MAX(dtDate1)as dtDate1
from cte
group by dtDate
)
select top 2 * from cte1
order by dtDate1 desc
drop table #MyTable
正如您自己所看到的,为期望的输出获取数据并不容易
无论您希望如何显示,在适当的数据类型上进行操作都是简单可靠的
;With CTE as
(
select *
,dateadd(second,id,(concat(dtDate,'-01') ))dtDate1
from #MyTable
)
,CTE1 as
(
select dtDate,MAX(dtDate1)as dtDate1
from cte
group by dtDate
)
select top 2 * from cte1
order by dtDate1 desc
drop table #MyTable
varchar
上的Order
也很危险。明天您可能需要对dtDate
样本数据进行一些计算
create table #MyTable (Id int, dtDate Varchar(10))
Insert into #MyTable Values
(0, '2018-08'),
(1, '2018-09'),
(2, '2018-09'),
(3, '2018-09'),
(4, '2018-10'),
(5, '2018-09'),
(6, '2018-08'),
(7, '2018-07')
您可以避免使用DISTINCT,尤其是在涉及大量数据且用于复杂查询的情况下
最重要的是,您以错误的方式存储数据。请使用合适的数据类型存储数据
;With CTE as
(
select *
,dateadd(second,id,(concat(dtDate,'-01') ))dtDate1
from #MyTable
)
,CTE1 as
(
select dtDate,MAX(dtDate1)as dtDate1
from cte
group by dtDate
)
select top 2 * from cte1
order by dtDate1 desc
drop table #MyTable
正如您自己所看到的,为期望的输出获取数据并不容易
无论您希望如何显示,在适当的数据类型上进行操作都是简单可靠的
;With CTE as
(
select *
,dateadd(second,id,(concat(dtDate,'-01') ))dtDate1
from #MyTable
)
,CTE1 as
(
select dtDate,MAX(dtDate1)as dtDate1
from cte
group by dtDate
)
select top 2 * from cte1
order by dtDate1 desc
drop table #MyTable
varchar
上的Order
也很危险。明天您可能需要对dtDate
进行一些计算。没有聚合的分组与DISTINCT相同。如果需要唯一的值,则使用DISTINCT可以更清楚地向任何阅读代码的人表达目标。优化器可能会对这两种方法使用相同的执行计划。不使用聚合的分组与DISTINCT相同。如果需要唯一的值,则使用DISTINCT可以更清楚地向任何阅读代码的人表达目标。优化器可能会对这两种方法使用相同的执行计划。