报告行和列名称连接到一个值(Microsoft Access)

我对此深有体会,因为我不使用数据库。 我希望我已经试图通过帮助其他网站上的InDesign和Photoshop人员来付出代价。

我可以使用访问或Excel以下。

我有这样的数据:

一年总统副主席
 1980年里根·布什老人
 1984年里根·布什老人
 1988年布什高级奎尔
 1992年克林顿·戈尔
 1996年克林顿·戈尔
 2000年小布什切尼
 2004年布什小切尼
 2008年奥巴马拜登
 2012年奥巴马拜登

我想要一个如下的报告:

拜登:2008年副总裁,2012
小布什:总统2000年,2004年
小布什:1988年总统; 副1980年,1984年
切尼:副2000年,2004年
克林顿:1992年总统,1996年
戈尔:副1992年,1996年
奥巴马:总统2008年,2012年
奎尔:1988年的副手
里根:1980年总统,1984年

我无法弄清楚如何识别可能出现在表格任何位置的通用名称,以及如何获取报表的行列标签。

这是真实数据的简化版本,与政治家无关。 实际上有十个相关的列标签,而不仅仅是两个。 “老布什” 举一个人举两个不同的办公室的例子。

目前还没有任何情况下,在同一行的两个不同的列中出现相同的名称,但是我不想排除这种可能性,除非这样做是非常复杂的。

谢谢!

首先我们需要通过UNION查询将数据从“less数行,多列”转换为“less数列,多行”。 (我把你的testing数据保存在名为[N_column_table]的表中)

SELECT [year], "President" AS office, [President] AS person FROM [N_column_table] UNION ALL SELECT [year], "Vice" AS office, [Vice] AS person FROM [N_column_table] 

如果将该查询保存为“3_column_data”,则可以像在其他查询,报告等中一样使用该查询。(在为真实数据构build查询时,必须添加约8个UNION ALL构造。

所以现在我们的数据看起来像这样

 year office person 1980 President Reagan 1984 President Reagan 1988 President Bush Sr. 1992 President Clinton 1996 President Clinton 2000 President Bush Jr. 2004 President Bush Jr. 2008 President Obama 2012 President Obama 1980 Vice Bush Sr. 1984 Vice Bush Sr. 1988 Vice Quayle 1992 Vice Gore 1996 Vice Gore 2000 Vice Cheney 2004 Vice Cheney 2008 Vice Biden 2012 Vice Biden 

现在,对于办公室和年代的“粘合”,我们需要使用一点VBAfunction。 在Access中创build一个模块,并粘贴下面的代码

 Public Function ListTerms(person As String) As String Dim cdb As DAO.Database Dim rstOffice As DAO.Recordset, rstYear As DAO.Recordset Dim result As String, yearString As String Const YearSeparator = ", " Const OfficeSeparator = "; " Set cdb = CurrentDb result = "" Set rstOffice = cdb.OpenRecordset( _ "SELECT DISTINCT office " & _ "FROM 3_column_data " & _ "WHERE person=""" & Replace(person, """", """""", 1, -1, vbBinaryCompare) & """ " & _ "ORDER BY 1") Do While Not rstOffice.EOF yearString = "" Set rstYear = cdb.OpenRecordset( _ "SELECT DISTINCT [year] " & _ "FROM 3_column_data " & _ "WHERE person=""" & Replace(person, """", """""", 1, -1, vbBinaryCompare) & """ " & _ "AND office=""" & Replace(rstOffice!Office, """", """""", 1, -1, vbBinaryCompare) & """ " & _ "ORDER BY 1") Do While Not rstYear.EOF If Len(yearString) > 0 Then yearString = yearString & YearSeparator End If yearString = yearString & rstYear!Year rstYear.MoveNext Loop rstYear.Close Set rstYear = Nothing If Len(result) > 0 Then result = result & OfficeSeparator End If result = result & rstOffice!Office & " " & yearString rstOffice.MoveNext Loop rstOffice.Close Set rstOffice = Nothing Set cdb = Nothing ListTerms = result End Function 

现在我们可以在查询中使用这个函数来列出每个人和他们在办公室的条件

 SELECT personlist.[person], ListTerms(personlist.[Person]) as terms FROM (SELECT DISTINCT person FROM 3_column_data) personlist 

哪个返回

 person terms Biden Vice 2008, 2012 Bush Jr. President 2000, 2004 Bush Sr. President 1988; Vice 1980, 1984 Cheney Vice 2000, 2004 Clinton President 1992, 1996 Gore Vice 1992, 1996 Obama President 2008, 2012 Quayle Vice 1988 Reagan President 1980, 1984