使用 SQL 从 3 个表创建数据透视表

发布于 2025-01-10 19:46:15 字数 3126 浏览 1 评论 0原文

我正在尝试连接三个表,然后创建一个数据透视表。我正在努力找出如何做到这一点。

下面是三个现有表和所需输出表的格式示例。

我在 Tableau 中工作,数据是从 Microsoft SQL 服务器输入的。

联系人表(表 1)

联系人 ID姓名渠道联系类型第 1 阶段第 2 阶段
12098AndrewGoogle客户4459344597
34556JoeFacebook客户4456844576
32344PaulIndeed买家
32211SimonIndeed客户4456244570
21212PeterFacebook客户4459944601
29871SamGoogle客户4461144​​613

联系人/交易关联(表 2)

交易 ID联系人 ID
54312098
23234556
45632211
78721212
65429871

交易历史记录(表 3)

交易ID阶段3阶段 4阶段 5
5434459944601
232445824459344599
45644580
787446104461244615
65444615

数据透视表(输出表)

联系人 ID数据透视名称数据透视值
12098阶段144593
12098第 2 级44597
12098第 3 级44599
12098第 4 级44601
34556第 1 级44568
34556第 2 级44576
34556第 3 级44582
34556第 4 级44593
34556第 5 级44599
32211第 1 级44562
32211第 2 级44570
32211第 3 级44580
21212第 1 级44599
21212第 2 级44601
21212第 3 级44610
21212第 4 级44612
21212第 5 阶段44615
29871第 1 阶段44611
29871第 2 阶段44613
29871第 3 阶段44615

任何建议将不胜感激。

谢谢。

I am trying to join three tables to then create a pivot table. I'm struggling to find figure out how to do this.

Below is an example of the format of the three existing tables and the desired out output table.

I am working in Tableau and the data is feeding in from a Microsoft SQL server.

Contact Table (Table 1)

Contact IDNameChannelContact TypeStage 1Stage 2
12098AndrewGoogleClient4459344597
34556JoeFacebookClient4456844576
32344PaulIndeedBuyer
32211SimonIndeedClient4456244570
21212PeterFacebookClient4459944601
29871SamGoogleClient4461144613

Contact/Deal Association (Table 2)

Deal IDContact ID
54312098
23234556
45632211
78721212
65429871

Deal History (Table 3)

Deal IDStage 3Stage 4Stage 5
5434459944601
232445824459344599
45644580
787446104461244615
65444615

Pivot Table (Output Table)

Contact IDPivot NamePivot Value
12098Stage 144593
12098Stage 244597
12098Stage 344599
12098Stage 444601
34556Stage 144568
34556Stage 244576
34556Stage 344582
34556Stage 444593
34556Stage 544599
32211Stage 144562
32211Stage 244570
32211Stage 344580
21212Stage 144599
21212Stage 244601
21212Stage 344610
21212Stage 444612
21212Stage 544615
29871Stage 144611
29871Stage 244613
29871Stage 344615

Any advise would be greatly appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2025-01-17 19:46:15

也许不是最好的解决方案,但这可能对您有用:

select ContactID,
       PivotName,
       PivotValue                 
FROM (
       select ContactID,max(Stage1) as Stage1,max(Stage2) as Stage2,max(Stage3) as Stage3,max(Stage4) as Stage4,max(Stage5) as Stage5
          from (
                  select cd.ContactID,c.Stage1,null as Stage2,null as Stage3,null as Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,null as Stage1,c.Stage2,null as Stage3,null as Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,null as Stage1,null as Stage2,dh.Stage3,null as Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,null as Stage1,null as Stage2,null as Stage3,dh.Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,c.Stage1,null as Stage2,null as Stage3,null as Stage4,dh.Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID 
                 )  t1 group by ContactID 
      ) t2 
unpivot
(
  PivotValue
  for PivotName in (Stage1,Stage2,Stage3,Stage4,Stage5)
) unpiv;

结果:

ContactID PivotName PivotValue
  12098 阶段1 44593
  12098 阶段2 44597
  12098 第三阶段 44599
  12098 第四阶段 44601
  21212 第一阶段 44599
  21212 阶段2 44601
  21212 第三级 44610
  21212 第四级 44612
  21212 第五级 44615
  29871 阶段1 44611
  29871 阶段2 44613
  29871 第三阶段 44615
  32211 第一阶段 44562
  32211 阶段2 44570
  32211 第三级 44580
  34556 阶段 1 44568
  34556 阶段2 44576
  34556 第三级 44582
  34556 第四级 44593
  34556 阶段 5 44599

演示

Maybe not the best solution , but this might work for you:

select ContactID,
       PivotName,
       PivotValue                 
FROM (
       select ContactID,max(Stage1) as Stage1,max(Stage2) as Stage2,max(Stage3) as Stage3,max(Stage4) as Stage4,max(Stage5) as Stage5
          from (
                  select cd.ContactID,c.Stage1,null as Stage2,null as Stage3,null as Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,null as Stage1,c.Stage2,null as Stage3,null as Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,null as Stage1,null as Stage2,dh.Stage3,null as Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,null as Stage1,null as Stage2,null as Stage3,dh.Stage4,null as Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID
                  union all
                  select cd.ContactID,c.Stage1,null as Stage2,null as Stage3,null as Stage4,dh.Stage5
                  from  Contact_deal cd inner join DealHistory dh on dh.DealID=cd.DealID inner join Contact c on c.ContactID=cd.ContactID 
                 )  t1 group by ContactID 
      ) t2 
unpivot
(
  PivotValue
  for PivotName in (Stage1,Stage2,Stage3,Stage4,Stage5)
) unpiv;

Result:

ContactID  PivotName  PivotValue
  12098     Stage1     44593
  12098     Stage2     44597
  12098     Stage3     44599
  12098     Stage4     44601
  21212     Stage1     44599
  21212     Stage2     44601
  21212     Stage3     44610
  21212     Stage4     44612
  21212     Stage5     44615
  29871     Stage1     44611
  29871     Stage2     44613
  29871     Stage3     44615
  32211     Stage1     44562
  32211     Stage2     44570
  32211     Stage3     44580
  34556     Stage1     44568
  34556     Stage2     44576
  34556     Stage3     44582
  34556     Stage4     44593
  34556     Stage5     44599

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文