应该是你的sql书写有问题,我试了一下下面的语句,可以正常执行:
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual
union
select sysdate from dual;

解决方案 »

  1.   

    select '北京'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='11'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='11'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='11'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='11'),0)a4
    from dual
    union
    select '吉林'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='22'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='22'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='22'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='22'),0)a4
    from dual
    union
    select '新疆'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='65'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='65'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='65'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='65'),0)a4
    from dual
    union
    select '宁夏'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='64'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='64'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='64'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='64'),0)a4
    from dual
    union
    select '天津'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='12'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='12'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='12'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='12'),0)a4
    from dual
    union
    select '河北'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='13'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='13'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='13'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='13'),0)a4
    from dual
    union
    select '山西'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='14'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='14'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='14'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='14'),0)a4
    from dual
    union
    select '内蒙'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='15'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='15'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='15'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='15'),0)a4
    from dual
    union
    select '辽宁'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='21'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='21'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='21'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='21'),0)a4
    from dual
    union
    select '黑龙江'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='23'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='23'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='23'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='23'),0)a4
    from dual
    union
    select '上海'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='31'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='31'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='31'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='31'),0)a4
    from dual
    union
    select '江苏'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='22'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='22'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='22'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='22'),0)a4
    from dual
    union
    select '浙江'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='33'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='33'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='33'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='33'),0)a4
    from dual
    union
    select '安徽'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='34'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='34'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='34'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='34'),0)a4
    from dual
    union
    select '福建'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='35'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='35'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='35'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='35'),0)a4
    from dual
    union
    select '江西'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='36'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='36'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='36'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='36'),0)a4
    from dual
    union
    select '山东'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='37'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='37'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='37'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='37'),0)a4
    from dual
    union
    select '河南'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='41'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='41'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='41'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='41'),0)a4
    from dual
    union
    select '湖北'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='42'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='42'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='42'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='42'),0)a4
    from dual这样的语句超过20个UNION就出错了。
      

  2.   

    不是写法问题,可能是sql语句过长了
      

  3.   

    SQL语句长度是有限制的。
    看看你的SQL语句是不是可以通过某种方式优化一下,减少字符
      

  4.   

    我想应该是语句写得太多了,可以再简化一下。Oracle就算有限制也不会就这点儿吧……
      

  5.   

    --感觉楼主写的语句有点恐怖
    其实可以只查询一次的,
    select '辽宁'zd,
    nvl((select count(0) from V_ageCount where nl<=14 and sf='21'),0)a1,
    nvl((select count(0) from V_ageCount where nl<=16 and nl >14 and sf='21'),0)a2,
    nvl((select count(0) from V_ageCount where nl<18 and nl >16 and sf='21'),0)a3,
    nvl((select count(0) from V_ageCount where nl>=18 and sf='21'),0)a4
    from dual--可以改成下面的
    select '辽宁'zd,
    sum(case when nl<=14 and sf='21' else 0 end) a1,
    sum(case when nl<=16 and nl >14 else 0 end) a2,
    sum(case when nl<18 and nl >16 else 0 end) a3,
    sum(case when nl>=18 and sf='21' else 0 end) a4
    from V_ageCount
      

  6.   

    改一下,
    select '辽宁'zd,
    sum(case when nl<=14 and sf='21' else 0 end) a1,
    sum(case when nl<=16 and nl >14 else 0 end) a2,
    sum(case when nl<18 and nl >16 else 0 end) a3,
    sum(case when nl>=18 and sf='21' else 0 end) a4
    from V_ageCount
    group by 分组字段
      

  7.   

    再改一下改一下,
    select '辽宁'zd,
    sum(case when nl<=14 and sf='21'then 1 else 0 end) a1,
    sum(case when nl<=16 and nl >14 then 1 else 0 end) a2,
    sum(case when nl<18 and nl >16 then 1 else 0 end) a3,
    sum(case when nl>=18 and sf='21' then 1 else 0 end) a4
    from V_ageCount
    group by 分组字段