我看到了很多关于如何将行转换为列的示例,并且我能够做到这一点。这是具有以下内容的表:
Userid action date TRACKING zip Subject
cbg10 O 1/2/2020 Amz 30105 $1 Sales journey
cbg10 O 1/2/2020 Amz 30105 $1 Sales journey
cbg10 C 1/2/2020 Amz 30105 $1 Sales journey
cbg10 P 1/2/2020 Amz 30105 $1 Sales journey
kel64 O 1/2/2020 Amz 45685 $1 Sales journey
kel64 O 1/2/2020 Amz 45685 $1 Sales journey
kel64 O 1/2/2020 Amz 45685 $1 Sales journey
kel64 C 1/2/2020 Amz 45685 $1 Sales journey
kel64 C 1/2/2020 Amz 45685 $1 Sales journey
kel64 p 1/2/2020 Amz 45685 $1 Sales journey
cbg10 O 4/18/2020 TG 30105 Summer arrives
cbg10 P 4/18/2020 TG 30105 Summer arrives
cbg10 C 4/18/2020 TG 30105 Summer arrives
这就是我希望表格的样子:正如您所看到的,有些打开的电子邮件打开了 2 次。而不是像第一个表中那样有 2 行,而是按主题添加人员采取的操作
Userid date O(Open) C(Click) P(Purchase) TRACKING zip Subject
cbg10 1/2/2020 2 1 1 Amz 30105 $1 Sales journey
kel64 1/2/2020 3 2 1 Amz 45685 $1 Sales journey
cbg10 4/2/2020 1 1 1 Amz 30105 Summer arrives
This is the code that I have, but it is not giving me what I need
select UserId,
max(case when Aciton = 'O' then 1 else 0 end) as [O(Open)],
max(case when Aciton = 'P' then 1 else 0 end) as [P(Paid)],
max(case when Aciton = 'C' then 1 else 0 end) as [C(Cash)],
[Date],
Tracking,
Zip
from myTable
GROUP BY UserId, [Date], [Tracking], [Zip]
ORDER BY UserId