2

私はデータベースをあまり使用しないので、これについては十分に理解していません。他の Web サイトで 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 年

テーブルのどこにでも現れる一般的な名前を識別する方法と、レポートの行と列のラベルを取得する方法がわかりません。

これは実際のデータを単純化したもので、政治家とは関係ありません。関連する列ラベルは、実際には 2 つだけでなく、10 個あります。「ブッシュ・シニア」は、1 人の人物が 2 つの異なる役職に就いている例を示しています。

現在、同じ行の 2 つの異なる列に同じ名前が表示されるケースはありませんが、それを許可することが劇的に複雑でない限り、可能性を排除したくありません。

ありがとう!

4

1 に答える 1

2

最初に行う必要があるのは、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」として保存すると、他のクエリやレポートなどでテーブルと同じように使用できます (UNION ALL実際のデータのクエリを作成するときに、さらに 8 つの構造を追加する必要があります)。

これで、データは次のようになります

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
于 2013-04-05T21:29:46.887 に答える