2013-03-21 3 views
2

나는 하나의 필드 (계정)를 항상 표시하고 그 다음 계수 또는 합계 기준에 대해 하위 쿼리를 표시하고자하는 테이블이 있습니다.SQL 하위 쿼리 같은 테이블의 모든 데이터

예 :

select ndhist_acct_nbr,  
    (select count(ndhist_acct_nbr) from dbo.nd_history where ndhist_type = '30' 
     and ndhist_rsn = '0' and ndhist_trcd = 'NF*' and ndhist_ref_type = '0' and ndhist_dt >= '03/01/2013') as NSF_TOTAL, 
    (select sum(ndhist_amt) from dbo.nd_history where ndhist_type = '30' 
     and ndhist_rsn = '98' and ndhist_trcd = 'TW0' and ndhist_ref_type = '11' and ndhist_dt >= '03/01/2013') as SIG_SPEND, 
    (select count(ndhist_acct_nbr) from dbo.nd_history where ndhist_type = '30' 
     and ndhist_rsn = '23' and ndhist_trcd = 'TW0' and ndhist_ref_type = '11' and ndhist_dt >= '03/01/2013') as PIN_TRANS, 
    (select count(ndhist_acct_nbr) from dbo.nd_history where ndhist_type = '30' 
     and ndhist_rsn = '21' and ndhist_trcd = 'SC*' and ndhist_ref_type = '0' and ndhist_dt >= '03/01/2013') as FOREIGN_AMT_FEE 
from dbo.nd_history 
group by ndhist_acct_nbr 

문제는 결과입니다 - 계좌 번호가 모두 표시하지만 카운트/합이 모든 데이터를 반복 필드. 어떤 도움이 굉장 할 것입니다!

+2

스택 오버플로에 오신 것을 환영합니다! 해당 태그 (Oracle, SQL Server, MySQL 등)를 추가하여 대상으로 삼고있는 RDBMS를 지정하십시오. 보편적으로 지원되지 않는 언어 나 제품 기능을 이용하는 대답이있을 수 있습니다. 또한 특정 RDBMS로 태그를 달아서 질문에 답하는 것이 더 적합한 사람들로부터주의를받을 수 있습니다. – Taryn

답변

1

시도 :

select ndhist_acct_nbr,  
     count(case when ndhist_type = '30' and ndhist_rsn = '0' and ndhist_trcd = 'NF*' and ndhist_ref_type = '0' and ndhist_dt >= '03/01/2013' 
        then ndhist_acct_nbr end) as NSF_TOTAL, 
     sum(case when ndhist_type = '30' and ndhist_rsn = '98' and ndhist_trcd = 'TW0' and ndhist_ref_type = '11' and ndhist_dt >= '03/01/2013' 
       then ndhist_amt end) as SIG_SPEND, 
     count(case when ndhist_type = '30' and ndhist_rsn = '23' and ndhist_trcd = 'TW0' and ndhist_ref_type = '11' and ndhist_dt >= '03/01/2013' 
        then ndhist_acct_nbr end) as PIN_TRANS, 
     count(case when ndhist_type = '30' and ndhist_rsn = '21' and ndhist_trcd = 'SC*' and ndhist_ref_type = '0' and ndhist_dt >= '03/01/2013' 
        then ndhist_acct_nbr end) as FOREIGN_AMT_FEE 
from dbo.nd_history 
group by ndhist_acct_nbr 

당신은에서 선택 후 인라인 쿼리 내부의 모든 쿼리를 넣고하여 결과에서 열을 파생 사용할 수 있습니다 -과 같이 :

select sq.*, 
     NSF_TOTAL*5 + SIG_SPEND*0.10 + PIN_TRANS*0.05 + FOREIGN_ATM_FEE as TOTAL_INCOME 
from 
(select ndhist_acct_nbr,  
     count(case when ndhist_type = '30' and ndhist_rsn = '0' and ndhist_trcd = 'NF*' and ndhist_ref_type = '0' and ndhist_dt >= '03/01/2013' 
        then ndhist_acct_nbr end) as NSF_TOTAL, 
     sum(case when ndhist_type = '30' and ndhist_rsn = '98' and ndhist_trcd = 'TW0' and ndhist_ref_type = '11' and ndhist_dt >= '03/01/2013' 
        then ndhist_amt end) as SIG_SPEND, 
     count(case when ndhist_type = '30' and ndhist_rsn = '23' and ndhist_trcd = 'TW0' and ndhist_ref_type = '11' and ndhist_dt >= '03/01/2013' 
        then ndhist_acct_nbr end) as PIN_TRANS, 
     count(case when ndhist_type = '30' and ndhist_rsn = '21' and ndhist_trcd = 'SC*' and ndhist_ref_type = '0' and ndhist_dt >= '03/01/2013' 
        then ndhist_acct_nbr end) as FOREIGN_AMT_FEE 
from dbo.nd_history 
group by ndhist_acct_nbr) sq 

이 작업을 수행 할 수 CTE를 지원하는 RDBMS (예 : Oracle, PostgreSQL 또는 SQLServer)를 사용하는 경우 CTE를 통해보다 우아하게 처리 할 수 ​​있습니다.

+0

수입이 (NSF_TOTAL) * 5) + ((SIG_SPEND) * 0.10 %) + ((PIN_TRANS) * $ 0.05) + ((FOREIGN_ATM_FEE) * $ 1.00) 항목을 추가하려면 어떻게해야합니까?)) as TOTAL_INCOME –

+0

@TomMurphy : 나는 이것을 어떻게 할 수 있는지 보여주기 위해 나의 대답을 업데이트했다. 현재 SIG_SPEND의 10 %를 사용하고있다; 대신에 0.1 %를 원하면 TOTAL_INCOME의 SIG_SPEND 구성 요소를'SIG_SPEND * 0.001'로 변경하십시오. –

1

하위 쿼리는 독립 실행 형이며 어떤 식 으로든 ndhist_acct_nbr 필드에 의존하지 않으므로 결과는 항상 동일합니다.

게다가,이 기술은 (출력의 모든 행에 대해이 많은 서브 쿼리를 사용하는) 나쁜 생각입니다. 쿼리를 단순화하고 count distinct 및 하위 쿼리 대신 sum(case when ... 절을 만듭니다.