2

当我使用事务表的内部联接时,我得到了重复的帐号。如何限制它,所以我看不到重复的帐号。

SELECT     A01_AccountMaster.AccountNumber, A01_AccountMaster.FamilyMemberType,
           A01_AccountMaster.AccountType, A01_AccountMaster.FamilyId, 
           A01_AccountMaster.Title, A01_AccountMaster.FirstName, 
           A01_AccountMaster.MiddleName, A01_AccountMaster.LastName,
           A01_AccountMaster.Suffix, A01_AccountMaster.OrganizationName,
           A01_AccountMaster.Status,
           T01_TransactionMaster.Date,
           T01_TransactionMaster.AccountNumber AS T01_Accountnumber  
FROM       A01_AccountMaster
INNER JOIN T01_TransactionMaster
      ON A01_AccountMaster.AccountNumber = T01_TransactionMaster.AccountNumber
WHERE      (A01_AccountMaster.Title = 'The')
           AND (T01_TransactionMaster.Date between '01/01/2010' and '09/12/2012')
ORDER BY   AccountNumber;
4

2 回答 2

0

由于您从交易表中检索元素,如“T01_TransactionMaster.Date”,并且 2 个交易可以属于同一个帐户,这意味着对于 1 个帐户行,您可以返回多个交易日期。

您希望计算机如何返回适当的数据?

你有不同的选择。

就像删除事务列的选择语句并使用 DISTINCT 一样,这对于这样的选择语句来说似乎有点重:

SELECT DISTINCT A01_AccountMaster.AccountNumber, A01_AccountMaster.FamilyMemberType, A01_AccountMaster.AccountType, A01_AccountMaster.FamilyId, A01_AccountMaster.Title, A01_AccountMaster.FirstName, A01_AccountMaster.MiddleName, A01_AccountMaster.LastName, A01_AccountMaster.Suffix, A01_AccountMaster.OrganizationName, A01_AccountMaster.Status
FROM A01_AccountMaster 
INNER JOIN T01_TransactionMaster on (A01_AccountMaster.AccountNumber) = T01_TransactionMaster.AccountNumber 
WHERE (A01_AccountMaster.Title = 'The') 
and (T01_TransactionMaster.Date between '01/01/2010' and '09/12/2012') 
order by AccountNumber

或者如果您需要返回交易日期,您可以在所有帐户字段上使用 groupBy 有不同的策略,并在交易字段上进行聚合(例如,返回最大日期以获取帐户最后一次交易的日期)

SELECT A01_AccountMaster.AccountNumber,A01_AccountMaster.AccountType,........,max(T01_TransactionMaster.Date)
FROM A01_AccountMaster 
INNER JOIN T01_TransactionMaster on (A01_AccountMaster.AccountNumber) = T01_TransactionMaster.AccountNumber 
WHERE (A01_AccountMaster.Title = 'The') 
and (T01_TransactionMaster.Date between '01/01/2010' and '09/12/2012') 
group by A01_AccountMaster.AccountNumber,A01_AccountMaster.AccountType,........
order by AccountNumber
于 2012-09-12T23:54:20.770 回答
0

我的建议是总结事务表以从中获得您想要的东西。

要聚合数据,请使用如下查询:

select am.*, tm.NumTransactions
from A01_AccountMaster am join
     (select AccountNumber, count(*) as numtransactions
      from T01_TransactionMaster
      group by AccountNumber
     ) tm
     on am.AccountNumber = tm.AccountNumber
where . . .

在子查询级别进行聚合简化了整个查询,因为您不必使用大量字段进行分组。此外,如果您引入更多表,在子查询级别进行聚合可以防止其他表中的记录之间的交互导致的问题。

于 2012-09-13T00:50:15.910 回答