按计数排序未正确排序 - SQL (MS Access 2007)
Posted
技术标签:
【中文标题】按计数排序未正确排序 - SQL (MS Access 2007)【英文标题】:Order by count not sorting correctly - SQL (MS Access 2007) 【发布时间】:2012-10-15 03:18:05 【问题描述】:概述: 比较不同账户的农产品代理日常交易,找出代理和公司持股之间的差异,仅显示新产品;当前交易中的代码,但不是以前的交易。
目标: 按频率最高的代码(即苹果)对交易进行排序。
当前表:
--------------------------------------------------------------------------------------------------------------
| DATE | ACCT_CODE | TYPE | TYPE_DESCRIPTION | CODE | DIFFERENCE | AGENT_HOLDING | COMPANY_HOLDING |
--------------------------------------------------------------------------------------------------------------
| 02 Aug 2012 | acc107101 | prod | produce | apple | -1500 | 0 | 1500 |
| 02 Aug 2012 | acc107101 | prod | produce | apple | 1500 | 2000 | 500 |
| 02 Aug 2012 | acc107101 | prod | produce | apple | 0 | 0 | 0 |
| 02 Aug 2012 | acc107101 | prod | produce | apple | 0 | 49797 | 49797 |
| 02 Aug 2012 | acc170000 | prod | produce | grapes | 2500 | 2500 | 0 |
| 02 Aug 2012 | acc170000 | prod | produce | grapes | -1500 | 0 | 1500 |
| 02 Aug 2012 | acc008221 | caus | cash | cash | 0 | 0 | 0 |
| 02 Aug 2012 | acc008221 | caus | cash | carrots | 0 | 43273 | 43274 |
| 01 Aug 2012 | acc008221 | caus | cash | cash | 0 | 0 | 0 |
| 01 Aug 2012 | acc008221 | caus | cash | carrots | 0 | 43273 | 43274 |
| 02 Aug 2012 | acc179185 | prod | produce | mango | 125 | 2775 | 2650 |
| 02 Aug 2012 | acc179185 | prod | produce | mango | -57646 | 453639 | 511286 |
| 02 Aug 2012 | acc636903 | prod | produce | mango | 0 | 481081 | 481081 |
| 02 Aug 2012 | acc001049 | prod | produce | mango | 2500 | 2500 | 0 |
| 02 Aug 2012 | acc001189 | prod | produce | apple | -1091 | 0 | 1091 |
--------------------------------------------------------------------------------------------------------------
代码:
SELECT
Count(CurrentTrade.Code) AS CountOfCode,
CurrentTrade.date,
CurrentTrade.Acct_Code,
CurrentTrade.Type,
CurrentTrade.Code,
CurrentTrade.Agent_Holding,
CurrentTrade.Company_Holding,
CurrentTrade.Difference
FROM CurrentTrade
LEFT JOIN PrevTrade
ON CurrentTrade.Company_Holding=PrevTrade.Company_Holding
WHERE (((PrevTrade.Company_Holding) Is Null) AND ((CurrentTrade.Code)<>'cash'))
GROUP BY
CurrentTrade.Code,CurrentTrade.date,
CurrentTrade.Acct_Code,
CurrentTrade.Type,
CurrentTrade.Agent_Holding,
CurrentTrade.Company_Holding,
CurrentTrade.Difference
ORDER BY count(CurrentTrade.Code) ASC;
当前结果:
---------------------------------------------
| COUNTOFCODE | DATE | TYPE | CODE |
---------------------------------------------
| 1 | 02 Aug 2012 | prod | grapes |
| 1 | 02 Aug 2012 | prod | grapes |
| 1 | 02 Aug 2012 | prod | apple |
| 1 | 02 Aug 2012 | prod | apple |
| 1 | 02 Aug 2012 | prod | apple |
| 1 | 02 Aug 2012 | prod | apple |
| 1 | 02 Aug 2012 | prod | apple |
| 1 | 02 Aug 2012 | prod | mango |
| 1 | 02 Aug 2012 | prod | mango |
| 1 | 02 Aug 2012 | prod | mango |
| 1 | 02 Aug 2012 | prod | mango |
---------------------------------------------
问题: 如何修复以使用正确的 ORDER BY 频率(苹果、芒果、葡萄)?
【问题讨论】:
【参考方案1】:我认为问题在于您的分组太细了。
在这种情况下,从 GROUP BY
子句中排除三个 Amount 列(AgentHolding
、CompanyHolding
和 Difference
)并在输出中聚合它们(sum
)可能是有意义的。
以下是此更改将产生的结果的示例:http://www.sqlfiddle.com/#!3/fc4d4/7
注意:我省略了 left join
和 PrevTrade
表,因为我不知道它的结构和数据,但希望这能说明意图。
【讨论】:
太棒了..谢谢!这更清洁并且有效。我添加了左连接,因为查询会即时比较 2 个表/2 个日期。以上是关于按计数排序未正确排序 - SQL (MS Access 2007)的主要内容,如果未能解决你的问题,请参考以下文章