Access 2010缩小查询结果已更新

时间:2015-05-07 15:28:08

标签: sql ms-access

我有一个大表(tblRateandTransit,34K行),我只从中选择了大约8个字段。

|SCAC|CarrierName|OriginCity|OState|DestCity|DState|Rate |LaneTier|
|KNIG|Knight     |York      |PA    |Canton  |NJ    |$2.99| 2      |
|HJBB|JB Hunt    |York      |PA    |Canton  |NJ    |$3.67| 2      |
| Could be 4 more different Canton carriers carriers, diff rates..|

我还有一个表(tblFromToCityState,可能是4个字段,100行))从一个excel文件中提供,该文件是一个链接,我需要的城市来自/来自大表的信息。

|FCity |FState|DestCity|DState|
|York  |PA    |Canton  |NJ    |

还有一张小桌子,批准的承运人。

|SCAC|Carrier Name|
|CLLQ|Coyote      |
|HBG1|Hub Group   |
|KNIG|Knight      |

最终我的查询从Origin_City / State向Destination_City / State询问,首先是基于第1层批准的最佳运营商,如果该运输车道没有最新的运费,则为最佳运费。

第2阶段是,如果FROM City为空,则使用FROM状态。或者如果TO City为Null,请使用TO状态。

如果我执行简单的FROM - TO - MIN(RATE)查询,效果很好。

SELECT RateandTransit.[Origin City],
    RateandTransit.[Origin State],
    RateandTransit.[Destination City],
    RateandTransit.[Destination State],
    Min(RateandTransit.Rate) AS MinOfRate,
    RateandTransit.[Lane Tier]
FROM tblFromToCityState
INNER JOIN RateandTransit
    ON (tblFromToCityState.TO = RateandTransit.[Destination City])
    AND (tblFromToCityState.FROM = RateandTransit.[Origin City])
GROUP BY RateandTransit.[Origin City],
    RateandTransit.[Origin State],
    RateandTransit.[Destination City],
    RateandTransit.[Destination State],
    RateandTransit.[Lane Tier]
HAVING (((RateandTransit.[Lane Tier]) > 1))
ORDER BY RateandTransit.[Origin City] DESC,
    RateandTransit.[Lane Tier];

|Origin City|State|Dest City |State|MinRate|LaneTier|
|York       |PA   |Burlington| NJ  |$8.11  | 2      |
|York       |PA   |Canton    | MA  |$2.80  | 2      |
|etc...one line per destination city...

当我将Carrier添加到查询中时,我可能会得到4到6个FROM / TO组合的记录,因为每个运营商都不同,所以Group By不起作用。例如,对于伯灵顿来说,我只需要8.11美元的记录,这是最便宜的托运人,但我看到了所有这些。

SELECT RateandTransit.[Origin City],
    RateandTransit.[Origin State],
    RateandTransit.[Destination City],
    RateandTransit.[Destination State],
    RateandTransit.SCAC,
    RateandTransit.[Carrier Legal Name],
    Min(RateandTransit.Rate) AS MinOfRate,
    RateandTransit.[Lane Tier]
FROM tblFromToCityState
INNER JOIN RateandTransit
    ON (tblFromToCityState.TO = RateandTransit.[Destination City])
    AND (tblFromToCityState.FROM = RateandTransit.[Origin City])
GROUP BY RateandTransit.[Origin City],
    RateandTransit.[Origin State],
    RateandTransit.[Destination City],
    RateandTransit.[Destination State],
    RateandTransit.SCAC,
    RateandTransit.[Carrier Legal Name],
    RateandTransit.[Lane Tier]
HAVING (((RateandTransit.[Lane Tier]) > 1))
ORDER BY RateandTransit.[Origin City] DESC,
    RateandTransit.[Lane Tier];

|Origin City|State|Dest City |State|SCAC|Carrier    |MinRate|LaneTier|
|York       |PA   |Burlington| NJ  |MPLT|Paul Miller|$8.11  | 2      |
|York       |PA   |Burlington| NJ  |XPOL|XPO Logics |$9.46  | 2      |
|York       |PA   |Burlington| NJ  |HHWY|Hub Group  |$9.60  | 2      |
|and so on...

我需要以某种方式过滤,只需要那个FROM / TO TIER最便宜的价格。本网站上的另一个问题是我需要的,谈论部件号:

SELECT T1.*
FROM My_Table T1
INNER JOIN (
        SELECT [Ven Part Num],
            MIN(Price) AS MPrice
        FROM My_Table
        GROUP BY [Ven Part Num]
    ) T2
    ON T1.[Ven Part Num] = T2.[Ven Part Num]
    AND T1.Price = T2.MPrice

我只是不确定如何用我已经拥有的东西来实现它:

现在尝试使用多种可能性修复一周,就像在报告中使用我的查询一样,我可以在MIN字段上执行MIN(RATE),或使用{{ 1}}在另一个查询中查询:

MIN(RATE)

一切都无济于事。我需要帮助!

0 个答案:

没有答案