2016-04-26 14 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 基本的には1行だけを返すことです このように 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

これはうまくいきますが、私はsum(paidwithdelay)、sum(overdue)として列名を取得しますが、これを削除する方法はありますか? – 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、クエリを更新しました –

関連する問題