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