我有一个像
这样的查询 select distinct i.charge_type, cp.name, sum(i.amount)
from charge.gp_schedule gp, charge.gsm_charge_plan i, charge cp
where i.code = gp.sales_audit_code
and cp.code = gp.code
group by i.charge_type ,cp.name
输出例如以下
GSMFixedCharge FCFBBR15 15
**GSMUsageCharge** Call Charges 2.16
**GSMUsageCharge** Service Charges 2
GSMFixedCharge Line Rental 23.98
GSMFixedCharge FCFAFPBL 1
如何仅根据' GSMUsageCharge'进一步对值进行求和。在同一个查询中,所需的输出将是
GSMFixedCharge FCFBBR15 15
GSMUsageCharge Call Charges 4.16
GSMFixedCharge Line Rental 23.98
GSMFixedCharge FCFAFPBL 1
我尝试了类似
的内容 select distinct i.charge_type, cp.name, DECODE(i.charge_type, 'GSMUsageCharge', sum (i.amount)
group by i.charge_type) result
但它不起作用......
答案 0 :(得分:1)
您可以尝试以下查询。首先,为'** GSMUsageCharge **'修改i.charge_type和cp.name值。接下来,将金额相加。请注意,使用GROUP BY时不需要使用DISTINCT。
SELECT charge_type, name, sum(amount)
FROM
( select
CASE
WHEN i.charge_type = '**GSMUsageCharge**' THEN 'GSMUsageCharge'
ELSE i.charge_type
END charge_type,
CASE
WHEN i.charge_type='**GSMUsageCharge**' AND cp.name='Service Charges' THEN 'Call Charges'
ELSE cp.name
END name,
i.amount amount
from charge.gp_schedule gp, charge.gsm_charge_plan i, charge cp
where i.code = gp.sales_audit_code
and cp.code = gp.code
) modified_names
group by charge_type, name;
答案 1 :(得分:0)
我想进一步阐述上述评论。如果我错过了一些东西,你必须原谅我,我把它快速地扔在一起只是为了让你在一个可能正确的方向。我也习惯了MYSQL,所以如果我错过了任何细微差别,请告诉我:)
SELECT * FROM (
(
select distinct i.charge_type, cp.name, sum(i.amount)
from charge.gp_schedule gp, charge.gsm_charge_plan i, charge cp
where i.code = gp.sales_audit_code
and i.charge_type <> '**GSMUsageCharge**'
and cp.code = gp.code
group by i.charge_type ,cp.name
)
UNION
(
select distinct i.charge_type, cp.name, sum(i.amount)
from charge.gp_schedule gp, charge.gsm_charge_plan i, charge cp
where i.code = gp.sales_audit_code
and i.charge_type = '**GSMUsageCharge**'
and cp.code = gp.code
group by cp.name
)
) result
ORDER BY name