• SQL 树结构统计数据



    create table #Tmp
    (
    ID int IDENTITY (1,1) not null,
    name varchar(50),
    sl int
    );

    DECLARE @ID VARCHAR(36)
    SET @ID = '7197ccfb-f01d-437f-9059-f4b821ebf2e1'
    ;WITH T AS
    (
    SELECT ID , parentid , NAME
    FROM Base_SXZD
    WHERE ID = @ID
    UNION ALL
    SELECT A.ID , A.parentid , A.NAME
    FROM Base_SXZD AS A JOIN T AS B ON A.parentid = B.id
    )
    insert into #Tmp
    select name=(select name from Base_SXZD where id='7197ccfb-f01d-437f-9059-f4b821ebf2e1'),sl = sum(m.sl) from (
    select a.id, a.name,count(b.id) as sl from T a
    left join jxxt_ajlb b on a.id = b.ajlxid
    " + GetIfWhere(all, ajly) + @"
    group by a.id,a.name) m


    SET @ID = '5090a923-3c1a-4628-95ae-12edf10d41ae'
    ;WITH T AS
    (
    SELECT ID , parentid , NAME
    FROM Base_SXZD
    WHERE ID = @ID
    UNION ALL
    SELECT A.ID , A.parentid , A.NAME
    FROM Base_SXZD AS A JOIN T AS B ON A.parentid = B.id
    )
    insert into #Tmp
    select name=(select name from Base_SXZD where id='5090a923-3c1a-4628-95ae-12edf10d41ae'),sl = sum(m.sl) from (
    select a.id, a.name,count(b.id) as sl from T a
    left join jxxt_ajlb b on a.id = b.ajlxid
    " + GetIfWhere(all, ajly) + @"
    group by a.id,a.name) m


    SET @ID = '91322101-c2cb-47bd-91ce-69418722d835'
    ;WITH T AS
    (
    SELECT ID , parentid , NAME
    FROM Base_SXZD
    WHERE ID = @ID
    UNION ALL
    SELECT A.ID , A.parentid , A.NAME
    FROM Base_SXZD AS A JOIN T AS B ON A.parentid = B.id
    )
    insert into #Tmp
    select name=(select name from Base_SXZD where id='91322101-c2cb-47bd-91ce-69418722d835'),sl = sum(m.sl) from (
    select a.id, a.name,count(b.id) as sl from T a
    left join jxxt_ajlb b on a.id = b.ajlxid
    " + GetIfWhere(all, ajly) + @"
    group by a.id,a.name) m

    select ID,name, isnull(sl,0) as ajsl from #Tmp
    drop table #Tmp

  • 相关阅读:
    SharePoint母板页更改
    SharePoint的一些基本操作
    百度地图
    内存管理
    根据文字的个数,label自动适应高度
    navgationBar
    接收服务器上的图片,可以用webview或者 imageview
    iOS 自带的解析json的类。
    改变uilable uibutton等的字体颜色、大小。
    Nsstring和NSdata的编码转换
  • 原文地址:https://www.cnblogs.com/ssk-bak/p/5596156.html
Copyright © 2020-2023  润新知