select content_type_code_id
, ABS(price) AS price
, SUM(case when price >= 0 THEN 1 ELSE 0 END) AS debits
, SUM(case when price < 0 THEN 1 ELSE 0 END) AS credits
from dbo.transaction_unrated
where transaction_date >= '2012/05/01'
and transaction_date < '2012/06/01'
and content_provider_code_id in (1)
group by content_type_code_id, ABS(price)
ORDER BY ABS(price) ASC
上述查询产生以下输出:
content_type_code_id price debits credits
1 0.00 317 0
1 0.99 178 1
1 1.99 786 1
但我想要这样的东西:
content_type_code_id price debits credits NetCount
1 0.00 317 0 317
1 0.99 178 1 177
1 1.99 786 1 785
其中 NetCount =(借方 - 贷方)
当我尝试为此创建另一列时,出现错误。