当我使用事务表使用内部联接时,我会收到重复的帐号。如何限制它,所以我没有看到重复的帐号。
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;
答案 0 :(得分:0)
由于您从事务表中检索元素,如“T01_TransactionMaster.Date”,并且2个交易可以属于同一个帐户,这意味着对于1个帐户行,您可以返回多个交易日期。
您希望计算机如何返回适当的数据?
你有不同的选择。
就像删除事务列的select语句并使用DISTINCT一样,这对于这样的select语句来说似乎有点重:
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
OR 如果您需要返回交易日期,您可以在所有帐户字段上使用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
答案 1 :(得分: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 . . .
在子查询级别执行聚合可以简化整个查询,因为您不必使用大量字段进行分组。此外,如果引入更多表,则在子查询级别进行聚合可以防止由其他表中的记录之间的交互引起的问题。