首页 > 解决方案 > 在交叉表查询中按地名 ID 对列和行进行排序

问题描述

我有一个交叉表查询,它工作得很好,除了它按字母顺序而不是不同的顺序显示列。

我有一个从起点到目的地火车站的定价网格,当行和列显示的是 station_id 而不是车站名称时,我之前以正确的顺序拥有该网格。

这些站有一个按时间顺序排列的 station_id。这些站 ID 与 station_from 和 station_to 编号字段相关联,并且查询会查找 from 和 to 的站名称。

当它运行时,所有的列和行都按 station_name 的字母顺序排列,我希望它按照 station_id 的顺序(列和行)。有任何想法吗?我在 Office 365 的 MS Access 中执行此操作。

这是我的查询(感谢@kenwhite 帮助我),它采用了一个表格,该表格映射了车站和价格区之间的路线。

TRANSFORM Max(r.price) AS MaxOfprice
SELECT o.[Station Name]
 FROM ([zone_pricing Query] AS r 
   INNER JOIN stations AS o ON r.station_from = o.station_ID) 
   INNER JOIN stations AS d ON r.station_to = d.station_ID
 WHERE (((r.ticket_type)='adult') AND ((r.season_ID)=1))
GROUP BY o.[Station Name], r.season_ID, r.ticket_type
PIVOT d.[Station Name];

zone_pricing 查询如下所示:

station_from,   station_to, zone_id,    price,  price_type, price_class,    season_ID,  ticket_type,    route_id

1   2   1   $58.00  base    flexi   1   adult   1

1   3   1   $58.00  base    flexi   1   adult   1

2   3   1   $58.00  base    flexi   1   adult   1

1   4   1   $58.00  base    flexi   1   adult   1

2   4   1   $58.00  base    flexi   1   adult   1

3   4   1   $58.00  base    flexi   1   adult   1

22  4   1   $58.00  base    flexi   1   adult   1

1   5   2   $83.00  base    flexi   1   adult   1

2   5   2   $83.00  base    flexi   1   adult   1

3   5   2   $83.00  base    flexi   1   adult   1

22  5   2   $83.00  base    flexi   1   adult   1

我的电台看起来像这样:

Station_ID, Location,   Station Name,   Scheduled Stop, Short Platform, Station Type,   station_code

1   Auckland    Auckland Strand Yes No  Rail    AKL

2       Middlemore  No  No  Rail    MDM

3       Papakura    Yes No  Rail    PAP

4       Hamilton    Yes No  Rail    HLR

5       Te Awamutu  No  Yes Rail    TWM

6       Otorohanga  Yes Yes Rail    OTO

7       Te Kuiti    No  Yes Rail    TEK

8       Taumarunui  No  No  Rail    TAU

标签: ms-access

解决方案


推荐阅读