我正在使用Access 2010,并且我继承了一个包含培训主题的表。其中一些科目是主要科目,一些科目是主要科目的子科目(均在同一张表中)。主表按编号索引:
ID | Type | Department | Subject | Sub 1 | Sub 2 | Sub 3
----+-----------+---------------+-----------------------------------+-----------+-----------+-----------
1 | Main | Candy | Chocolate covered bananas | 2 | 3 | 4
2 | Sub | Candy | Melting Chocolate | | |
3 | Sub | Candy | Dipping machine | | |
4 | Sub | Candy | Freezer Operation | | |
5 | Main | Candy | Chocolate covered Strawberries | 2 | 3 |
我试图制作一份报告,显示主要主题,然后是主要主题的子要求。所以报告看起来像这样:
糖果部门
涂有巧克力的香蕉
- 融化巧克力
- 浸渍机
- 冷冻机操作
巧克力草莓
- 融化巧克力
- 浸渍机
等...
我无法弄清楚如何使关系或查找命令正确以获得正确的引用。我开始怀疑我必须把表分成两个 - 一个用于主要科目,一个用于子要求。但这并不是一个很好的选择,因为它有点大表并且与其他报表和查询相关联。其中一些主题包含多达10个子区域(表格中的字段多于我在此处指出的字段,但其中很多字段对于解决方案来说并不是必需的)
答案 0 :(得分:1)
这可以通过写一个" Union"查询。
选择" SQL" Access中的查看模式可以使用此类查询。
这里的目标是" unpivot"数据,以便每个子类型都有一个返回主类型的引用。
SELECT Table1.ID AS MainID
, Table1.Department AS MainDepartment
, Table1.Subject AS MainSubject
, Table1_1.ID AS SubID
, Table1_1.Department AS SubDepartment
, Table1_1.Subject AS SubSubject
FROM Table1
INNER JOIN Table1 AS Table1_1 ON Table1.[Sub 1] = Table1_1.ID
UNION
SELECT Table1.ID AS MainID
, Table1.Department AS MainDepartment
, Table1.Subject AS MainSubject
, Table1_1.ID AS SubID
, Table1_1.Department AS SubDepartment
, Table1_1.Subject AS SubSubject
FROM Table1
INNER JOIN Table1 AS Table1_1 ON Table1.[Sub 2] = Table1_1.ID
UNION
SELECT Table1.ID AS MainID
, Table1.Department AS MainDepartment
, Table1.Subject AS MainSubject
, Table1_1.ID AS SubID
, Table1_1.Department AS SubDepartment
, Table1_1.Subject AS SubSubject
FROM Table1
INNER JOIN Table1 AS Table1_1 ON Table1.[Sub 3] = Table1_1.ID;
每个子类型的输出应该有1行。