는 매트와 답을 가지고 있지만, 경우에 당신이 순수 SQL 접근의 호기심처럼 보이는 원래 테이블, 그리고 중복을 제거하는 별개를 사용합니다. 추악한,하지만 여전히 종류의 깔끔한, 나는 생각한다 :
postgres=# create table myTable(col1 int, col2 int, val int);
CREATE TABLE
postgres=#
postgres=# insert into myTable values(1, 1, 1);
INSERT 0 1
postgres=# insert into myTable values(1, 2, 2);
INSERT 0 1
postgres=# insert into myTable values(1, 2, 3);
INSERT 0 1
postgres=# insert into myTable values(2, 1, 4);
INSERT 0 1
postgres=# insert into myTable values(2, 1, 5);
INSERT 0 1
postgres=# insert into myTable values(2, 1, 6);
INSERT 0 1
postgres=# insert into myTable values(2, 2, 7);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 8);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 9);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 10);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 11);
INSERT 0 1
postgres=#
postgres=# select col1, col2, count(*)\
Invalid command \. Try \? for help.
postgres-# from myTable
postgres-# group by col1, col2
postgres-# order by 1, 2;
col1 | col2 | count
------+------+-------
1 | 1 | 1
1 | 2 | 2
2 | 1 | 3
2 | 2 | 1
2 | 3 | 4
(5 rows)
postgres=#
postgres=#
postgres=# select col1 || ',' || col2 AS theGroup
postgres-# ,count(*) AS theCount
postgres-# from myTable
postgres-# group by col1 || ',' || col2
postgres-# order by 1;
thegroup | thecount
----------+----------
1,1 | 1
1,2 | 2
2,1 | 3
2,2 | 1
2,3 | 4
(5 rows)
postgres=#
postgres=#
postgres=# select distinct a.col1, a.col2, b.theCount
postgres-# from myTable a
postgres-# ,(select col1 || ',' || col2 AS theGroup
postgres(# ,count(*) theCount
postgres(# from myTable
postgres(# group by col1 || ',' || col2) b
postgres-# where a.col1 || ',' || a.col2 = b.theGroup
postgres-# order by 1, 2;
col1 | col2 | thecount
------+------+----------
1 | 1 | 1
1 | 2 | 2
2 | 1 | 3
2 | 2 | 1
2 | 3 | 4
(5 rows)
postgres=#
감사. 이전에이 작업을 수행 할 수 없었지만 지금은 저에게 효과적이기 때문에 분명히 실수했습니다. –
그룹이 그룹화되지 않았거나 엉뚱한 것으로 보이는 경우 SQL의 order by 절을 확인하십시오. 그룹화 된 열이 그룹 중첩과 같은 순서로 순서대로 정렬되었는지 확인하십시오.이 경우 SELECT 열 (들) FROM 테이블 ORDER BY 날짜, 카테고리, 설명. – Travis