sql - report records are in multiple systems
问题描述
Working in SQL I have a dataset of source systems and the ID of people who have access, simplified it looks like this:
System | ID |
---|---|
Sys1 | A |
Sys2 | A |
Sys1 | B |
sys2 | C |
What I want to get out is something like this:
ID | Sys1 | Sys2 |
---|---|---|
A | 1 | 1 |
B | 1 | 0 |
C | 0 | 1 |
I am sure someone has a solution to this here already, but my searching didn't turn up anything.
解决方案
if it is limited to handful of system values you can use this query:
select
ID
, MAX(case when system = 'Sys1' then 1 else 0 end) Sys1
, MAX(case when system = 'Sys2' then 1 else 0 end) Sys2
, MAX(case when system = 'Sys3' then 1 else 0 end) Sys3
, MAX(case when system = 'Sys4' then 1 else 0 end) Sys4
, ....
from table
GROUP BY ID
推荐阅读
- c# - 使用单击一次部署安装程序包
- java - 如何使我的这部分代码在 java 中可扩展
- node.js - 如何在单个 digitalocean vps 服务器中服务多个节点应用程序?
- java - 在 windows 中,如何在不提及任何特定端口的情况下启动 appium 服务器并利用可用的空闲端口?
- angular - Angular 6中嵌套FormArray中的patchValue
- java - 使用java字节码编辑器追加行后缺少'while'语句
- java - Java:我无法在 while 循环中打印到文本文件,尽管能够在 while 循环之前这样做
- r - 带有负值的条形图上的水平标准误差条
- pytorch - 使用索引张量选择张量的第二个暗淡
- mongodb - 无法序列化类 org.springframework.data.mongodb.core.geo.GeoJsonPoint