SQL子查询来自同一个表的所有数据

时间:2013-03-21 11:57:37

标签: 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 个答案:

答案 0 :(得分: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更优雅地完成此任务。

答案 1 :(得分:1)

您的子查询是独立的 - 它们不以任何方式依赖ndhist_acct_nbr字段,因此结果始终相同。

此外,这种技术(对每行输出使用这么多子查询)是个坏主意。 您应该简化查询,而不是count distinct和子查询,而是生成sum(case when ...子句。