查找 2 个日期操作之间的匹配项

发布于 2024-11-07 03:00:02 字数 250 浏览 1 评论 0原文

我想找到 Patient.dob - 20patient.dob -30(以年为单位)之间的所有 Patients.dob

我正在使用 Oracle 10g,并尝试过 dateAdd 并尝试替换 SYSTIME - 12*30 但都不起作用。

数据加载良好,并且采用应有的日期格式。

这怎么能做到呢?

I want to find all Patients.dob that are between Patient.dob - 20, and patient.dob -30 (in years).

I am using Oracle 10g, and have tried dateAdd and tried substituting SYSTIME - 12*30 but neither works.

The data is loaded fine and is in the date format as it should be.

How can this be done?

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

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

发布评论

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

评论(2

月亮坠入山谷 2024-11-14 03:00:02

您可以提取年份并在两者之间进行计算(但这种方式只能获得年份差异,不需要精确的 20-30 年差异),

with datam as
(
 select level id, sysdate - level dob
   from dual 
 connect by level < 15000)

 select count(id) count_id , extract(year from dob)
   from datam
  where extract(year from dob) between 2011-30 and 2011-20
  group by extract(year from dob)
  order by 2 desc 

但是,忽略闰年(这就是您尝试对 12*30 执行的操作,它应该是 365*12)

with datam as
(
 select level id, sysdate - level dob
   from dual 
 connect by level < 15000)

 select count(id) count_id , extract(year from dob)
   from datam
  where dob between (sysdate - 365*30) and (sysdate - 365*20)
  group by extract(year from dob)
  order by 2 desc ;

但是@Briguy37 也可以:

with datam as
(
 select level id, sysdate - level dob
   from dual 
 connect by level < 15000)

 select count(id) count_id , extract(year from dob)
   from datam
  where dob between add_months(dob,- 12*30) and add_months(sysdate,- 12*20)
  group by extract(year from dob)
  order by 2 desc ;

You can extract the year and do a between (but this way only gets the year difference, no necessary the exact 20-30 year difference)

with datam as
(
 select level id, sysdate - level dob
   from dual 
 connect by level < 15000)

 select count(id) count_id , extract(year from dob)
   from datam
  where extract(year from dob) between 2011-30 and 2011-20
  group by extract(year from dob)
  order by 2 desc 

but, ignoring leap years (this is what you were trying to do with the 12*30, it should have been 365*12)

with datam as
(
 select level id, sysdate - level dob
   from dual 
 connect by level < 15000)

 select count(id) count_id , extract(year from dob)
   from datam
  where dob between (sysdate - 365*30) and (sysdate - 365*20)
  group by extract(year from dob)
  order by 2 desc ;

But @Briguy37 works as well:

with datam as
(
 select level id, sysdate - level dob
   from dual 
 connect by level < 15000)

 select count(id) count_id , extract(year from dob)
   from datam
  where dob between add_months(dob,- 12*30) and add_months(sysdate,- 12*20)
  group by extract(year from dob)
  order by 2 desc ;
仙女 2024-11-14 03:00:02

您可以使用 add_months 来执行此操作:

select * from patient p 
  where 
    add_months(p.dob,30 * 12) > current_date and 
    add_months(p.dob,20 * 12) < current_date;

You can use add_months to do it:

select * from patient p 
  where 
    add_months(p.dob,30 * 12) > current_date and 
    add_months(p.dob,20 * 12) < current_date;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文