将行数据转换为列。访问

时间:2014-11-03 18:21:15

标签: ms-access ms-access-2010 ms-access-2013

似乎这应该是相当简单的,但我已经搜索并搜索并尝试了一段时间,现在已经超出了我。

我有一个名为“Antenna_Electrical_Parameters”的表,如下所示:

站点ID天线ID电气控制器电气倾斜

Site_106 3 Port 1-2 4

Site_106 3 Port 3-4 4

Site_106 4 Port 1-2 1

Site_106 4 Port 3-4 4

Site_106 4 Port 5-6 4

Site_106 5 Port 1-2 1

Site_106 5 Port 3-4 7

Site_106 5端口5-6 7

Site_106 6 Port 1-2 10

Site_106 6 Port 3-4 7

Site_106 6端口5-6 7

Site_106 7端口1-8 4

Site_106 8端口1-8 1

Site_106 9端口1-8 3

我想构建一个可以产生这个的查询:

站点ID天线ID控制器1控制器1电动倾斜控制器2控制器2电动倾斜控制器3控制器3电气倾斜

Site_106 3端口1-2 4端口3-4 4

Site_106 4端口1-2 1端口3-4 4端口5-6 4

Site_106 5端口1-2 1端口3-4 7端口5-6 7

Site_106 6端口1-2 10端口3-4 7端口5-6 7

Site_106 7端口1-8 4

Site_106 8端口1-8 1

Site_106 9端口1-8 3

我还需要查询作为子表单的源。使用交叉表查询,我有点接近我想要的格式,但后来无法使用该查询作为子表单的源。

感谢。

How I would like to see the data

1 个答案:

答案 0 :(得分:0)

我最终构建了一个名为qry_ControllerSequence的选择查询:

SELECT t1。[Site ID],t1。[Antenna ID]," Controller" &安培;计数(*)AS ControllerSequence,t1。[电气控制器],t1。[电气倾斜] FROM Antenna_Electrical_Parameters AS t1 LEFT JOIN Antenna_Electrical_Parameters AS t2 ON(t2。[ID]< = t1。[ID])AND(t2。[Site ID] = t1。[Site ID])AND(t2。[Antenna ID] = t1。[天线ID]) GROUP BY t1。[Site ID],t1。[Antenna ID],t1。[Electrical Controller],t1。[Electrical Tilt];

然后是两个交叉表查询,第一个叫qry_ControllerPorts:

TRANSFORM First(qry_ControllerSequence。[电气控制器])AS [FirstOfElectrical Controller] SELECT qry_ControllerSequence。[Site ID],qry_ControllerSequence。[Antenna ID] 来自qry_ControllerSequence GROUP BY qry_ControllerSequence。[Site ID],qry_ControllerSequence。[Antenna ID] PIVOT qry_ControllerSequence.ControllerSequence In(" Controller 1"," Controller 2"," Controller 3"," Controller 4");

第二个叫qry_ControllerET:

TRANSFORM First(qry_ControllerSequence。[Electrical Tilt])AS [FirstOfElectrical Tilt] SELECT qry_ControllerSequence。[Site ID],qry_ControllerSequence。[Antenna ID] 来自qry_ControllerSequence GROUP BY qry_ControllerSequence。[Site ID],qry_ControllerSequence。[Antenna ID] PIVOT qry_ControllerSequence.ControllerSequence In(" Controller 1"," Controller 2"," Controller 3"," Controller 4");

然后使用选择查询将它们组合在一起:

SELECT qry_ControllerPorts。[Site ID],qry_ControllerPorts。[Antenna ID],qry_ControllerPorts。[Controller 1],qry_ControllerET。[Controller 1] AS [Controller 1 ET],qry_ControllerPorts。[Controller 2],qry_ControllerET。[Controller 2] ] AS [Controller 2 ET],qry_ControllerPorts。[Controller 3],qry_ControllerET。[Controller 3] AS [Controller 3 ET],qry_ControllerPorts。[Controller 4],qry_ControllerET。[Controller 4] AS [Controller 4 ET] FROM qry_ControllerPorts INNER JOIN qry_ControllerET ON(qry_ControllerPorts。[Antenna ID] = qry_ControllerET。[Antenna ID])AND(qry_ControllerPorts。[Site ID] = qry_ControllerET。[Site ID]);

在添加" In(" Controller 1"," Controller 2" &&)之前,我不会将选择查询用作子表单的源代码。 #34;控制器3","控制器4")"到交叉表查询结束时的PIVOT函数(如上面的代码所示)。

如果有人知道更简单或更清洁的方式,请告诉我。否则,此解决方案有效。

感谢。