这个SQL怎么写?

[复制链接]
查看11 | 回复6 | 2007-1-16 17:14:42 | 显示全部楼层 |阅读模式
有一个费用表a:字段fee_type_id费用类型,charge费用
纪录如下:
id
fee_type_id
charge
1
10
35.2
2
20
20.3
3
30
15.3
4
40
53.1
1
20
40.3
3
30
27.3
2
20
15.4
4
10
90.2
1
30
120.2
..........
B表对应fee_type_id,fee_type_name
fee_type_id
fee_type_name
10
租用费
20
卫星电视费
30
优惠费
40
电话费
要得到如下结果
现在求每个ID的费用,和优惠的费用;并列显示

id
charge
discount_charge
1
10
5
..............
回复

使用道具 举报

千问 | 2007-1-16 17:14:42 | 显示全部楼层
你的表有问题吧,费用和优惠费用没有关系啊
回复

使用道具 举报

千问 | 2007-1-16 17:14:42 | 显示全部楼层
select id

,sum(decode(fee_type_id,30,0,charge)) charge

,sum(decode(fee_type_id,30,charge,0)) discount_charge
from a group by id
回复

使用道具 举报

千问 | 2007-1-16 17:14:42 | 显示全部楼层
你的discount_charge 怎么算啊。
信息不全吧
回复

使用道具 举报

千问 | 2007-1-16 17:14:42 | 显示全部楼层
最初由 斯年 发布
[B]你的表有问题吧,费用和优惠费用没有关系啊 [/B]

应该是这个
回复

使用道具 举报

千问 | 2007-1-16 17:14:42 | 显示全部楼层
这个有点困难,只能写pro*c了.....
我有个费用表;其中charge里面包含优惠费用;现在想把优惠费用做为一个列;
根据acct_item_type_id帐目类型ID来分;
select a.*,b.charge
from bill_acct_item a,bill_acct_item b,bill_acct_item_type c,bill_acct_item_type d
where a.acct_item_type_id=c.acct_item_type_id and b.acct_item_type_id=d.acct_item_type_id
and substr(c.standard_code,1,1) between 0 and 9
and substr(d.standard_code,1,1) between 'A' and 'Z'
and substr(c.standard_code,2,5)=substr(d.standard_code,2,5)

A-Z代表优惠费用类型
0-9代表代表非优惠类型
回复

使用道具 举报

千问 | 2007-1-16 17:14:42 | 显示全部楼层
举例:费用帐目类型表
acct_item_type_idstandard_code name_ch
36
350010
省内长途费
3036
C50010
省内长途费优惠
还存在一种情况是
有优惠帐目类型无非优惠帐目类型
acct_item_type_idstandard_code name_ch
10419
Z43210
市话区内+区间+来电+月租优惠
这种情况就不能用上面的语句了....
怎样考虑把这种情况加进去
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行