基于列值中的一个条件选择SQL

时间:2018-02-23 18:41:12

标签: sql sql-server

有人可以使用以下数据帮助我查询SQL查询吗?enter image description here

每当我看到类型为停用而不管同一ID中的其他类型时,我必须只获取停用值,其余值应为空。

select     id,     
pid,    
type,    
case when type='deactivate' then null as value1 else value1 end,     
case when type='deactivate' then  null as value2 else value2 end,     
case when type='deactivate' then  null as value3 else value3 end,     
case when type='deactivate' then null as value4 else value4 end,     
case when type='deactivate' then null as value5 else value5 end,     
case when type='deactivate' then value6 end,     
case when type='deactivate' then deactivate_date end,     
case when type='deactivate' then status end     
from typetable  

但是每当pid发生变化时,我都会在value1到5中获得值。 每当我看到type ='deactivate'时,我的数据中是否有任何方法我必须忽略同一ID中所有类型的值1到5的值? 输出应该类似于ID-1和&的空值。 3如下所述。

id  pid type    value1  value2  value3  value4  value5  value6  deactive date   Status
0   1   case    99  null    null    null    null    null    null    
0   2   test    null    101 null    null    null    null    null    
0   3   levels  null    null    CAAD    null    null    null    null    
0   4   package null    null    null    null    DSIT    null    null    

1   1   case    null    null    null    null    null    null    null    
1   2   test    null    null    null    null    null    null    null    
1   3   levels  null    null    null    null    null    null    null    
1   4   package null    null    null    null    null    null    null    
1   5   deactivate  null    null    null    null    null    9999999 9/1/2014    Void ID

2   1   case    CLR 6#  null    null    null    null    null    null    
2   2   test    null    6   null    null    null    null    null    
2   3   levels  null    null    MAA null    null    null    null    

3   1   case    null    null    null    null    null    null    null    
3   2   test    null    null    null    null    null    null    null    
3   3   levels  null    null    null    null    null    null    null    
3   4   package null    null    null    null    null    null    null    
3   5   deactivate  null    null    null    null    null    9999999 1/7/2016    Closed

3 个答案:

答案 0 :(得分:1)

declare @t table (id int, pid int, col1 varchar(10), col2 varchar(10)) 
insert into @t 
       values (1, 1, 'askjdf', 'laskjdf')
            , (1, 2, 'asksfjdf', 'ljdf')
            , (2, 1, 'askjdf', 'laskjdf')
            , (2, 2, 'deact', 'laskjdf');

select t.* 
from @t t 
where not exists (select 1 from @t tm where tm.id = t.id and tm.col1 = 'deact') 
union all 
select t.id, t.pid, t.col1, null  
from @t t 
where     exists (select 1 from @t tm where tm.id = t.id and tm.col1 = 'deact') 
order by id, pid;

答案 1 :(得分:0)

这样的事情会起作用:

public void onApplicationEvent(ContextRefreshedEvent event) {
        Environment env = ApplicationContextProvider.getApplicationContext().getBean(Environment.class);

        if(env.getActiveProfiles() != null) {
            if(env.getActiveProfiles().length == 1 && Arrays.binarySearch(env.getActiveProfiles(), "master") == -1 ) {
                initialize();
            }
        }

    }

类似,可能更好:

let theDelegate = MyDelegate()
delegate = theDelegate

答案 2 :(得分:0)

如果你在id AND type='deactivate'上进行自我左连接并且检查连接表的类型字段而不是左表的类型字段,那么最干净,最简单:

select t1.id,t1.pid,t1.type,
case when t2.type='deactivate' then null else t1.value1 end AS value1,
case when t2.type='deactivate' then null else t1.value2 end AS value2, 
case when t2.type='deactivate' then null else t1.value3 end AS value3, 
case when t2.type='deactivate' then null else t1.value4 end AS value4, 
case when t2.type='deactivate' then null else t1.value5 end AS value5, 
case when t2.type='deactivate' then t1.value6 end AS value6, 
case when t2.type='deactivate' then t1.deactivate_date end AS deactivate_date, 
case when t2.type='deactivate' then t1.status end from typetable t1 
LEFT JOIN typetable t2 ON t1.id=t2.id AND t2.type='deactivate'