我有一个由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;
答案 0 :(得分:0)
您正在选择不在GROUP BY
子句中的列。您必须对它们进行一些修改才能将它们放入结果集中,通常使用MIN
或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
答案 1 :(得分:0)
尝试做一些表格gropuping逻辑,如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
答案 2 :(得分: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