数据透视表中的文本值?
我有一个包含 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。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在数据透视表中遇到了同样的问题...非常适合摘要,但不适用于文本矩阵。
我刚刚“举起了”我使用的一些代码示例。在这里,我在 AD 列中有数据,并围绕 F 列构建矩阵(在同一张表中)。
检查一下这是否有帮助。
我仍然无法让代码看起来正确,所以请注意,很多代码在代码窗口之前开始。
代码示例 1:
代码示例 2:
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:
Code Example 2:
我无法检查查询是否正常,但或多或少会是这样的查询:
I have no way to check if the query is fine, but more or less would be a query like this:
您所寻求的通常称为交叉表。这可以像这样静态地完成:
但是,如果您寻求的是动态数量的类别(以及列),则不能在 SQL 中本地完成。后面的解决方案称为动态交叉表。最好的方法是在中间层中构建类似于上面静态版本的 SQL 语句,或者使用可以执行相同操作的报告工具。
What you seek is often called a crosstab. This can be done statically like so:
However, if what you seek is to have a dynamic number of categories (and thus columns), this cannot be done natively in SQL. This later solution is called a dynamic crosstab. The best approach is either to build the SQL statement akin to the static version above in your middle-tier or using a reporting tool which will do the same.