SQL问题:根据datediff从记录到记录获取记录

发布于 2024-07-29 01:18:35 字数 235 浏览 6 评论 0原文

好吧,这里有一个棘手的问题...如果我的数据如下所示:

Table1

ID  Date_Created 
1   1/1/2009
2   1/3/2009
3   1/5/2009
4   1/10/2009
5   1/15/2009
6   1/16/2009

如何获取彼此相隔 2 天的记录? 我的最终结果集应该是第 1-3 行和第 5-6 行。 谢谢!

Ok, got a tricky one here... If my data looks like this:

Table1

ID  Date_Created 
1   1/1/2009
2   1/3/2009
3   1/5/2009
4   1/10/2009
5   1/15/2009
6   1/16/2009

How do I get the records that are 2 days apart from each other? My end result set should be rows 1-3, and 5-6. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉月流沐 2024-08-05 01:18:36
select distinct t1.*
from Table1 t1
inner join Table1 t2 
    on abs(cast(t1.Date_Created - t2.Date_Created as float)) between 1 and 2
select distinct t1.*
from Table1 t1
inner join Table1 t2 
    on abs(cast(t1.Date_Created - t2.Date_Created as float)) between 1 and 2
等你爱我 2024-08-05 01:18:36

——这给了你什么?

从 table1 t1、table1 t2 选择 DISTINCT t1.id、t1.date_created、t2.id、t2.date_created 其中 datediff(dd,t1.date_created,t2.date_created) = 2 AND t1.id != t2.id ORDER BY t1 。ID;

-- what does this give you?

select DISTINCT t1.id, t1.date_created, t2.id, t2.date_created from table1 t1, table1 t2 where datediff(dd,t1.date_created,t2.date_created) = 2 AND t1.id != t2.id ORDER BY t1.id;

岁吢 2024-08-05 01:18:36

这行得通吗?

select t1.id, t2.id 
  from table1 t1 
  join table1 t2 
    on t2.date_created - t1.date_created <= 2

Would this work?

select t1.id, t2.id 
  from table1 t1 
  join table1 t2 
    on t2.date_created - t1.date_created <= 2
梅窗月明清似水 2024-08-05 01:18:36

我可能建议使用编程代码来做到这一点。 您想要收集行组(单独的组)。 我认为您无法使用单个查询来解决此问题(这只会返回一组行)。

I might suggest using programming code to do it. You want to collect groups of rows (separate groups). I don't think you can solve this using a single query (which would give you just one set of rows back).

云淡月浅 2024-08-05 01:18:36

如果您想获取相隔“N”天内的行,您可以尝试以下操作:

select t1.date_created, t2.date_created 
from table1 t1, table1 t2 
where t1.id <> t2.id and 
      t2.date_created-t1.date_created between 0 and N;

例如,正如您所说,如果您想获取相隔 2 天的行,
您可以使用以下内容:

select t1.date_created,t2.date_created 
from table1 t1, table1.t2 
where t1.id <> t2.id and 
      t2.date_created-t1.date_created between 0 and 2;

我希望这有帮助......

问候,
斯里克里希纳。

If you want to get the rows which are WITHIN 'N' days apart, you can try this:

select t1.date_created, t2.date_created 
from table1 t1, table1 t2 
where t1.id <> t2.id and 
      t2.date_created-t1.date_created between 0 and N;

for exmaple, as you said, if you want to get the rows which are WITHIN 2 days a part,
you can use the below:

select t1.date_created,t2.date_created 
from table1 t1, table1.t2 
where t1.id <> t2.id and 
      t2.date_created-t1.date_created between 0 and 2;

I hope this helps....

Regards,
Srikrishna.

陌上青苔 2024-08-05 01:18:36

游标速度最快,但这里有一个 SELECT 查询可以做到这一点。 请注意,对于“最多 N”天而不是 2 天,您必须将表 Two 替换为 0 到 N-1 之间的整数表(并且效率会变差)。

我承认您并不完全清楚您想要什么,但我猜您想要的行范围总共包含至少两行,并且其中连续行最多相距 2 天。 如果日期随着 ID 一起增加,这应该可行。

with Two as (
  select 0 as offset union all select 1 
), r2(ID, Date_Created_o, dr) as (
  select
    ID, Date_Created+offset,
    Date_Created + offset - dense_rank() over (
      order by Date_Created+offset
    ) from r cross join Two
)
  select
    min(ID) as start, max(ID) as finish
  from r2
  group by dr
  having min(ID) < max(ID)
  order by dr;

A cursor will be fastest, but here is a SELECT query that will do it. Note that for "up to N" days apart instead of 2 you'll have to replace the table Two with a table of integers from 0 to N-1 (and the efficiency will get worse).

I'll admit it's not entirely clear what you want, but I'm guess you want the ranges of rows that contain at least two rows in all and within which the successive rows are at most 2 days apart. If dates increase along with IDs, this should work.

with Two as (
  select 0 as offset union all select 1 
), r2(ID, Date_Created_o, dr) as (
  select
    ID, Date_Created+offset,
    Date_Created + offset - dense_rank() over (
      order by Date_Created+offset
    ) from r cross join Two
)
  select
    min(ID) as start, max(ID) as finish
  from r2
  group by dr
  having min(ID) < max(ID)
  order by dr;
有深☉意 2024-08-05 01:18:35
SELECT l.*
FROM Table1 l
INNER JOIN Table1 r ON DATEDIFF(d, l.Date_Created, r.Date_Created) = 2
      AND r.Date_Created = (SELECT TOP 1 * FROM Table1 WHERE Date_Created > l.Date_Created ORDER BY Date_Create)
SELECT l.*
FROM Table1 l
INNER JOIN Table1 r ON DATEDIFF(d, l.Date_Created, r.Date_Created) = 2
      AND r.Date_Created = (SELECT TOP 1 * FROM Table1 WHERE Date_Created > l.Date_Created ORDER BY Date_Create)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文