sql - 基于变量有条件地检索数据
问题描述
我有以下数据集:
have
:
data have;
infile datalines delimiter=",";
input id $ nbtimes $ typeof $ dp $;
datalines4;
ABC,1,GROUP,A
ABC,1,GROUP,B
ABC,2,COMPANY,A
ABC,2,COMPANY,B
DEF,1,COMPANY,A
DEF,1,COMPANY,B
;;;;
run;
如果 ID 报告“Group”和“Company”,我想输出该实体的数据typeof= "Group"
;如果 ID 仅报告“公司”,我只想检索具有typeof= "Company"
.
我希望能够通过proc sql
want
:
ID | nbtimes | 类型 | 类型 |
---|---|---|---|
美国广播公司 | 1 | 团体 | 一个 |
美国广播公司 | 1 | 团体 | 乙 |
国防军 | 1 | 公司 | 一个 |
国防军 | 1 | 公司 | 乙 |
解决方案
使用 SQL 创建满足 typeof 的数据集,然后将其与原始数据集合并。根据您提供的条件,ABC 有两种不同的 typeof,包括 'GROUP',DEF 有一种,所以这里是程序:
proc sql;
select a.* from have a right join (
select distinct ID,case when count(distinct typeof)>1 then 'GROUP'
else 'COMPANY' end as typeof
from have group by ID
) b
on a.ID=b.ID and a.typeof=b.typeof;
quit;
推荐阅读
- android - Android anko警报不显示项目
- spring-boot - 如何在 Spring Boot tar 文件中包含 pkcs12 二进制文件
- azure - 如何在 Azure 存储 Blob 中使用占位符?
- php - 此集合实例上不存在属性 [students]
- java - 底部导航视图中的菜单项 TITLE VISIBILITY
- d3.js - json 中的 D3.js attr 不起作用?
- javascript - 如何在不使用输入的情况下从变量中获取值然后复制到剪贴板?
- javascript - 不能在移动设备上工作
- ios - OCMock3 与 IOS 的集成问题
- ios - 是否可以将 swift 与任何其他语言内联?