在SQL中对“喜欢”行进行分组并求和?

时间:2012-03-02 17:35:38

标签: sql visual-studio-2010 reporting-services

我的报告看起来像这样:

Report

注意我圈出的行(1109组中的第2行和第3行)。这些行具有相同的MemberSep,Location和Consumer文本。唯一的区别是它们每个都有不同的TODKWH001和TODKWH002字段值。

我想要做的是将这样的行组合在一起,并将TODKWH001和TODKWH002字段汇总在一起。

所以,而不是这两行:

00002574027 00000003105401 YEAGER FMS PMP 50 13 00    0      1
00002574027 00000003105401 YEAGER FMS PMP 50 13 00 4998     81

我只有一行:

00002574027 00000003105401 YEAGER FMS PMP 50 13 00 4998     82

我可以在SQL中执行此操作吗?或者我应该尝试在报告中进行分组吗?

此外,这是我用来填充报告的SQL:

SELECT CAR1.CAV_MBRHISTDETL.MBRSEP, 
       CAR1.CAV_MBRHISTDETL.LOCATION, 
       CAR1.CAV_MBRHISTDETL.BILLTYPE, 
       CAR1.CAV_MBRHISTDETL.BILLMOYR, 
       CAR1.CAV_MBRHISTDETL.RATE, 
       CAR1.CAV_LOCINFODETL.DIST, 
       CAR1.CAV_DEMANDHISTDETL.TODKWH_001, 
       CAR1.CAV_DEMANDHISTDETL.TODKWH_002, 
       CAR1.CAV_LOCINFODETL.ADDR1, CAR1.CAV_DEMANDHISTDETL.READTYPE
FROM CAR1.CAV_LOCINFODETL, { oj CAR1.CAV_MBRHISTDETL LEFT OUTER JOIN
     CAR1.CAV_DEMANDHISTDETL ON CAR1.CAV_MBRHISTDETL.MBRSEP = 
     CAR1.CAV_DEMANDHISTDETL.MBRSEP AND 
     CAR1.CAV_MBRHISTDETL.BILLMOYR = CAR1.CAV_DEMANDHISTDETL.BILLMOYR }
WHERE CAR1.CAV_MBRHISTDETL.LOCATION = CAR1.CAV_LOCINFODETL.LOCATION AND
    (CAR1.CAV_MBRHISTDETL.BILLMOYR IN ('1104', '1105', '1106', '1107', '1108','1109')) AND
    (CAR1.CAV_MBRHISTDETL.RATE = '0096') AND (CAR1.CAV_MBRHISTDETL.BILLTYPE IN ('00', '01'))
ORDER BY CAR1.CAV_LOCINFODETL.DIST, 
         CAR1.CAV_MBRHISTDETL.BILLMOYR, 
         CAR1.CAV_MBRHISTDETL.MBRSEP

1 个答案:

答案 0 :(得分:2)

将此工作添加到当前查询中:

SELECT MemberSep, Location, Consumer, SUM(TODKWH001), SUM(TODKWH002)
FROM yourtable
GROUP BY MemberSep, Location, Consumer