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