sql - 在单行上分组事务

标签 sql excel vba

假设我有以下包含交易数据的数据集。下面的子集用于说明目的:

BillName    Item Number
Customer A  477565
Customer A  8400212
Customer A  8400213
Customer A  461230
Customer A  461240
Customer A  477545
Customer A  8657915
Customer B  672050
Customer B  892223
Customer C  640741
Customer C  640772
Customer C  640660

如何使用 Excel 或 SQL Server 将其转换为如下所示?
Customer A  477565  8400212 8400213 461230  461240  477545  8657915
Customer B  672050  892223                  
Customer C  640741  640772  640660  

最佳答案

在 excel 中,添加另一行以显示条目的顺序并创建数据透视表:

BillName 组项目编号优先级

BillName    group   Item Number precedence
Customer    A   477565  1
Customer    A   8400212 2
Customer    A   8400213 3
Customer    A   461230  4
Customer    A   461240  5
Customer    A   477545  6
Customer    A   8657915 7
Customer    B   672050  1
Customer    B   892223  2
Customer    C   640741  1
Customer    C   640772  2
Customer    C   640660  3

创建一个如下所示的数据透视表:

enter image description here

或以表格形式重复所有标签
enter image description here

编辑:对于 SQL,请查看 Pivot 关系运算符来执行此类操作。

关于sql - 在单行上分组事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47085871/

相关文章:

sql - 如何计算 SQL 中的小数位?

excel - scripting.dictionary 无法识别数字

arrays - 带查找的动态数组的行式文本连接

ms-access - WScript.Shell 运行时错误 - IwshShell3 失败

excel - 输入日期后,宏将运行

mysql - 如何在MYSQL的更新语句中使用Substring_Index

如果另一张表上不存在特定行,则MySQL有条件地插入到一张表中

excel - 编译错误: Syntax Error caused by code inserting a excel formula

sql - IF 语句而不是选择

excel - 使用 VBA 将 Excel 电子表格中嵌入的 Word 文档保存到磁盘