Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

sql - Text values in a pivot table?

I have a table (in MySQL) with 3 columns:

Location    Category     Supplier

   A        Computers    Company X
   A        Printers     Company Y
   B        Computers    Company X
   B        Printers     Company Y
   B        Software     Company Y
   C        Computers    Company Y
   C        Software     Company Z

Now I need to make a matrix containing the above information, like this :

       Computers      Printers       Software

A      Company X      Company Y
B      Company X      Company Y      Company Y
C      Company Y                     Company Z

Eventually I need to have this in Excel.

In reality I have a variable number of categories, so doing it in MySQL with a join for each column is not a good option. I could write a function in PHP, but I was wondering if there's a more elegant solution.

I looked a pivot tables in Excel, but they seem more suited for numbers as values. But maybe I'm overlooking something, since I never work with Excel myself.

Any idea's?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ran into the same problem with pivot tables... Perfect for summaries, but not for text matrices.

I have just "lifted" some code examples that I used. Here I have the data in columns A-D and build the matrix (in the same sheet) around column F.

Check to see if this helps.

I still have trouble getting the code to look right , so please be aware that a lot of the code starts before the code window.

Code Example 1:

'Fill in the values

Sheets("TempFile").Select

ListRow = 1

MisMatchCounter = 0

Do Until Cells(ListRow, 1).Value = ""

    ' Get table entry from third column of list.

    TableEntry = Cells(ListRow, 3).Value

    On Error Resume Next

    If Err.Number > 0 Then MsgBox Err.Number

    ' Get position of product name within range of row titles.

    If TableEntry <> "" Then

        TableRow = Application.Match(Cells(ListRow, 1), Range("F3:" & MYLastRowAddress), 0) ' 2 rows less than reality

        ' Get position of product size within range of column titles.

        TableColumn = Application.Match(Cells(ListRow, 2), Range("G2:" & MYLastColAddress), 0)

        Set CellToFill = Range("F2").Offset(TableRow, TableColumn)

        ' If there's already an entry in the cell, separate it from the new entry with a comma and space.

        If Err.Number = 0 Then

            If CellToFill.Value <> "" Then

                CellToFill.Value = CellToFill.Value & ","

                CellToFill.Value = CellToFill.Value & TableEntry

            Else

                CellToFill.Value = TableEntry

            End If

        Else

            MisMatchCounter = MisMatchCounter + 1

            Sheets("Errors").Cells(MisMatchCounter, 1).Value = ListRow

            Sheets("Errors").Cells(MisMatchCounter, 2).Value = Cells(ListRow, 1)

            Sheets("Errors").Cells(MisMatchCounter, 3).Value = Cells(ListRow, 2)

            Sheets("Errors").Cells(MisMatchCounter, 4).Value = Cells(ListRow, 3)

            Sheets("Errors").Cells(MisMatchCounter, 5).Value = Cells(ListRow, 4)

        End If

    End If

    On Error GoTo 0

    ListRow = ListRow + 1

Loop

Code Example 2:

Sub CreateManualMatrix()

    Dim TableRow, TableColumn As Integer

    Dim TableEntry As String

    Dim CellToFill As Range

    'Sheet is called Lijst

    'Column A is names for top row

    'Column B is names for left column

    'Column C is value for Matrix



    'Matrix Top Row starts at H1

    'Matrix Left Column starts at G2



    MatrixLastColAddress = Range("H1").End(xlToRight).Address

    MatrixLastRow = Range("G65536").End(xlUp).Row

    LijstReadColumn = 3

    LijstCurrentRow = 2 'make 1 if no header is used

    Do Until Sheets("Lijst").Cells(LijstCurrentRow, 1).Value = ""

        ' Get table entry from third column of list.

        TableEntry = Sheets("Lijst").Cells(LijstCurrentRow, LijstReadColumn).Value

        ' Get position of Employee name within Matrix.

        TableColumn = Application.Match(Sheets("Lijst").Cells(LijstCurrentRow, 1), Range("H1:" & MatrixLastColAddress), 0)

        ' Get position of Qualification Name within Matrix titles.

        TableRow = Application.Match(Sheets("Lijst").Cells(LijstCurrentRow, 2), Range("G2:G" & MatrixLastRow), 0)

        Set CellToFill = Range("G1").Offset(TableRow, TableColumn)

        ' If there's already an entry in the cell, separate it from the new entry with a comma and space.

        If CellToFill.Value <> "" Then CellToFill.Value = CellToFill.Value & ","

        ' Add the new entry to the cell.

        CellToFill.Value = CellToFill.Value & TableEntry

        LijstCurrentRow = LijstCurrentRow + 1

    Loop

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...