当前位置: 代码迷 >> Sql Server >> 用sql语句 统计此编码下 有几个直接下级编码,该如何解决
  详细解决方案

用sql语句 统计此编码下 有几个直接下级编码,该如何解决

热度:74   发布时间:2016-04-27 13:31:33.0
用sql语句 统计此编码下 有几个直接下级编码
编码 名称 计算下面有几个直接下级(如下所示)
435 司法的 0
996 死地方 3
99601 沙发斯蒂芬 0
99602 司法所 1
9960201 司法的 0
99603 撒旦法 0
997 萨旦法 0
998 实施的 5
99801 急急急 0
99802 死死地 2
9980201 3lod你 2
998020101 解决的 3
99802010101 及会计 0
99802010102 让人此 0
99802010103 看55 0
998020102 及会计 0
9980203 合计 0
99803 jj看看 0
99806 呵呵 0
99807 和计划 0
999 城乡社 0
 
如上所列 如何用sql语句计算 是0,2,3,5 等等

------解决方案--------------------
这样?

SQL code
--> --> (Roy)生成測試數據 if not object_id('Tempdb..#T') is null    drop table #TGoCreate table #T([编码] varchar(50),[名称] nvarchar(10))Insert #Tselect 435,N'司法的' union allselect 996,N'死地方' union allselect 99601,N'沙发斯蒂芬' union allselect 99602,N'司法所' union allselect 9960201,N'司法的' union allselect 99603,N'撒旦法' union allselect 997,N'萨旦法' union allselect 998,N'实施的' union allselect 99801,N'急急急' union allselect 99802,N'死死地' union allselect 9980201,N'3lod你' union allselect 998020101,N'解决的' union allselect 99802010101,N'及会计' union allselect 99802010102,N'让人此' union allselect 99802010103,N'看55' union allselect 998020102,N'及会计' union allselect 9980203,N'合计' union allselect 99803,N'jj看看' union allselect 99806,N'呵呵' union allselect 99807,N'和计划' union allselect 999,N'城乡社'GoSelect a.[编码],a.名称,COUNT(b.编码) as 直属 from #T as aleft join #T as b on b.[编码] like a.[编码]+'__'  group by a.[编码],a.名称order by 1/*编码    名称    直属435    司法的    0996    死地方    399601    沙发斯蒂芬    099602    司法所    19960201    司法的    099603    撒旦法    0997    萨旦法    0998    实施的    599801    急急急    099802    死死地    29980201    3lod你    2998020101    解决的    399802010101    及会计    099802010102    让人此    099802010103    看55    0998020102    及会计    09980203    合计    099803    jj看看    099806    呵呵    099807    和计划    0999    城乡社    0*/
------解决方案--------------------
SQL code
select 编码,名称,       直属数量=(select count(1) from tb where 编码 like a.编码+'__')from tb a
  相关解决方案