2013-12-14 1 views
1

Oracle 11g 버전 1.1에서 SQL을 생성하여 여러 행의 열 값을 연결할 수 있습니까?Oracle 11g 버전 1.1에서 여러 행의 열 값을 연결하는 SQL 쿼리

dName CName totalAmount count  type 
A  B  600   3  water,house 
A  C  400   1   air  

하는 출력을위한 중복도 형식 별개의 같은 ..

그래서 기본적 유형의 열을 제거 -

Table A 

dName cName amount type 
    A  B  100 water 
    A  B  200 house 
    A  C  400 air 
    A  B  300 water 

는 SQL의 출력이되어야한다 :

다음 예입니다 result는 dName 및 cName에 의해 표 A의 유형 값과 sum (amount) 그룹의 결합입니다.

SQL에 대한 도움이 필요합니까? Oracle 11g 버전 1.1을 사용하고 있습니다. 따라서 listagg() 함수는 작동하지 않습니다. 실제로 collect() 함수를 사용하고 싶지 않습니다. 현재 테이블 구조를 변경할 필요가 없다는 뜻입니다.

답변

1

당신은 wm_concat(), 지원되지 않는 기능을 시도 할 수 있습니다 :

select dName, CName, totalAmount, "count", wm_concat("type") as "type" 
from a 
group by dName, CName, totalAmount, "count"; 

Here이 오라클에 끝낼 수있는 다양한 방법에 좋은 자원이다.

편집 :

당신이 당신의 자신의 함수를 작성하지 않으과 함께 가져 가지의 단지 소수가있는 경우, 조건부 집계 방법을 사용할 수 있습니다 :

select dName, CName, totalAmount, "count", 
     (max(case when seqnum = 1 then "type" end) || 
     max(case when seqnum = 2 then ','||"type" end) || 
     max(case when seqnum = 3 then ','||"type" end) || 
     max(case when seqnum = 4 then ','||"type" end) || 
     max(case when seqnum = 5 then ','||"type" end) 
     ) as "type" 
from (select a.*, 
      row_number() over (partition by dName, CName, totalAmount, "count" 
           order by "type" 
           ) as seqnum 
    ) a 
group by dName, CName, totalAmount, "count"; 
+0

11.1 am. wm_concat 지원 안함 – Abhi

0

이 시도 오라클 11g의 쿼리

01,234,907,979 :

select t1.dName,t1.CName,t2.TotalAmount,t2.count,t1.type from 
(select dName,CName,LISTAGG(TYPE,',') within group(order by TYPE)as type 
from 
(select UNIQUE dName,CName,type from A) 
group by dName, CName)t1, 
(select dName,CName,sum(amount) as TotalAmount,count(amount) as count 
from A group by dName, CName)t2 
where t1.dName||t1.CName = t2.dName||t2.CName order by CName; 

이 쿼리를 시도 listagg 사용할 수없는 경우