【发布时间】:2021-04-02 08:19:39
【问题描述】:
我是 SQL 的超级新手,所以如果我做错了一切,我深表歉意,但有什么办法可以修改此代码
select e.*,
sum(case when (o.svcdate_form >= e.pre_date AND o.svcdate_form < e.svcdate_form AND (o.dx1 IN(e.dx1, e.dx2, e.dx3,e.dx4) OR o.dx2 IN(e.dx1, e.dx2, e.dx3,e.dx4)
OR o.dx3 IN(e.dx1, e.dx2, e.dx3,e.dx4))) then pay else 0 end) as pre_pay,
sum(case when (o.svcdate_form > e.svcdate_form AND o.svcdate_form <= e.post_date AND (o.dx1 IN(e.dx1, e.dx2, e.dx3,e.dx4) OR o.dx2 IN(e.dx1, e.dx2, e.dx3,e.dx4)
OR o.dx3 IN(e.dx1, e.dx2, e.dx3,e.dx4))) then pay else 0 end) as post_pay,
sum(case when (o.svcdate_form >= e.pre_date AND o.svcdate_form <= e.post_date AND (o.dx1 IN(e.dx1, e.dx2, e.dx3,e.dx4) OR o.dx2 IN(e.dx1, e.dx2, e.dx3,e.dx4)
OR o.dx3 IN(e.dx1, e.dx2, e.dx3,e.dx4))) then o.pay else 0 end) as total_pay
from eoc_dx_lookup_member_wide_new e
left join ccaeo190_ky o
on o.enrolid = e.enrolid
and (
e.dx1 in (o.dx1, o.dx2, o.dx3)
or e.dx2 in (o.dx1, o.dx2, o.dx3)
or e.dx3 in (o.dx1, o.dx2, o.dx3)
or e.dx4 in (o.dx1, o.dx2, o.dx3)
)
group by e.svcdate_form;
或者至少有一个代码可以给我一个与我想要做的类似的结果?我的案例陈述有多个条件,不知道这是否可能。 提前致谢!
“eoc_dx_lookup_member_wide_new e”表如下所示:
enrolid | msclmid | svcdate_form | proc2 | pre_date | post_date | dx1 | dx2 | dx3 | dx4
------------------------------------------------------------------------------------------------------
1234 | 22 | 2019-03-18 | 20600 | 2019-03-08 | 2019-03-28 | M2021 | NULL | NULL | NULL
1234 | 23 | 2019-05-08 | 28291 | 2019-02-07 | 2019-08-06 | M2021 | M21611 | M25571 | NULL
1234 | 20610 | 2019-09-19 | 20680 | 2019-09-09 | 2019-09-29 | Z4789 | M21611 | M65871 | M1990
我的“ccaeo190_ky o”表如下所示:
enrolid | msclmid | svcdate_form | proc1 | dx1 | dx2 | dx3 | pay
--------------------------------------------------------------------------
1234 | 45 | 2019-03-10 | 20600 | M2021 | NULL | NULL | 45
1234 | 47 | 2019-03-26 | 74987 | NULL | NULL | M2021 | 62
1234 | 24 | 2019-06-20 | 34567 | M2021 | M21611 | M25571 | 123
1234 | 78 | 2019-05-08 | 27459 | NULL | NULL | M2021 | 62
1234 | 60 | 2019-09-19 | 97860 | M1990 | NULL | NULL | 54
我希望实现的是这样的输出:
enrolid | msclmid | svcdate_form | proc2 | pre_date | post_date | dx1 | dx2 | dx3 | dx4 | pre_pay | post_pay | total_pay
--------------------------------------------------------------------------------------------------------------------------------------
1234 | 22 | 2019-03-18 | 20600 | 2019-03-08 | 2019-03-28 | M2021 | NULL | NULL | NULL | 45 | 62 | 107
1234 | 23 | 2019-05-08 | 28291 | 2019-02-07 | 2019-08-06 | M2021 | M21611 | M25571 | NULL | 0 | 123 | 185
1234 | 20610 | 2019-09-19 | 20680 | 2019-09-09 | 2019-09-29 | Z4789 | M21611 | M65871 | M1990 | 0 | 0 | 54
我想对 o 表中的 pay 列求和 仅当它们位于 pre_date 和 post_date 窗口内 并且我的映射表
【问题讨论】:
-
(1) 查询不完整 (2) 你仍然要说出你想做什么 (3) 样本数据和期望的结果也有助于澄清你的问题。
-
您的问题本身并不完整。
-
我已经修改了我的问题以包括我的整个查询以及我正在使用的示例
标签: mysql sql conditional-statements where-clause case-statement