我有一个数据库,上面有这样的治疗日期:
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当finish列= yes时,则处理完成。这张桌子上有许多垫子:
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和约会。谢谢你帮忙。
发布于 2022-03-03 10:58:55
试试这个:
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见[医]小提琴中的测试结果
https://stackoverflow.com/questions/71332154
复制相似问题