简体   繁体   English

如何转置以使各列具有不同的值,每列具有多行

[英]How to transpose making columns distinct values with multiples lines for each column

I have a table that each line has a customer name and a GUID that is a code for an audience. 我有一个表格,每一行都有一个客户名称和一个GUID,该GUID是针对受众的代码。 I want to have a resulta that each column has the GUIDs for each customer. 我想要一个结果,每个列都有每个客户的GUID。

Original table: 原始表格:

Customer        | Audience
                |
Customer A      | a4e7c9b7-c9d6-4698-8dc8-e0f2920f6bb7
Customer B      | 7a1d31b9-ecec-4c09-9eb9-cb10a8cc7d58
Customer A      | fa89f93d-ad7c-47ee-9a0c-598430c63c43
Customer C      | 0bc45f14-6c1b-48cf-a79e-a7cdc24deecc
Customer A      | 980b90cc-c923-46be-a180-bbbdf43169b2
Customer B      | 29d4806f-70fb-4b5f-9630-7314c32a536a

I need somethin like 'select Customer A, Customer B, Customer C ........ Audience FROM Table' 我需要一些东西,例如“选择客户A,客户B,客户C ........来自表的受众群体”

The result that I need is something like these: 我需要的结果是这样的:

在此处输入图片说明

It could work export everything to excel and manipulate there. 它可以将所有内容导出并在其中表现出色并进行操作。

Thanks! 谢谢!

One option would be a pivot query with the use of ROW_NUMBER to assign arbitrary positions to each GUID, for each customer. 一种选择是使用ROW_NUMBER为每个客户为每个GUID分配任意职位的数据透视查询。 We can do this with three separate CTEs, one for each of the three customers in your data set: 我们可以通过三个独立的CTE来完成此操作,对于您数据集中的三个客户,每个CTE:

WITH cteA AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rn
    FROM yourTable
    WHERE Customer = 'Customer A'
),
cteB AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rn
    FROM yourTable
    WHERE Customer = 'Customer B'
),
cteC AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rn
    FROM yourTable
    WHERE Customer = 'Customer C'
)

SELECT
    a.Audience,
    b.Audience,
    c.Audience
FROM cteA a
FULL OUTER JOIN cteB b
    ON a.rn = b.rn
FULL OUTER JOIN cteC c
    ON a.rn = c.rn AND b.rn = c.rn;

在此处输入图片说明

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将不同的列值转置为列名,并将 map 其他列值转置为每个列名 - How to transpose distinct column values to column names and map other column values to each column name 从多列获取值的最后一列 - Getting last column with values from multiples columns Oracle SQL在一个查询中选择多列不同的值,并根据不同的值对每一列进行分页 - Oracle SQL select multiple columns distinct values in one query with pagination requirement for each column based on distinct value Oracle - 每列的不同值(非不同计数) - Oracle - Distinct values of each column(Not distinct count) 如何计算表的每一列中的所有不同值 - How to count all distinct values in each column of table 如何使用 mysql 语句在每列中查找不同值的数量 - How to find number of distinct values in each column using mysql statement 如何在sql中设置列的每个不同值的唯一名称 - how to set unique name of each distinct values of a column in sql 如何在PostgreSQL中为每个列获取不同的值及其计数 - how to get distinct values and their count for each column in postgresql 将一列中的不同 500 个值拆分为 Oracle 中的 2 列,每列 250 个值 - Split distinct 500 values in one column into 2 columns of 250 values each in Oracle 如何在主列中的每个值的其他列中获取不同的值 - How to get distinct values in other columns per value in the primary column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM