插入一列以不同的药物治疗

发布于 2025-01-11 09:51:15 字数 1490 浏览 0 评论 0原文

我有一个包含耐心治疗日期的数据库,如下所示:

pacient_id |       date |    type | finish
         1 | 2021-01-05 | routine |   null
         1 | 2021-01-10 | routine |   null
         1 | 2021-01-22 | routine |   null
         1 | 2021-01-30 | routine |    yes
         1 | 2021-02-04 | routine |   null
         1 | 2021-02-12 | routine |   null
         1 | 2021-02-19 | routine |   null
         1 | 2021-02-22 | routine |    yes

我需要在查询中添加一个新列,以显示咨询是第一次治疗还是第二次治疗,例如:

pacient_id |       date |    type | finish | treatment
         1 | 2021-01-05 | routine |   null |         1
         1 | 2021-01-10 | routine |   null |         1
         1 | 2021-01-22 | routine |   null |         1
         1 | 2021-01-30 | routine |    yes |         1
         1 | 2021-02-04 | routine |   null |         2
         1 | 2021-02-12 | routine |   null |         2
         1 | 2021-02-19 | routine |   null |         2
         1 | 2021-02-22 | routine |    yes |         2

当完成列=是时,则治疗完成。该表有很多 pacients:

pacient_id |       date |    type | finish
         1 | 2021-01-05 | routine |   null
         2 | 2021-01-10 | routine |   null
         5 | 2021-01-22 | routine |   null
         2 | 2021-01-30 | routine |    yes
         1 | 2021-02-04 | routine |   null
         3 | 2021-02-12 | routine |   null
         9 | 2021-02-19 | routine |   null
         1 | 2021-02-22 | routine |    yes

所以我订购 pacient_id 和日期。感谢您的帮助。

I have a database with the dates of pacient treatment like this:

pacient_id |       date |    type | finish
         1 | 2021-01-05 | routine |   null
         1 | 2021-01-10 | routine |   null
         1 | 2021-01-22 | routine |   null
         1 | 2021-01-30 | routine |    yes
         1 | 2021-02-04 | routine |   null
         1 | 2021-02-12 | routine |   null
         1 | 2021-02-19 | routine |   null
         1 | 2021-02-22 | routine |    yes

I need a new column on a query that show if the consults are of first treatment or of second, example:

pacient_id |       date |    type | finish | treatment
         1 | 2021-01-05 | routine |   null |         1
         1 | 2021-01-10 | routine |   null |         1
         1 | 2021-01-22 | routine |   null |         1
         1 | 2021-01-30 | routine |    yes |         1
         1 | 2021-02-04 | routine |   null |         2
         1 | 2021-02-12 | routine |   null |         2
         1 | 2021-02-19 | routine |   null |         2
         1 | 2021-02-22 | routine |    yes |         2

When the finish column = yes, then the treatment is finish. The table have many pacients:

pacient_id |       date |    type | finish
         1 | 2021-01-05 | routine |   null
         2 | 2021-01-10 | routine |   null
         5 | 2021-01-22 | routine |   null
         2 | 2021-01-30 | routine |    yes
         1 | 2021-02-04 | routine |   null
         3 | 2021-02-12 | routine |   null
         9 | 2021-02-19 | routine |   null
         1 | 2021-02-22 | routine |    yes

So I order to pacient_id and date. Thanks for help.

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

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

发布评论

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

评论(1

七色彩虹 2025-01-18 09:51:15

试试这个:

SELECT pacient_id, date, type, finish
     , 1 + count(*) FILTER (WHERE finish = 'yes') OVER (PARTITION BY pacient_id ORDER BY date ASC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS treatment
  FROM your_table

dbfiddle 中查看测试结果

Try this :

SELECT pacient_id, date, type, finish
     , 1 + count(*) FILTER (WHERE finish = 'yes') OVER (PARTITION BY pacient_id ORDER BY date ASC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS treatment
  FROM your_table

see test result in dbfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文