最近在学SSIS遇到的问题,在源数据中有 表Batch(batch_id,等列) , Error(batch_id,error_id等列) 
在数据仓库事实表的加载过程中,我想把凡是在Error表中能找到batch_id,也就是说有错误的batch,在事实表中加一个Error_flag(数据类型为bit) 列标记,batch有错误就是Error_flag = 1, 否则为0。
 运行的结果希望如下
batch_id, Error_flag
1 , 1
2 , 0
3 , 0
4 , 1

解决方案 »

  1.   

    select batch_id , Error_flag = 1 from Batch where batch_id in (select batch_id from Error)
    union all
    select batch_id , Error_flag = 1 from Batch where batch_id not in (select batch_id from Error)
      

  2.   

    --上面错了点.
    select batch_id , Error_flag = 1 from Batch where batch_id in (select batch_id from Error)
    union all
    select batch_id , Error_flag = 0 from Batch where batch_id not in (select batch_id from Error)
      

  3.   

    如果在上面的源数据中加一个Map(map_id,等列)表,然后还是上面的两个表Batch(batch_id,map_id 等列) , Error(batch_id,error_id等列)  
    也就是说这回只要Map中 只要有一个Batch有错误 我就说 Error_flag = 1 (因为一个map表下可能有多个 batch情况,只有全部batch没错误,我们才认为它成功 Error_flag=0)。
    运行的结果希望如下
    Map_id, Error_flag 
    1 , 1 
    2 , 0 
    3 , 0 
    4 , 1
      

  4.   

    如果在上面的源数据中加一个Map(map_id,等列)表,然后还是上面的两个表Batch(batch_id,map_id 等列) , Error(batch_id,error_id等列)   
    也就是说这回只要Map中 只要有一个Batch有错误 我就说 Error_flag = 1 (因为一个map表下可能有多个 batch情况,只有全部batch没错误,我们才认为它成功 Error_flag=0)。 
    运行的结果希望如下 
    Map_id, Error_flag  
    1 , 1  
    2 , 0  
    3 , 0  
    4 , 1 
    ---
    是用map_id吧?select distinct map_id , Error_flag = 0 from batch where map_id not in
    (select distinct a.map_id from batch a, map b where a.map_id = b.map_id)
    union all
    select distinct a.map_id , Error_flag = 1 from batch a, map b where a.map_id = b.map_id
      

  5.   

    是用map_id吧? 
    select distinct map_id , Error_flag = 0 from batch where map_id not in (select map_id from map)
    union all
    select distinct map_id , Error_flag = 1 from batch where map_id in (select map_id from map)
      

  6.   

    严格上来说我并不是想得到楼上的那个结果
    因为我的数据结构比较复杂,又是新学,所以表达可能不是太清楚
    我想得到的结果是基于map_id,  source_object  统计error_flag, 因为map_id,  source_object 同时相等的情况下可能对应一个或多个batch_id, 这样只要有一个batch_id有error就说这个map_id,  source_object   后的error_flag =1,  否则为0  为了好理解 我创建了一个Viewmap_id=1000, source_object= D20070425T041616  有三个Batch_id 也就是说,只要有一个error_flag 为1 那这个map_id,source_object后面的error_flag=1
      

  7.   

    如果在上面的源数据中加一个Map(map_id,等列)表,然后还是上面的两个表Batch(batch_id,map_id,  source_object 等列) , Error(batch_id,error_id等列)   --update b
    set error_flag = 1
    from batch b
    left join (select map_id,source_object from error e left join batch a on a.batch_id = a.batch_id) c on b.map_id = c.map_id and b.source_object =c.source_object select * from batch