因为牵涉的表连接太多了,就只用的临时表作为查询主表
临时表t有product_id,year,product_name,plan_name,batch_name,first_login_time,
is_graduated,is_degree,product_type_code这几个字段,我要根据不同条件来统计不同的人数,
我的统计sql如下:
select PRODUCT_ID,YEAR,PRODUCT_NAME,PLAN_NAME,BATCH_NAME,nvl(count(*),'0') APPLY_NUM,'高等学历提升' as PRODUCT_CATEGORY_NAME,PRODUCT_TYPE_CODE
,nvl((
select count(1) from t where t.FIRST_LOGIN_DATE is not null group by YEAR,PRODUCT_ID having t.YEAR=YEAR and t.PRODUCT_ID=PRODUCT_ID
),'0') as ATTEND_NUM
,nvl((
select count(1) from t where t.IS_GRADUATED='是' group by YEAR,PRODUCT_ID having t.YEAR=YEAR and t.PRODUCT_ID=PRODUCT_ID
),'0') as GRADUATE_NUM
,nvl((
select count(1) from t where t.IS_DEGREE='是' group by YEAR,PRODUCT_ID having t.YEAR=YEAR and t.PRODUCT_ID=PRODUCT_ID
),'0') as DEGREE_NUM
from t 
group by PRODUCT_ID,YEAR,PRODUCT_NAME,PLAN_NAME,BATCH_NAME,PRODUCT_TYPE_CODE
order by YEAR desc
其中YEAR和PRODUCT_ID为查询出来的值再传入统计字段过滤,现在问题是YEAR和PRODUCT_ID无法传入,这样就一直报一对多的错误,求正确的sql的写法。OracleSQL

解决方案 »

  1.   

    你子查询里面用到having那结果应该是多条数据,  列的子查询只能有一行一列的数据。  你看下是不是这个问题!
      

  2.   


    我这样写就可以传入值,但是红色部分不那么写就无法传入了,现在都还没弄懂什么原因
    select xl.PLAN_NAME,xl.BATCH_NAME,xl.PRODUCT_NAME,nvl(count(1),'0')  APPLY_NUM,xl.YEAR,xl.PRODUCT_ID
    ,nvl((
    select count(1) from xl t where t.FIRST_LOGIN_DATE is not null and t.year = xl.year and t.product_id=xl.product_id  group by YEAR,PRODUCT_ID
    ),'0') as ATTEND_NUM
    ,nvl((
    select count(1) from xl t where t.IS_GRADUATED='是' and t.year = xl.year and t.product_id=xl.product_id group by YEAR,PRODUCT_ID
    ),'0') as GRADUATE_NUM
    ,nvl((
    select count(1) from xl t where t.IS_DEGREE='是' and t.year = xl.year and t.product_id=xl.product_id group by YEAR,PRODUCT_ID
    ),'0') as DEGREE_NUM
    from xl
    group by xl.PLAN_NAME,xl.BATCH_NAME,xl.PRODUCT_NAME,xl.YEAR,xl.PRODUCT_ID
    order by xl.year desc