2

我有一个表,我希望始终显示一个字段(帐户),然后子查询计数或总和与标准。

例子:

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 distinct和子查询,制作sum(case when ...子句。

于 2013-03-21T12:08:57.633 回答