MySQL Case 语句中的 AND 语句?

MySQL AND statement in Case Statement?

我是 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" table 看起来像这样:

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”table 看起来像这样:

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 table 中的薪酬列求和,前提是 它们属于 pre_date 和 post_date window 包含映射中特定行的任何 dx 代码 table

怎么样?

CREATE TABLE test.a (
enrolid INT
, msclmid INT
, svcdate_form DATE
, proc2 INT
, pre_date DATE
, post_date DATE
, dx1 VARCHAR(50)
, dx2 VARCHAR(50)
, dx3 VARCHAR(50)
, dx4 VARCHAR(50)
);

CREATE TABLE test.b (
enrolid INT
, msclmid INT
, svcdate_form DATE
, proc1 INT
, dx1 VARCHAR(50)
, dx2 VARCHAR(50)
, dx3 VARCHAR(50)
, pay INT
);

INSERT INTO test.a VALUES 
(1234,22,str_to_date('2019-03-18','%Y-%m-%d'),20600,str_to_date('2019-03-08','%Y-%m-%d'),str_to_date('2019-03-28','%Y-%m-%d'),'M2021',NULL,NULL,NULL),
(1234,23,str_to_date('2019-05-08','%Y-%m-%d'),28291,str_to_date('2019-02-07','%Y-%m-%d'),str_to_date('2019-08-06','%Y-%m-%d'),'M2021','M21611','M25571',NULL),
(1234,20610,str_to_date('2019-09-19','%Y-%m-%d'),20680,str_to_date('2019-09-09','%Y-%m-%d'),str_to_date('2019-09-29','%Y-%m-%d'),'Z4789','M21611','M65871','M1990');


INSERT INTO test.b VALUES 
(1234,45,str_to_date('2019-03-10','%Y-%m-%d'),20600,'M2021',NULL,NULL,45),
(1234,47,str_to_date('2019-03-26','%Y-%m-%d'),74987,NULL,NULL,'M2021',62),
(1234,24,str_to_date('2019-06-20','%Y-%m-%d'),34567,'M2021','M21611','M25571',123),
(1234,78,str_to_date('2019-05-08','%Y-%m-%d'),27459,NULL,NULL,'M2021',62),
(1234,60,str_to_date('2019-09-19','%Y-%m-%d'),97860,'M1990',NULL,NULL,54);

和查询:

SELECT e.enrolid 
, e.msclmid 
, e.svcdate_form
, e.proc2 
, e.pre_date 
, e.post_date 
, e.dx1 
, e.dx2
, e.dx3 
, e.dx4 
, sum(CASE 
    WHEN o.svcdate_form >= e.pre_date AND o.svcdate_form < e.svcdate_form THEN pay
    ELSE 0
  END) as pre_pay 
, sum(CASE
    WHEN o.svcdate_form > e.svcdate_form AND o.svcdate_form <= e.post_date THEN pay 
    ELSE 0
  END) as post_pay 
, sum(CASE
    WHEN o.svcdate_form >= e.pre_date AND o.svcdate_form <= e.post_date THEN pay 
    ELSE 0
  END) as total_pay
FROM test.a e
LEFT JOIN test.b o
ON e.enrolid = o.enrolid 
WHERE 1=1
AND CASE 
        WHEN e.dx1 = o.dx1 OR e.dx1 = o.dx2 OR e.dx1 = o.dx3 THEN 1
        WHEN e.dx2 = o.dx1 OR e.dx2 = o.dx2 OR e.dx2 = o.dx3 THEN 1
        WHEN e.dx3 = o.dx1 OR e.dx3 = o.dx2 OR e.dx3 = o.dx3 THEN 1
        WHEN e.dx4 = o.dx1 OR e.dx4 = o.dx2 OR e.dx4 = o.dx3 THEN 1
        ELSE 0
      END = 1
GROUP BY e.enrolid 
, e.msclmid 
, e.svcdate_form
, e.proc2 
, e.pre_date 
, e.post_date 
, e.dx1 
, e.dx2
, e.dx3 
, e.dx4
;

不过,其中一个有点不同 column/row ..我真的不明白你的具体情况..无论如何,这对你来说可能是一个很好的起点

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|      |      |     |     45|      62|      107|
   1234|     23|  2019-05-08|28291|2019-02-07|2019-08-06|M2021|M21611|M25571|     |    107|     123|      292|
   1234|  20610|  2019-09-19|20680|2019-09-09|2019-09-29|Z4789|M21611|M65871|M1990|      0|       0|       54|