通过计数为了不正确排序 - SQL(MS Access 2007年)(Order by count

2019-09-24 02:46发布

概述:比较日常交易的产品作为代理各种帐户找代理及公司持股仅显示有什么新的区别; 代码在当前的交易,而不是以前的交易。

目的:按代码频率最高的行业(如苹果)。

当前表:

--------------------------------------------------------------------------------------------------------------
|        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频率(苹果,芒果,葡萄)?

Answer 1:

我认为问题是,你正在做的分组过于详尽。

在这种情况下,它很可能是有意义的排除三个少量列( AgentHoldingCompanyHoldingDifference从) GROUP BY子句中,只是它们聚集( sum )输出。

以下是对这种变化会产生一个例子: http://www.sqlfiddle.com/#!3/fc4d4/7

注:我离开了left joinPrevTrade表,因为我不知道它的结构和数据,但希望这说明了意图。



文章来源: Order by count not sorting correctly - SQL (MS Access 2007)