2012-10-18 2 views
3

여러 개의 열이 일치하는 곳에서 내 쿼리를 그룹화해야합니다. 예를 들어, 날짜, 카테고리 및 설명이 일치하는 모든 행을 그룹화합니다.cfquery를 여러 열로 그룹화하려면 어떻게합니까?

<cfoutput query="myQry" group="date,category,description"> 
    #date# #category# #description# 
    <cfoutput> 
    #detail# 
    </cfoutput> 
</cfoutput> 

내가 '아무튼 그룹화 cfoutput과 알고 : 나는 여러 열이 일치과 같이 위치를 그룹화 할,

<cfoutput query="myQry" group="date"> 
    #date# 
    <cfoutput> 
    #detail# 
    </cfoutput> 
</cfoutput> 

그러나 : 나는 같은 하나의 열을 그룹화 할 때 그룹을 cfoutput과 사용하는 방법을 알고 위와 같이 작업하십시오. 그렇다면 어떻게 여러 열을 그룹화 할 수 있습니까?

답변

13

<cfoutput group=""> 태그를 추가합니다. 이은으로 "하나"그룹을 할 수있는 "가상"열을 생성하는 결과를 다시 조인

<cfoutput query="myQry" group="date"> 
<cfoutput group="category"> 
    <cfoutput group="description"> 
    #date# #category# #description# 
    <cfoutput> 
    #detail# 
    </cfoutput> 
    </cfoutput> 
</cfoutput> 
</cfoutput> 
+0

감사. 이전에이 작업을 수행 할 수 없었지만 지금은 저에게 효과적이기 때문에 분명히 실수했습니다. –

+3

그룹이 그룹화되지 않았거나 엉뚱한 것으로 보이는 경우 SQL의 order by 절을 확인하십시오. 그룹화 된 열이 그룹 중첩과 같은 순서로 순서대로 정렬되었는지 확인하십시오.이 경우 SELECT 열 (들) FROM 테이블 ORDER BY 날짜, 카테고리, 설명. – Travis

4

는 매트와 답을 가지고 있지만, 경우에 당신이 순수 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=# 
관련 문제