gpt4 book ai didi

sql - 数据透视表中的文本值?

转载 作者:行者123 更新时间:2023-12-04 20:47:41 28 4
gpt4 key购买 nike

我有一个包含 3 列的表(在 MySQL 中):

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

现在我需要制作一个包含上述信息的矩阵,如下所示:
       Computers      Printers       Software

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

最终我需要在 Excel 中有这个。

实际上,我有可变数量的类别,因此在 MySQL 中为每列连接并不是一个好的选择。我可以用 PHP 编写一个函数,但我想知道是否有更优雅的解决方案。

我在 Excel 中查看了数据透视表,但它们似乎更适合将数字作为值。但也许我忽略了一些东西,因为我自己从不使用 Excel。

有任何想法吗?

最佳答案

我在使用数据透视表时遇到了同样的问题……非常适合摘要,但不适用于文本矩阵。

我刚刚“解除”了一些我使用的代码示例。在这里,我在 A-D 列中有数据,并在 F 列周围构建矩阵(在同一张表中)。

检查这是否有帮助。

我仍然无法让代码看起来正确,所以请注意很多代码在代码窗口之前开始。

代码示例 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

代码示例 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

关于sql - 数据透视表中的文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7744119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com