-1

我有一个包含 12 列的表,如下所示:

acct, addr1, addr2, addr3, addr4, addr5, 
addr6, addr7, add8, zip, domicile, shares

我需要的是将总数汇总shares到一行中以重复行。标准是addr1通过addr4是相同的,这确定该行是重复的。

我试过这个,但它没有用。

SELECT acct, addr1, addr2, addr3, addr4, addr5, 
addr6, addr7, add8, 
       zip, DOMICILE, sum(shares) as total_shares 
FROM table_name 
GROUP BY addr1, addr2, addr3, addr4 ORDER BY addr1;
4

3 回答 3

0

您正在选择不在您的GROUP BY子句中的列。您必须对它们进行一些修改才能将它们放入结果集中,通常使用MINor MAX,例如:

SELECT MIN(acct) as acct, na1, na2, na3, na4, MIN(na5) as na5, MIN(na6) as na6, MIN(na7) as na7, MIN(na8) as na8, MIN(zip) as zip, MIN(DOMICILE) as DOMICILE, sum(shares) as total_shares
FROM table_name
GROUP BY na1, na2, na3, na4
ORDER BY na1
于 2013-08-22T17:41:22.520 回答
0

尝试做一些表分组逻辑,如 INTERSECT 或 EXCEPT

您可以根据需要组合不同的列...

在此示例中,我将捕获 (addr1 = addr2 = addr3 = addr4) 的所有情况

SELECT acct, addr1 FROM table_name 
INTERSECT
SELECT acct, addr2 FROM table_name 
INTERSECT
SELECT acct, addr3 FROM table_name 
INTERSECT
SELECT acct, addr4 FROM table_name 
于 2013-08-22T17:42:15.487 回答
0
select sum(total_shares)
from 
(
  SELECT na1, na2, na3, na4, sum(shares) as total_shares 
    FROM table_name 
   GROUP BY na1, na2, na3, na4 
  having count(*) > 1
) ttl
于 2013-08-22T18:43:27.960 回答