选择SQL查询的一部分中的两个子查询

时间:2016-06-16 20:30:23

标签: sql

我被要求在sql语句中创建两个新列我有基本的语句,但每当我尝试将它们放在一个语句中时我得到一个错误我试过让它们成为UNION语句的一部分但仍然得到错误。关于我在做错误的SQL语句下面的任何提示

    select 
wdate, 
sum(pl_meter1) as plan_1b,
sum(re_meter1) as result_1b, 
sum(pl_meter2) as plan_2b, 
sum(re_meter2) as result_2b, 
(Select ITDSC1 FROM AGGW.gwd1110 where  SUBSTR(ITDSC1,18,1) = 'G') AS PLANG,
 (Select ITDSC1 FROM AGGW.gwd1110 where SUBSTR(ITDSC1,18,1) != 'G') AS PLANNG  
from   aggw.gwd1110  
where   wdate >= 20160529 
group by wdate 
order by wdate;

1 个答案:

答案 0 :(得分:1)

尝试这种方式:

select wdate, 
       sum(pl_meter1) as plan_1b,
       sum(re_meter1) as result_1b, 
       sum(pl_meter2) as plan_2b, 
       sum(re_meter2) as result_2b, 
       max(case when SUBSTR(ITDSC1,18,1) = 'G' then ITDSC1 end) AS PLANG,
       max(case when SUBSTR(ITDSC1,18,1) != 'G' then ITDSC1 end) AS PLANNG  
from aggw.gwd1110  
where wdate >= 20160529 
group by wdate 
order by wdate;