Sql 如何获得每个ID的正数和负数计数?
我想得到每个id的负值和正值的计数Sql 如何获得每个ID的正数和负数计数?,sql,oracle,count,window-functions,Sql,Oracle,Count,Window Functions,我想得到每个id的负值和正值的计数 ID=1有2个正事务和0个负事务。等等。 with trans_detail as ( select 1 as trans_id, 100 as trans_amount from dual union all select 1 as trans_id, 200 as trans_amount from dual union all select 2 as trans_id, -100 as trans_amount from dual u
ID=1有2个正事务和0个负事务。等等。
with trans_detail as
(
select 1 as trans_id, 100 as trans_amount from dual union all
select 1 as trans_id, 200 as trans_amount from dual union all
select 2 as trans_id, -100 as trans_amount from dual union all
select 2 as trans_id, -300 as trans_amount from dual union all
select 3 as trans_id, 400 as trans_amount from dual union all
select 3 as trans_id, -500 as trans_amount from dual
)
select trans_id,
count(*) over (partition by trans_id) as pos_count,
count(*) over (partition by trans_id) as neg_count
from trans_detail
where trans_amount > 0
UNION
select trans_id,
count(*) over (partition by trans_id) as pos_count,
count(*) over (partition by trans_id) as neg_count
from trans_detail
where trans_amount < 0;
选择trans\u id,
nvl(总和(当交易金额小于0时,则为1结束),0)为负,
nvl(金额(当交易金额大于0时,则为1结束),0)作为pos
从trans_细节
按trans_id分组
SQL Fiddle:每次看到一个正的或负的金额时,都要计算1,并将其相加
select trans_id,
sum(case when trans_amount >=0 then 1 else 0 end) as pos_amt,
sum(case when trans_amount < 0 then 1 else 0 end) as neg_amt
from trans_detail
group by trans_id
选择trans\u id,
金额(如果交易金额>=0,则为1,否则为0结束)作为pos金额,
作为负金额的总和(当交易金额小于0时,则为1,否则为0结束)
从trans_细节
按trans_id分组
您可以使用条件计数:
with trans_detail as
(
select 1 as trans_id, 100 as trans_amount from dual union all
select 1 as trans_id, 200 as trans_amount from dual union all
select 2 as trans_id, -100 as trans_amount from dual union all
select 2 as trans_id, -300 as trans_amount from dual union all
select 3 as trans_id, 400 as trans_amount from dual union all
select 3 as trans_id, -500 as trans_amount from dual
)
select trans_id,
count(case when trans_amount >= 0 then trans_id end) as pos_count,
count(case when trans_amount < 0 then trans_id end) as neg_count
from trans_detail
group by trans_id
order by trans_id;
TRANS_ID POS_COUNT NEG_COUNT
---------- ---------- ----------
1 2 0
2 0 2
3 1 1
);这会忽略零,但如果愿意,您可以将其包含在任一解码中。试试这个
select trans_id,
Sum(case when trans_amount>=0 then 1 else 0 end) as pos_count,
Sum(case when trans_amount<0 then 1 else 0 end) as neg_count,
from trans_detail
group by trans_id
选择trans\u id,
合计(当交易金额>=0时,则为1,否则为0结束)作为pos_计数,
金额(交易金额时的情况)
with trans_detail as
(
select 1 as trans_id, 100 as trans_amount from dual union all
select 1 as trans_id, 200 as trans_amount from dual union all
select 2 as trans_id, -100 as trans_amount from dual union all
select 2 as trans_id, -300 as trans_amount from dual union all
select 3 as trans_id, 400 as trans_amount from dual union all
select 3 as trans_id, -500 as trans_amount from dual
)
select trans_id,
count(case when trans_amount >= 0 then trans_id end) as pos_count,
count(case when trans_amount < 0 then trans_id end) as neg_count
from trans_detail
group by trans_id
order by trans_id;
TRANS_ID POS_COUNT NEG_COUNT
---------- ---------- ----------
1 2 0
2 0 2
3 1 1
select trans_id,
count(decode(sign(trans_amount), 1, trans_id)) as pos_count,
count(decode(sign(trans_amount), -1, trans_id)) as neg_count
from trans_detail
group by trans_id
order by trans_id;
select trans_id,
Sum(case when trans_amount>=0 then 1 else 0 end) as pos_count,
Sum(case when trans_amount<0 then 1 else 0 end) as neg_count,
from trans_detail
group by trans_id