如何在SQL中扩展组

发布于 2025-02-06 17:43:45 字数 4046 浏览 1 评论 0原文

我该如何从:

organisation | individual | month      | number_consultations | mode         | professional | setting
A            | A01        | 2016-01-01 | 7                    | face-to-face | nurse        | group1
A            | A01        | 2016-01-01 | 3                    | telephone    | doctor       | group1

.....为

organisation | individual | month      | number_consultations | mode          | professional    | setting
A            | A01        | 2016-01-01 | 7                    | face-to-face  | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | telephone     | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | digital       | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | doctor          | group1
A            | A01        | 2016-01-01 | 3                    | telephone     | doctor          | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | doctor          | group1
A            | A01        | 2016-01-01 | 0                    | digital       | doctor          | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | telephone     | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | digital       | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | other           | group1
A            | A01        | 2016-01-01 | 0                    | telephone     | other           | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | other           | group1
A            | A01        | 2016-01-01 | 0                    | digital       | other           | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | digital       | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | face-to-face  | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | digital       | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | face-to-face  | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | digital       | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | face-to-face  | other           | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | other           | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | other           | group2
A            | A01        | 2016-01-01 | 0                    | digital       | other           | group2

.....

对于每个组织,个人和月份,我想扩展:

  • 模式(面对面,电话,家庭视频,数字)

  • 专业人士(护士,医生,其他直达电脑,其他)

  • 设置(group1,group2,group3,group4)

如何在SQL中执行此操作?

How can I go from:

organisation | individual | month      | number_consultations | mode         | professional | setting
A            | A01        | 2016-01-01 | 7                    | face-to-face | nurse        | group1
A            | A01        | 2016-01-01 | 3                    | telephone    | doctor       | group1

To:

organisation | individual | month      | number_consultations | mode          | professional    | setting
A            | A01        | 2016-01-01 | 7                    | face-to-face  | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | telephone     | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | digital       | nurse           | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | doctor          | group1
A            | A01        | 2016-01-01 | 3                    | telephone     | doctor          | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | doctor          | group1
A            | A01        | 2016-01-01 | 0                    | digital       | doctor          | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | telephone     | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | digital       | otherdirectcare | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | other           | group1
A            | A01        | 2016-01-01 | 0                    | telephone     | other           | group1
A            | A01        | 2016-01-01 | 0                    | homevisit     | other           | group1
A            | A01        | 2016-01-01 | 0                    | digital       | other           | group1
A            | A01        | 2016-01-01 | 0                    | face-to-face  | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | digital       | nurse           | group2
A            | A01        | 2016-01-01 | 0                    | face-to-face  | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | digital       | doctor          | group2
A            | A01        | 2016-01-01 | 0                    | face-to-face  | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | digital       | otherdirectcare | group2
A            | A01        | 2016-01-01 | 0                    | face-to-face  | other           | group2
A            | A01        | 2016-01-01 | 0                    | telephone     | other           | group2
A            | A01        | 2016-01-01 | 0                    | homevisit     | other           | group2
A            | A01        | 2016-01-01 | 0                    | digital       | other           | group2

.....

For each organisation, individual and month, I would like to expand:

  • mode (face-to-face, telephone, homevisit, digital)

  • professional (nurse, doctor, otherdirectcare, other)

  • setting (group1, group2, group3, group4)

How can I do this in SQL?

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

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

发布评论

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

评论(1

多情出卖 2025-02-13 17:43:45

您可以使用多个交叉加入 s:

with modes(mode) as (
   select 'face-to-face'
   union all
   select 'telephone'
   union all
   select 'homevisit'
   union all
   select 'digital'
),
professionals(p) as (
   select 'nurse'
   union all
   select 'doctor'
   union all
   select 'otherdirectcare'
   union all
   select 'other'
),
settings(s) as (
   select 'group1'
   union all
   select 'group2'
   union all
   select 'group3'
   union all
   select 'group4'
)
select t1.*, coalesce(t2.number_consultations, 0) 
from (select t.organization, t.individual, t.month, m.mode, p.p, s.s 
      from tbl t cross join modes m cross join professionals p cross join settings s) t1 
left join tbl t2 on t1.mode = t2.mode and t1.p = t2.professional and t1.s = t2.setting

You can use several cross joins:

with modes(mode) as (
   select 'face-to-face'
   union all
   select 'telephone'
   union all
   select 'homevisit'
   union all
   select 'digital'
),
professionals(p) as (
   select 'nurse'
   union all
   select 'doctor'
   union all
   select 'otherdirectcare'
   union all
   select 'other'
),
settings(s) as (
   select 'group1'
   union all
   select 'group2'
   union all
   select 'group3'
   union all
   select 'group4'
)
select t1.*, coalesce(t2.number_consultations, 0) 
from (select t.organization, t.individual, t.month, m.mode, p.p, s.s 
      from tbl t cross join modes m cross join professionals p cross join settings s) t1 
left join tbl t2 on t1.mode = t2.mode and t1.p = t2.professional and t1.s = t2.setting

See fiddle.

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