我对这个问题很不了解,因为我不怎么使用数据库。我希望这足以让我通过在其他网站上帮助人们使用InDesign和Photoshop来传递它!
我可以使用Access或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查询将该数据从"少行多列"转换为"少列多行"。(我将测试数据保存在一个名为[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",那么你可以像在其他查询、报告等中使用表一样使用它。(当您为真实数据构建查询时,您将不得不添加~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
现在,为了"粘合"办公室和年份,我们需要使用一个小的VBA函数。在Access中创建一个模块,并粘贴以下代码
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