2

1 つのフィールド (アカウント) を常に表示したいテーブルがあり、次に基準を使用してカウントまたは合計のサブクエリを表示します。

例:

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

問題は結果です。すべてのアカウント番号が表示されますが、カウント/合計フィールドはすべてデータを繰り返します。どんな助けでも素晴らしいでしょう!

4

2 に答える 2

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 を介してよりエレガントに実行できます。

于 2013-03-21T12:08:35.583 に答える
1

サブクエリはスタンドアロンですndhist_acct_nbr。フィールドにまったく依存しないため、結果は常に同じです。

さらに、この手法 (出力のすべての行に対してこれほど多くのサブクエリを使用する) は悪い考えです。クエリを単純化し、count distinctand サブクエリの代わりに makesum(case when ...句を使用する必要があります。

于 2013-03-21T12:08:57.633 に答える