我有一个包含以下列和数据的表:
activity_dt | activity_amt
2009-01-01 | -500
2009-01-01 | 750
我是否可以编写一个查看activity_amt
符号的查询,如果是正数则将其放入信用列,如果是负数,则将其存入借方列? (我正在使用Sybase)
activity_dt | debits | credits
2009-01-01 | -500 | 750
答案 0 :(得分:37)
select activity_dt,
sum(case when activity_amt < 0 then activity_amt else 0 end) as debits,
sum(case when activity_amt > 0 then activity_amt else 0 end) as credits
from the_table
group by activity_dt
order by activity_dt
答案 1 :(得分:5)
我不确定Sybase中的确切语法,但您应该能够对日期进行分组并总结正值和负值:
select
activity_dt,
sum(case when activity_amt < 0 then activity_amt else 0 end) as debits,
sum(case when activity_amt >= 0 then activity_amt else 0 end) as credits
from
theTable
group by
activity_dt
答案 2 :(得分:0)
select (select JV_GroupsHead.GroupTitle
from JV_GroupsHead
whereJV_GroupsHead.Id=jv.GroupId) as 'GroupName'
,jv.Revenue
,jv.AccountNo
,jv.AccountNoTitle
,(case when jv.Revenue < 0 then jv.Revenue else 0 end) as 'debits'
,(case when jv.Revenue> 0 then jv.Revenue else 0 end) as 'credits'
from JVFunction1('2010-07-08','2010-08-08') as jv
答案 3 :(得分:0)
我使用DECODE
函数找到了这个问题的新答案。
我希望这对每个人都有用。
select activity_dt,
sum((DECODE(activity_amt /-ABS(activity_amt), 1, activity_amt, 0))) as credits,
sum((DECODE(activity_amt /-ABS(activity_amt), -1, activity_amt, 0))) as debits
from the_table
group by activity_dt
order by activity_dt;