【问题标题】:How to execute sub queries in mysql case functions?如何在 mysql 案例函数中执行子查询?
【发布时间】:2019-10-31 18:34:40
【问题描述】:

我试图根据特定列的值(在我的情况下为jjsc_rev_cycle)以不同的方式提取字段值的计数(作为两个不同的值)。

如果jjsc_rev_cycle0 然后得到

count(distinct jjsc_job_no) as new_order_count

否则

count(distinct jjsc_job_no) as rev_order_count

我正在尝试在这样的 1 个查询中执行此操作,但显示语法错误:

SELECT jjsc_rev_cycle, 
 CASE jjsc_rev_cycle 
 WHEN jjsc_rev_cycle = 0 
 THEN  select count(distinct jjsc_job_no) as new_order_count
 ELSE  select count(distinct jjsc_job_no) as rev_order_count 
 END
 FROM jdwf_job_status_cycle 
 WHERE  jjsc_time >= '2017-06-01' and jjsc_time <= '2017-06-03' group 
 by jjsc_wf_userid

示例表数据:

jjsc_job_no jjsc_rev_cycle  jjsc_time
 7201170    0               2019-06-12 15:49:26
 7201171    0               2019-06-12 15:35:56
 7201172    0               2019-06-12 15:31:49
 7201162    0               2019-06-12 15:31:15
 7201166    1               2019-06-12 15:30:39
 7201169    0               2019-06-12 15:29:22
 7201164    0               2019-06-12 15:28:38
 7201168    0               2019-06-12 15:27:55
 7201167    0               2019-06-12 15:26:49
 7201165    0               2019-06-12 15:25:51
 7201161    0               2019-06-12 15:24:28
 7201160    0               2019-06-12 15:22:21
 7201159    0               2019-06-12 15:21:13
 7201158    0               2019-06-12 15:20:16
 7200991    0               2019-06-11 16:18:15
 7200999    0               2019-06-11 14:38:48
 7200991    1               2019-06-11 14:37:56
 7200984    0               2019-06-11 14:37:06
 7201097    0               2019-05-30 12:55:43

预期输出:

new_order_count    rev_order_count
17                 2

我做错了什么?或者有其他方法可以代替吗?

【问题讨论】:

    标签: mysql sql case


    【解决方案1】:

    您在问题中使用了count(distinct),所以我认为这是必要的。如果是,那么正确的逻辑是:

    SELECT jjsc_wf_userid,
           COUNT(DISTINCT CASE WHEN jjsc_rev_cycle = 0 THEN  jjsc_job_no END) as new_order_count,
           COUNT(DISTINCT CASE WHEN jjsc_rev_cycle <> 0 THEN  jjsc_job_no END) as rev_order_count
    FROM jdwf_job_status_cycle 
    WHERE jjsc_time >= '2017-06-01' AND
          jjsc_time < '2017-06-04' 
    GROUP BY jjsc_wf_userid
    

    请注意,我更改了“时间”比较的第二个限制。据推测,jjsc_time 有一个时间分量。无论如何,这个逻辑在有或没有时间组件的情况下都是安全的。

    如果您不需要COUNT(DISTINCT),那么您可以将其简化为:

    SELECT jjsc_wf_userid,
           SUM(jjsc_rev_cycle = 0) as new_order_count,
           SUM(jjsc_rev_cycle <> 0) as rev_order_count
    FROM jdwf_job_status_cycle 
    WHERE jjsc_time >= '2017-06-01' AND
          jjsc_time < '2017-06-04' 
    GROUP BY jjsc_wf_userid
    

    【讨论】:

    • 这个解决方案很有意义。我也一定会试试这个。
    【解决方案2】:

    您不能在机箱内执行聚合,但在我看来您需要在下面

     SELECT jjsc_rev_cycle, sum(case when jjsc_rev_cycle = 0 then 1 else 0 end) 
     as new_order_count,
    sum(case when jjsc_rev_cycle!= 0 then 1 else 0 end)  as rev_order_count
    
     FROM jdwf_job_status_cycle 
     WHERE  jjsc_time >= '2017-06-01' and jjsc_time <= '2017-06-03' group 
     by jjsc_wf_userid
    

    在获取您的样本数据后 我认为您需要在使用 case when 之前应用 distinct

     SELECT jjsc_wf_userid, sum(case when jjsc_rev_cycle = 0 then 1 else 0 end) 
         as new_order_count,
        sum(case when jjsc_rev_cycle!= 0 then 1 else 0 end)  as rev_order_count
       from
       (  
       select distinct jjsc_wf_userid,jjsc_job_no,jjsc_rev_cycle   
         FROM jdwf_job_status_cycle 
       ) a group by jjsc_wf_userid
    

    【讨论】:

    • 您的答案中有两个ass。这行得通。谢谢
    • 很抱歉打扰您,但我如何仅获得唯一 jjsc_job_no 的计数?您的答案将所有行都视为计数@Zaynul Abadin Tuhin
    • @RedBottle 您可以将其添加到 group by
    • 我实际上不能这样做,因为我必须按jjsc_wf_userid 对其进行分组,并按jjsc_job_nojjsc_wf_userid 对它进行分组@Zaynul Abadin Tuhin 也不能给我正确的结果。帮帮我
    • 你能给我你的样本表吗,里面有数据和预期的输出dbfiddle.uk
    猜你喜欢
    • 2017-11-24
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多