概述:
比较不同账户的农产品代理的日常交易,以发现代理和公司持股之间的差异,仅显示新产品;当前交易中的代码,但不是以前的交易。
目标:
按频率最高的代码(即苹果)对交易进行排序。
当前代码:
SELECT
Count(CurrentTrade.Code) AS CountOfCode,
CurrentTrade.portfolio_code,
CurrentTrade.Type,
CurrentTrade.Code,
sum(CurrentTrade.Custodian_holding) AS AgentHolding,
sum(CurrentTrade.Company_holding) AS CompanyHolding,
sum(CurrentTrade.Difference) AS TotalDifference
FROM
CurrentTrade
LEFT JOIN PrevTrade
ON CurrentTrade.Company_holding = PrevTrade.Company_holding
WHERE
PrevTrade.Company_holding) Is Null
GROUP BY CurrentTrade.Code, CurrentTrade.portfolio_code, CurrentTrade.Code, CurrentTrade.Type
ORDER BY count(CurrentTrade.Code);
当前结果:
CountOfCode account_code Type Code AgentHodling CompanyHolding TotalDifference
1 acc001049 prod mango 2500 0 2500
1 acc636903 prod mango 481081.29 481081.285 0.01
1 acc179186 prod mango 453639.61 511285.81 -57646.2
1 acc179185 prod mango 2775 2650 125
1 acc170001 prod grapes 0 1500 -1500
1 acc170000 prod grapes 2500 0 2500
1 acc107104 prod apple 49797.22 49797.28 -0.06
1 acc107103 prod apple 0 -0.06 0.06
1 acc107102 prod apple 2000 500 1500
1 acc107101 prod apple 0 1500 -1500
1 acc001189 prod apple 0 1090.513 -1090.513
参考:
我收到了一个答案(按计数排序不正确 - SQL(MS Access 2007)),所以这是一个跟进。
问题:
是否可以在不汇总所有结果的情况下按频率排序,并且仍然按最高频率(苹果、芒果、葡萄)显示订单,包括关联的帐号?
如果使用 sum 作为 order by,“不能在 ORDER BY 中具有聚合函数”,所以我不确定我是否可以这样做并且仍然保持帐号分开?