我的union all sql如下:
select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7')
union all 
select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7')
union all 
select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('a')
结果如下:
1
1
0
想要执行对上面的结果进行sum,也就是对1,1,0 进行sum,按照下面的语句执行会出现错误:
select sum(a) from (
select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7')
union all 
select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7')
union all 
select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('a')
)
错误如下:
消息 102,级别 15,状态 1,第 7 行
')' 附近有语法错误。我的环境是SqlServer2005, 请问可以在union all 之后进行sum么? 不行的话,谁能指教一个合适的方法。谢谢

解决方案 »

  1.   

    select sum(a) from ( 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7') 
    union all 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7') 
    union all 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('a') 
    as t
      

  2.   

    select sum(a) from (
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7')
    union all
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7')
    union all
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('a')
    )  tmp
      

  3.   

    select sum(a) from ( 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7') 
    union all 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7') 
    union all 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('a') 
    AS T
      

  4.   

    少了表别名.SQL下是编译不通过的.
      

  5.   

    ---SQL语法 差个别名
    select sum(a) from ( 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7') 
    union all 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('N','1','2','3','4','5','6','7') 
    union all 
    select case isnull(sum(1),0) when 0 then 0 else 1 end as a from card_info where app_id=370 and card_type='贷记卡' and pay_in_last_12m in ('a') 
    ) t