返回1行而不是多行

时间:2016-04-26 08:55:05

标签: mysql

您好我有一个查询,目前正在向我返回4行。

SELECT (case when a.duedate < '2016-04-26' and a.total_paid > 0 
        then  count(a.duedate)
        else 0 end) as paidWithDelay,
        (case when a.duedate < '2016-04-26' and a.total_paid = 0 
        then  count(a.duedate)
        else 0 end) as Overdue,
        (case when a.duedate > '2016-04-26' and a.total_paid > 0  
        then count(a.duedate)
        else 0 end) as paidOnTime,
        (case when a.duedate > '2016-04-26' and a.total_paid = 0  
        then  count(a.duedate)
        else 0 end) as waitingForPayment
        FROM payment_plan a
        where a.payor_orig_id = 611 and a.UPDATE_DT is null
        group by a.duedate;

图片中就像这样。enter image description here 基本上我想要的只是返回一行 像这样 paidWithDelay 2 , overdue 1 , paidontime 0 and waitingForPayment 1 , 我尝试写前面的案例,但没有用。

2 个答案:

答案 0 :(得分:2)

    select sum(paidwithdelay)paidwithydelay,sum(overdue)overdue,sum(paidontime)paidontime,sum(waitingforpayment)waitingforpayment 
from (SELECT (case when a.duedate < '2016-04-26' and a.total_paid > 0 
            then  count(a.duedate)
            else 0 end) as paidWithDelay,
            (case when a.duedate < '2016-04-26' and a.total_paid = 0 
            then  count(a.duedate)
            else 0 end) as Overdue,
            (case when a.duedate > '2016-04-26' and a.total_paid > 0  
            then count(a.duedate)
            else 0 end) as paidOnTime,
            (case when a.duedate > '2016-04-26' and a.total_paid = 0  
            then  count(a.duedate)
            else 0 end) as waitingForPayment
            FROM payment_plan a
            where a.payor_orig_id = 611 and a.UPDATE_DT is null
            group by a.duedate)temp;

答案 1 :(得分:1)

试试这个:

select sum(paidWithDelay), 
       sum(Overdue), 
       sum(paidOnTime), 
       sum(waitingForPayment)
from 
(
    SELECT (case when a.duedate < '2016-04-26' and a.total_paid > 0 
        then  count(a.duedate)
        else 0 end) as paidWithDelay,
        (case when a.duedate < '2016-04-26' and a.total_paid = 0 
        then  count(a.duedate)
        else 0 end) as Overdue,
        (case when a.duedate > '2016-04-26' and a.total_paid > 0  
        then count(a.duedate)
        else 0 end) as paidOnTime,
        (case when a.duedate > '2016-04-26' and a.total_paid = 0  
        then  count(a.duedate)
        else 0 end) as waitingForPayment
    FROM payment_plan a
    where a.payor_orig_id = 611 and a.UPDATE_DT is null
    group by a.duedate
) t1;