2016-04-26 93 views
1

嗨我有一個查詢,目前正在返回我4行。返回1行而不是多行

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
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; 
+0

這個工作,但我得到的總和(paidwithdelay),sum(逾期)列名稱,有沒有什麼辦法可以刪除? – FaF

+0

編輯答案 立即檢查 – Priyanshu

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; 
+0

錯誤代碼:1248年每個派生的表必須有自己的別名,這是我所得到的,當我嘗試你的代碼。 – FaF

+0

應該在最後加上臨時表,上面的查詢會給你錯誤 – Priyanshu

+0

謝謝@Priyanshu,更新了查詢 –