这可能会造成混淆,但是我敢肯定你们中的一个可以帮助我解决这个问题。所以基本上,我的主表包含以下内容:
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 = WHERE条件并且我想将每个专栏连在一起。
基本上,我想要一列用于报告名称,存储过程名称和一列用于电子邮件。
如果您需要可视化的图片,请告诉我!
谢谢。
答案 0 :(得分:3)
您似乎想要条件聚合:
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;
要过滤:
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 . . .