2

概述:比较不同账户的农产品代理的日常交易,以发现代理和公司持股之间的差异,仅显示新产品;当前交易中的代码,但不是以前的交易。

目标:按频率最高的代码(即苹果)对交易进行排序。

当前表:

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

4

1 回答 1

1

我认为问题在于您的分组太细了。

AgentHolding在这种情况下,从子句中排除三个 Amount 列(CompanyHolding和)并在输出中聚合它们( )可能是有意义的。DifferenceGROUP BYsum

这是此更改将产生的示例:http ://www.sqlfiddle.com/#!3/fc4d4/7

注意:由于我不知道它的结构和数据,所以我省略了表格,但希望这能说明意图left joinPrevTrade

于 2012-10-15T09:01:13.537 回答