如何合并具有不同条件的相同表

时间:2016-06-06 18:30:08

标签: sql union teradata union-all

我需要编写一个查询来生成一个结果,以便在其他OLD_ACCOUNT_ID可用时显示NULL,并在同一个表格中显示NEW_ACCOUNT_IDNULL基于OLD条件。我尝试了如下所示的查询,但这不会在NEWSELECT DISTINCT A.ACCOUNT_ID as NEW_ACCOUNT_ID, A.ACTIVE_FLAG,A.DATA_ID FROM Table A WHERE DATA_SOURCE_PROVIDER_ID='X' AND ACTIVE_FLAG='Y' and DATA_ID= '12345678' union SELECT DISTINCT B.ACCOUNT_ID AS OLD_ACCOUNT_ID, B.ACTIVE_FLAG,B.DATA_ID FROM Table B WHERE DATA_SOURCE_PROVIDER_ID='X' AND ACTIVE_FLAG='N' and DATA_ID= '12345678' 的单独列中生成数据。有人可以帮忙吗。

map.values().removeAll(Collections.singleton(null));

1 个答案:

答案 0 :(得分:1)

用例

select DISTINCT  
  case ACTIVE_FLAG when 'Y'  then ACCOUNT_ID end as NEW_ACCOUNT_ID, 
  case ACTIVE_FLAG when 'N'  then ACCOUNT_ID end as OLD_ACCOUNT_ID, 
  ACTIVE_FLAG,D
  DATA_ID 
from Table
whene DATA_ID= '12345678';