原数据是
id     name     parent_id    quesnum
1       aa        null         null
2       bb          1           0
3       cc          1           119
4       dd         null        null
5       ee          4           0
6       ff          5           30
7       gg          5           40
8       qq          5           null要得到以下结果
id     name     parent_id    quesnum
1       aa        null         null
3       cc          1           119
4       dd         null        null
5       ee          4           0
6       ff          5           30
7       gg          5           40
就是如果是两层的,第二层不要。三层的,第二层的只要第三有数据不管有没有数据都要,第三层没有数据不要

解决方案 »

  1.   

    select * from tablename a
    where exists (
    select 1 from tablename
    where parent_id=a.id
    )
    or isnull(quesnum,0)<>0
      

  2.   

    楼主表述太不清楚其实一句话:过滤掉quesnum=0或者空的叶子节点
      

  3.   

    过滤掉quesnum=0或者空的叶子节点,另外如果第二个节点(即不是是叶子节点),为0 的话则改为空
      

  4.   

    select a.id,a.qtablename,a.wk_code,a.wk_desc,a.wk_level,a.wk_name,wk_national,a.parent_id,a.ppath,a.wk_sort,a.wk_special,a.wk_statue,
    quesnum=(case when a.quesnum=0 then null else a.quesnum end ),b.state from  (select a.* from user_worktype a
    where  exists (select 1 from user_worktype where parent_id=a.id) 
    or isnull(quesnum,0)<>0 ) a ,user_worktype_quespower b
    where  a.id = b.worktype_id