首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于TypeID创建列

基于TypeID创建列
EN

Stack Overflow用户
提问于 2019-06-19 21:53:41
回答 1查看 61关注 0票数 1

这可能会让人困惑,但我很确定你们中的一个人可以帮我解决这个问题。所以基本上,我有一个包含以下内容的主表:

代码语言:javascript
复制
ObjID, InfoID, InfoVal, (other columns not needed).

When InfoID = 1, the InfoVal for that row represents the Report name. 
When InfoID = 4, the InfoVal for that row represents the Stored Procedures Name.
When InfoID = 16,17,23 or 24, the InfoVal for that row represents the Email for that Report.

我的问题是,如何准确地将每个条件转换为列并将其连接在一起?到目前为止,我有多个单独的查询,其中InfoID = ??我想把每一列连接在一起。

本质上,我想要一个用于报告名称、存储过程名称的列和一个用于电子邮件的列。

如果你需要图片可视化,请让我知道!

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-19 21:56:27

您似乎想要条件聚合:

代码语言:javascript
复制
select objid,
       max(case when infoid = 1 then infoval end) as report_name,
       max(case when infoid = 4 then infoval end) as stored_procedure_name,
       max(case when infoid = 16 then infoval end) as email1,
       max(case when infoid = 17 then infoval end) as email2,
       max(case when infoid = 23 then infoval end) as email3,
       max(case when infoid = 24 then infoval end) as email4
from t
group by objid;

要过滤:

代码语言:javascript
复制
select t.*
from (select objid,
             max(case when infoid = 1 then infoval end) as report_name,
             max(case when infoid = 4 then infoval end) as stored_procedure_name,
             max(case when infoid = 16 then infoval end) as email1,
             max(case when infoid = 17 then infoval end) as email2,
             max(case when infoid = 23 then infoval end) as email3,
             max(case when infoid = 24 then infoval end) as email4
      from t
      group by objid
    ) t
where email1 like . . .
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56669377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档