数据库学德很差,我现在有一个问题
select d.ORG_NAME, count(b1),count(b2),count(b3),WTZG_BFHXZ
  from HSE_JC_WTZG, SY_ORGANIZATION d
 where d.ORG_CODE = substr(WTZG_WTSSDW_CODE, 0, 5)
   and WTZG_BFHXZ ='01' b1 and WTZG_BFHXZ <> '02' b2 and WTZG_BFHXZ <> '03'b3 
 group by d.ORG_NAME, WTZG_BFHXZ
union
select d.ORG_NAME,count(b1),count(b2),count(b3),WTZG_BFHXZ
  from HSE_JC_WTZG, SY_ORGANIZATION d
 where d.ORG_CODE = substr(WTZG_WTSSDW_CODE, 0, 5)
   and WTZG_BFHXZ <>'01' b1 and WTZG_BFHXZ ='02' b2 and WTZG_BFHXZ <> '03'b3 
 group by d.ORG_NAME, WTZG_BFHXZ
union
select d.ORG_NAME,count(b1),count(b2),count(b3), WTZG_BFHXZ
  from HSE_JC_WTZG, SY_ORGANIZATION d
 where d.ORG_CODE = substr(WTZG_WTSSDW_CODE, 0, 5)
   and WTZG_BFHXZ <>'01' b1 and WTZG_BFHXZ <> '02' b2 and WTZG_BFHXZ = '03'b3 
 group by d.ORG_NAME, WTZG_BFHXZ
各位看下错在哪?(count(b1),count(b2),count(b3)必须得显示)

解决方案 »

  1.   

    --照你的意思写的 具体在说
    select d.ORG_NAME,WTZG_BFHXZ,
    count(case when WTZG_BFHXZ ='01' then 1 end) b1,
    count(case when WTZG_BFHXZ <> '02' then 1 end) b2,
    count(case when WTZG_BFHXZ <> '03' then 1 end) b2,
      from HSE_JC_WTZG, SY_ORGANIZATION d
     where d.ORG_CODE = substr(WTZG_WTSSDW_CODE, 0, 5)
    group by by d.ORG_NAME, WTZG_BFHXZ
    union
    select d.ORG_NAME,WTZG_BFHXZ,
    count(case when WTZG_BFHXZ <>'01' then 1 end) b1,
    count(case when WTZG_BFHXZ ='02' then 1 end) b2,
    count(case when WTZG_BFHXZ <>'03' then 1 end) b2,
      from HSE_JC_WTZG, SY_ORGANIZATION d
     where d.ORG_CODE = substr(WTZG_WTSSDW_CODE, 0, 5)
    group by by d.ORG_NAME, WTZG_BFHXZ
    union
    select d.ORG_NAME,WTZG_BFHXZ,
    count(case when WTZG_BFHXZ <>'01' then 1 end) b1,
    count(case when WTZG_BFHXZ <>'02' then 1 end) b2,
    count(case when WTZG_BFHXZ ='03' then 1 end) b2,
      from HSE_JC_WTZG, SY_ORGANIZATION d
     where d.ORG_CODE = substr(WTZG_WTSSDW_CODE, 0, 5)
    group by by d.ORG_NAME, WTZG_BFHXZ
      

  2.   

    where 条件取别名做什么呢? 这个是不符合语法的
    还有如果你的各个查询结果不存在重复数据的话 可以使用union all 这样速度快的