我有一张表,我希望总是显示一个字段(帐户),然后子查询计数或带标准的总和。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
问题是结果 - 所有的帐户号码的出现,但计数/总和字段的所有重复数据。任何帮助都是极好的!
欢迎来到Stack Overflow!请通过添加适当的标记(Oracle,SQL Server,MySQL等)来指定您要定位的RDBMS。可能会有利用不被普遍支持的语言或产品功能的答案。此外,通过为特定的RDBMS添加标签,您的问题可能会得到更适合回答的人的关注。 – Taryn 2013-03-21 12:04:45