2013-10-10 4 views
0

에서 나는 아래 질문을 게시하고 greate 응답을 받았습니다.반환 된 데이터 제한 SQL

How to display rows that when added together equal zero

내 문제를 응답에 가장 가까운을 온 대답이 있었다 :

SELECT t1.* 
FROM transactions AS t1 
INNER JOIN 
(
    SELECT 
    tbis.client_ref , 
    tbis.supplier_key, 
    sum(tbis.client_amount) AS total 
    FROM transactions tbis 
    WHERE tbis.client_amount !=0 
    GROUP BY tbis.client_ref, tbis.supplier_key 
    HAVING sum(tbis.client_amount) =0 
) AS t2 ON t1.client_ref = t2.client_ref 
     AND t1.supplier_key = t2.supplier_key 
ORDER BY t2.total; 
난 데 문제는이 결과의 행을 포함된다는 점이다

경우 클라이언트 금액 = 0 나는 기본적으로 클라이언트 금액이 0이 아닌 행에서만 아래를 수행해야합니다.

아이디어가 있으십니까?

많은 감사

(다른 게시물에 묵시적으로) 또는 당신이 그것에 참여하지 않는 중 트랜잭션 테이블에 기본 키가 없기 때문에이 문제가 발생하는 것 같습니다
+0

*** SQL에 ***입니다 단지 * 구조적 쿼리 언어 * - 많은 데이터베이스 시스템에서 사용하는 언어 ,하지만 데이터베이스 제품 ... 많은 것들이 공급 업체에 따라 다릅니다 - 그래서 우리는 정말로 ** 데이터베이스 시스템 ** (그리고 어떤 버전)을 사용하고 있는지 알고 있어야합니다 (그에 따라 태그를 업데이트하십시오). –

답변

0

. 즉 0이 아닌 값을 가진 client_refsupplier_key 쌍이 적어도있는 한 모든 값이 반환됩니다. 는

SELECT t1.* 
FROM transactions AS t1 
INNER JOIN 
(
    SELECT 
    tbis.client_ref , 
    tbis.supplier_key, 
    sum(tbis.client_amount) AS total 
    FROM transactions tbis 
    WHERE tbis.client_amount !=0 
    GROUP BY tbis.client_ref, tbis.supplier_key 
    HAVING sum(tbis.client_amount) =0 
) AS t2 ON t1.client_ref = t2.client_ref 
     AND t1.supplier_key = t2.supplier_key 
WHERE t1.client_amount !=0 

ORDER BY t2.total; 

또는도는 외부 조인 쿼리에 WHERE를 추가 어느 client_amount

SELECT t1.* 
FROM transactions AS t1 
INNER JOIN 
(
    SELECT 
    tbis.client_ref , 
    tbis.supplier_key, 
    sum(tbis.client_amount) AS total 
    FROM transactions tbis 
    WHERE tbis.client_amount !=0 
    GROUP BY tbis.client_ref, tbis.supplier_key 
    HAVING sum(tbis.client_amount) =0 
) AS t2 ON t1.client_ref = t2.client_ref 
     AND t1.supplier_key = t2.supplier_key 
     AND t1.client_amount = t2.client_amount 

ORDER BY t2.total; 
+0

Genius ! 옵션 1은 문제를 치료 방법으로 분류했습니다. 당신은 고객 당 총을 얻어야했기 때문에 기본 키에 가입하지 않고 있다는 사실에 주목했습니다. 정말 고맙습니다! – chris1982

관련 문제