试图将分配百分比加起来

时间:2013-03-26 19:18:40

标签: sql

我是SQL的新手,经过很少的培训就被抛入其中,因此非常简单的答案非常感谢!我试图提取参与者分配的查询,但我需要将每个有趣的所有分配汇总在一起。我目前有以下内容:

select external_plan,
   participant_tax_id,
   external_vehicle,
sum (decimal(alloc_rate,15,2)),
   money_type
from trc.vst_partalloc
where external_plan='RF00580'
and money_type='101'
group by alloc_rate,
     external_plan,
   participant_tax_id,
   external_vehicle,
   money_type

这将返回以下示例:

EXTERNAL_PLAN   PARTICIPANT_TAX_ID  EXTERNAL_VEHICLE    Alloc_Rate    MONEY_TYPE

RF00580                  #########  DFCEX              0.03 101
RF00580                  #########  ACRNX              0.06 101
RF00580                  #########  STSVX              0.06 101
RF00580                  #########  VISGX             0.06  101
RF00580                  #########  VMVIX              0.06 101
RF00580                  #########  RERGX              0.09 101
RF00580                  #########  DODGX             0.12  101
RF00580                  #########  DRLCX              0.12 101
RF00580                  #########  ABNAK             0.20  101
RF00580                  #########  PTTRX              0.20 101

因此#########表示这些行中的一个参与者,分配率加起来为100,我需要的是返回一行而不显示external_vehicle但总结{{1}到100 - 这有意义吗?感谢任何帮助,提前谢谢!

2 个答案:

答案 0 :(得分:0)

看起来您需要做的就是从select和group by子句中删除External_vehicle。

答案 1 :(得分:0)

简单的答案是从external_vehicle移除alloc_rate amd group by

select external_plan, participant_tax_id,
       sum (decimal(alloc_rate,15,2)),
       money_type
from trc.vst_partalloc
where external_plan='RF00580' and money_type='101'
group by external_plan, participant_tax_id, money_type

一些意见:

(1)您不需要在where子句和group by子句中包含external_plan和money_type。如果要将它们限制为单个值,则可以使用查询:

select participant_tax_id,
       sum (decimal(alloc_rate,15,2))
from trc.vst_partalloc
where external_plan='RF00580' and money_type='101'
group by participant_tax_id

(2)假设alloc_rate是数字类型,您正在进行转换以用于输出目的。你应该在之后进行转换

select external_plan, participant_tax_id,
       decimal(sum (alloc_rate),15,2),
       money_type
from trc.vst_partalloc
where external_plan='RF00580' and money_type='101'
group by external_plan, participant_tax_id, money_type