下面我創建了一個2表transaction_type和transaction_master,然後 transaction_type。 TRANSACTION_TYPE是包含信用卡和借記卡的ID 然後TRANSACTION_TYPE是包含該交易明細pivot將null轉換爲零
transaction_type
-----------------
transaction-id|transaction-name
1 |credit
2 |debit
transaction_master
-----------
transaction-id|date_of_transaction|amount
1 |2016-01-12 |100
2 |2015-12-30 |200
1 |2016-01-05 |300
1 |2015-12-04 |500
2 |2015-12-12 |50
2 |2015-12-25 |1000
1 |2016-01-30 |100
normal PIVOT output is
-----------------------
YEAR|Jan |Feb |mar |Apr |may |Jun |Jul |Aug |Sep |Oct |Nov |Dec
2015|null|null|null|null|null|null|null|null|null|null|null|-700
2016|500 |null|null|null|null|null|null|null|null|null|null|null
But i want this
YEAR|Jan |Feb |mar |Apr |may |Jun |Jul |Aug |Sep |Oct |Nov |Dec
2015|0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0
2016|500 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0
在上面的「空」和「負」的價值觀應該是「0」
with AA as(select year(d.date_of_transaction),left(date name(month,date_of_transaction),3) as month,sum (case when c. transaction_type like '%c%' then d.Amount else d.Amount*-1)as balance
from transaction_master d join on transaction_type c c.transaction-id=d.transaction-id group by date_of_transaction) select * from AA pivot(sum(balance) for [month] in (Jan,Feb,Mar,Apr,May,jun,Jul,Aug,Sep,Oct,Nov,Dec)) as pvt
你能告訴我們你的查詢嗎? –
CASN WHEN ISNULL(** col_name **,0)<= 0 THEN 0 ELSE ** col_name ** END AS alias_name – Sankar
with aa as(select year(d.date_of_transaction),left(datename(month,date_of_transaction) ,3)作爲月份 –