如何汇总按客户分组的多个列和行

时间:2014-01-21 02:20:02

标签: sql group-by access-vba ms-access-2010

我正在尝试获得一个最终专栏,将所有兴趣列添加为最终数字,以便每个客户有一个数字。我目前有:

+--------+--------+-------+-------+-------+-------+
|Customer|Number  |IntMax |IntMin |IntMin1|IntMax1|
+--------+--------+-------+-------+-------+-------+
|Jones   |72352516|$0.00  |$381.47|$0.00  |$0.00  |
+--------+--------+-------+-------+-------+-------+
|Jones   |72352516|$455.31|$0.00  |$0.00  |$0.00  |
+--------+--------+-------+-------+-------+-------+
|Brett   |70920356|$0.00  |$0.00  |$194.56|$129.71|
+--------+--------+-------+-------+-------+-------+
|Gavin   |79023561|$0.00  |$617.29|$0.00  |$0.00  |
+--------+--------+-------+-------+-------+-------+
|Gavin   |79023561|$531.46|$0.00  |$0.00  |$0.00  |
+--------+--------+-------+-------+-------+-------+

我希望看到的结果是:

+--------+--------+---------+
|Customer|Number  |IntFinal |
+--------+--------+---------+
|Jones   |72352516|$836.78  |
+--------+--------+---------+
|Brett   |70920356|$324.27  |
+--------+--------+---------+
|Gavin   |79023561|$1,148.76|
+--------+--------+---------+

非常感谢

4 个答案:

答案 0 :(得分:0)

SELECT Customer, Number, 
       SUM(IntMax + IntMin + IntMin1 + IntMax1) AS IntFinal
  FROM Table1
 GROUP BY Customer, Number

输出:

| CUSTOMER |   NUMBER | INTFINAL |
|----------|----------|----------|
|    Brett | 70920356 |   324.27 |
|    Jones | 72352516 |   836.78 |
|    Gavin | 79023561 |  1148.75 |

这是 SQLFiddle 演示

答案 1 :(得分:0)

<!-- language: lang-sql -->
  SELECT Customer,Number,SUM((IntMax+IntMin+IntMin1+IntMax1)) as IntFinal
    FROM tablename
GROUP BY Customer,Number

答案 2 :(得分:0)

create table #temp
(
    Customer varchar(12) not null,
    Number  int not null,
    IntMax decimal(19,6),
    IntMin decimal(19,6),
    IntMin1 decimal(19,6),
    IntMax1 decimal(19,6),
)
insert into #temp values('Jones',72352516,'0.00','381.47','0.00','0.00')
insert into #temp values('Jones',72352516,'455.31','0.00','0.00','0.00')
insert into #temp values('Brett',70920356,'0.00','0.00','194.56','129.71')
insert into #temp values('Gavin',79023561,'0.00','617.29','0.00','0.00')
insert into #temp values('Gavin',79023561,'531.46','0.00','0.00','0.00')

select * from #temp

select t1.Customer, t1.Number, SUM(T1.IntMax+t1.IntMax1+t1.IntMin+t1.IntMin1) as    IntFinal from #temp t1 group by t1.Customer, t1.Number

drop table #temp

答案 3 :(得分:0)

感谢您的所有答案 - 我最终也找到了以下答案:

SELECT Interest_Min_Max1.Customer,CCur(Sum([IntMax])+ Sum([IntMin])+ Sum([IntMax1])+ Sum([IntMin1]))AS Total 从表1中可以看出 GROUP BY Interest_Min_Max1.Customer,Interest_Min_Max1.Number;