?!oracle中分组求各列的和并条件查询,这样的sql怎么写

2024-11-26 06:34:29
推荐回答(2个)
回答1:

表名test
字段 type_id counts
1 2
1 3
2 2
2 8

select type_id,sum(counts) from test group by type_id;

结果
type_id counts
1 5
2 10
-------------补充-----------------
a表:
type_id name
1 服装
2 首饰

b表:
id type_id counts
1 1 4
2 2 5
3 1 8
4 2 2

select b.type_id,sum(counts) from a,b where a.type_id=b.type_id
group by b.type_id;
结果:
type_id counts
1 12
2 7

回答2:

select organization_id,declaration_id from YY_CUSTOM_DECLARATION_HEADER
结果:
organization_id declaration_id
88 157
88 156
88 158

select organization_id,sum(declaration_id) from YY_CUSTOM_DECLARATION_HEADER
group by organization_id
having sum(declaration_id)>0
结果:
organization_id sum(declaration_id)
88 471

select organization_id,sum(declaration_id) from YY_CUSTOM_DECLARATION_HEADER
group by organization_id
having sum(declaration_id)<0
结果:
无资料