如何解决“查询未将指定的表达式'credit usd'作为聚合函数的一部分包括在内”?

时间:2019-05-08 13:56:10

标签: sql excel vba

我尝试选择“ max(TxnDate)作为txDate,[帐户名dr],[贷方usd]” ,但出现错误“查询不包含指定的表达式'credit在Excel VBA编程中使用“作为聚合函数的一部分”,但在工作中找到了我在SQL Server或PHPMyadmin中尝试过的情况

我正在使用VBA编程来开发MSExcel 2013

SELECT max(TxnDate) as txDate, [account name dr], [credit usd] from 
   (
       SELECT t.TxnDate, t.[account name dr], t.[credit usd] FROM 
       ( 
           select [account name dr], max([credit usd]) as max_c from [FT_Pure$] GROUP BY [account name dr]
       ) as a INNER JOIN [FT_Pure$] as t on (t.[account name dr] = a.[account name dr] and t.[credit usd] = max_c)
   ) as ta group by ta.[account name dr]

我希望输出应列出 txDate,account_name_dr和Credit_Usd

这是错误消息:

Error messsage

2 个答案:

答案 0 :(得分:0)

如果您尝试获取每个帐户的最新行,那么就不会想到聚合。您可以使用过滤:

select t.*
from [FT_Pure$] t
where t.TxnDate = (select max(t2.TxnDate)
                   from [FT_Pure$] t2
                   where t2.[account name dr] = t.[account name dr]
                  );

答案 1 :(得分:0)

外部GROUP BY子句缺少外部选择中的[credit usd]和[帐户名dr]。

只需将其添加到:

SELECT max(TxnDate) as txDate, [account name dr], [credit usd] from 
   (
       SELECT t.TxnDate, t.[account name dr], t.[credit usd] FROM 
       ( 
           select [account name dr], max([credit usd]) as max_c from [FT_Pure$] GROUP BY [account name dr]
       ) as a INNER JOIN [FT_Pure$] as t on (t.[account name dr] = a.[account name dr] and t.[credit usd] = max_c)
   ) as ta group by ta.[account name dr], [credit usd]