select b.typename,b.shortname,subcount=count(1) from
tab a
inner join 
(
  select typename,shortname,pid from tab where pid=0
) b 
on a.id=b.pid
group by b.typename,b.shortname,b.pid

解决方案 »

  1.   

    select b.typename,b.shortname,subcount=count(1) from 
    tab a 
    inner join 

      select id,typename,shortname from tab where pid=0 
    ) b 
    on a.pid=b.id 
    group by b.typename,b.shortname,b.id
      

  2.   

    试试
    select a.Typename,a.ShortName,(select count(id) form 表 where pid=a.id) form 表 a where PID=0
      

  3.   

    更正
    select a.Typename,a.ShortName,(select count(id) form 表 where pid=a.id)as subcount form 表 a where a.PID=0
      

  4.   

    if not object_id('MOVIE') is null
    drop table MOVIE
    Go
    Create table MOVIE([ID] int,[PID] int,[Typename] nvarchar(5),[ShortName] nvarchar(2))
    Insert MOVIE
    select 1,0,N'动漫',N'动漫' union all
    select 3,0,N'综艺',N'综艺' union all
    select 4,3,N'行规划',N'达到' union all
    select 5,0,N'剧集',N'剧集' union all
    select 6,1,N'覆盖特瑞特',N'达到' union all
    select 7,0,N'电影',N'电影' union all
    select 8,7,N'动作',N'动作' union all
    select 9,7,N'爱情',N'爱情' union all
    select 10,5,N'国产',N'国产' union all
    select 11,5,N'港台',N'港台' union all
    select 13,7,N'战争',N'战争'
    Go
    Select * from MOVIESELECT A.TYPENAME,A.SHORTNAME,SUBCOUNT=COUNT(1)  
    FROM MOVIE A
    INNER JOIN MOVIE B
    ON A.ID=B.PID
    WHERE  A.PID=0
    GROUP BY A.TYPENAME,A.SHORTNAME
    ORDER BY SUBCOUNT DESC/*
    TYPENAME SHORTNAME SUBCOUNT    
    -------- --------- ----------- 
    电影       电影        3
    剧集       剧集        2
    综艺       综艺        1
    动漫       动漫        1(所影响的行数为 4 行)*/
      

  5.   

    create table #C
    (
     ID int,
     PID int,
     Typename nvarchar(20),
     ShortName nvarchar(20) 
    )
    insert into #C
    select 1, 0, '动漫', '动漫' union all 
    select 3, 0, '综艺', '综艺' union all
    select 4, 3, '行规划', '达到' union all
    select 5, 0, '剧集', '剧集' union all
    select 6, 1, '覆盖特瑞特', '达到' union all
    select 7, 0, '电影', '电影' union all
    select 8, 7, '动作', '动作' union all
    select 9, 7, '爱情', '爱情' union all
    select 10, 5, '国产', '国产' union all
    select 11, 5, '港台', '港台' union all
    select 13, 7, '战争', '战争'
    select Typename,ShortName,cnt from #C A,
    (select PID,COUNT(1) cnt from #C group by PID) B
    where A.ID=B.PID and A.PID=0 
      

  6.   

    select b.typename,b.shortname,count(1) subcount from 
    table_A a 
    inner join 

      select typename,shortname,pid from table_A where pid=0 
    ) b 
    on a.id=b.pid 
    group by b.typename,b.shortname,b.pid
      

  7.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[PID] int,[Typename] varchar(10),[ShortName] varchar(4))
    insert [tb]
    select 1,0,'动漫','动漫' union all
    select 3,0,'综艺','综艺' union all
    select 4,3,'行规划','达到' union all
    select 5,0,'剧集','剧集' union all
    select 6,1,'覆盖特瑞特','达到' union all
    select 7,0,'电影','电影' union all
    select 8,7,'动作','动作' union all
    select 9,7,'爱情','爱情' union all
    select 10,5,'国产','国产' union all
    select 11,5,'港台','港台' union all
    select 13,7,'战争','战争'
     
    ---查询---
    select 
      Typename,
      ShortName,
      subcount=(select count(1) from tb where pid=t.id)
    from [tb] t
    where pid=0
    ---结果---
    Typename   ShortName subcount    
    ---------- --------- ----------- 
    动漫         动漫        1
    综艺         综艺        1
    剧集         剧集        2
    电影         电影        3(所影响的行数为 4 行)