2012-11-15 3 views
0

쿼리의 값을 디코딩하려고합니다. 상태에 따라, 나는 sysdate을 반환하거나 원하는 또는 컬럼의 MAX : SYSDATE가 집계 함수가 아니기 때문에집계 및 비 집계 결과가있는 디코드

SELECT DECODE(t2.productstatus, 'Stores', SYSDATE, MAX(t1.actual_moveout)) 
    INTO v_last_moveout 
    FROM rw_product_flow t1 
    JOIN rw_product_master t2 
    ON t1.facility = t2.facility 
    AND t1.product = t2.product 
WHERE t1.facility = p_facility 
    AND t1.product = p_product; 

그러나, 이것은하는 ORA-00937이 아닌 하나의 그룹 그룹 funciton 오류를 던지고있다 . 자세한 IF 블록을 작성하지 않고이를 달성하는 가장 좋은 방법은 무엇입니까?

+1

't2.productstatus'에 의해 그룹이 필요하지 않습니까? – Laurence

+0

예, 가능합니다. 'GROUP BY t1.facility, t1.product, t2.productstatus'를 추가하면 원하는 결과를 얻을 수 있습니다. – Paul

+0

@Laurence - 우승자가있는 것 같습니다. 그것을 약간의 설명과 함께 대답으로 추가하십시오. –

답변

1

Maxgroup by없이 자신의 것으로 사용할 수 있지만 다른 정보도 필요하면 데이터를 그룹화해야합니다.

내 심령 디버깅 능력

GROUP BY t1.facility, t1.product, t2.productstatus 

는 당신이 필요로 할 것입니다 말해 (MySQL은 많은 경우에 비 그룹화 열에 대한 의미있는 디폴트를 제공, 여기 다르다).

0

이 시도 :

SELECT DECODE(t2.productstatus, 'Stores', SYSDATE, MAX(t1.actual_moveout) over()) 
    INTO v_last_moveout 
    FROM rw_product_flow t1 
    JOIN rw_product_master t2 
    ON t1.facility = t2.facility 
    AND t1.product = t2.product 
WHERE t1.facility = p_facility 
    AND t1.product = p_product; 

난 당신이 디코드에 둥지 윈도 (일명 ​​분석) 기능을 할 수있는 100 % 확실하지 않다. 오류가 발생하면 파생 테이블을 사용하여 쉽게 해결할 수 있습니다.

select DECODE(productstatus, 'Stores', SYSDATE, max_moveout) 
from (
    SELECT t2.productstatus, 
      MAX(t1.actual_moveout) over() as max_moveout 
     INTO v_last_moveout 
     FROM rw_product_flow t1 
     JOIN rw_product_master t2 
     ON t1.facility = t2.facility 
     AND t1.product = t2.product 
    WHERE t1.facility = p_facility 
     AND t1.product = p_product 
)