cteP是父表,字段v是值,需要求和
cteC是子表,因为实际过滤中需要用到cteC中的字段,所以做了连接。 with cteP(id,v) as
(
select 1,6
),
cteC (id,parentID) as
(
select 1,1
union
select 2,1
)
select sum(cteP.v)
from cteP left join cteC
on cteP.id=cteC.parentID以上连接后的结果集是2行,我想求cteP表字段v的合计.期望输出是:6
但以上输出的结果为:12
问题的原因我已经明白了,想知道在连接的情况下可否求cteP表字段v的合计呢?
谢谢。

解决方案 »

  1.   

    with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select sum(cteP.v)
        from cteP left join cteC
        on cteP.id=cteC.parentID
    with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select sum(cteP.v)
        from cteP inner join cteC
        on cteP.id=cteC.parentID
      

  2.   


    with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select sum(cteP.v)
        from cteP inner join cteC
        on cteP.id=cteC.id
      

  3.   

    ----不要掉了前面的;号
    ;with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select sum(cteP.v)
        from cteP inner join cteC
        on cteP.id=cteC.id
      

  4.   

    ;with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select sum(cteP.v)
        from cteP inner join cteC
        on cteP.id=cteC.id/*
    -----------
    6(1 行受影响)
    */
      

  5.   

    select m.id,m.v from cteP m where exists(select 1 from cteC where parentID = m.id)select m.id,sum(m.v) v from cteP m where exists(select 1 from cteC where parentID = m.id) group by m.id
      

  6.   

    create table cteP(id int,v int)
    insert into cteP values(1,6)
    create table cteC (id int,parentID int)
    insert into cteC values(1,1 )
    insert into cteC values(2,1 )
    goselect m.id,m.v from cteP m where exists(select 1 from cteC where parentID = m.id)/*id          v           
    ----------- ----------- 
    1           6(所影响的行数为 1 行)
    */select m.id,sum(m.v) v from cteP m where exists(select 1 from cteC where parentID = m.id) group by m.id
    /*id          v           
    ----------- ----------- 
    1           6(所影响的行数为 1 行)
    */drop table cteP , ctec
      

  7.   

    --问题的原因我已经明白了,想知道在连接的情况下可否求cteP表字段v的合计呢? 
    只要是 P 和 C 是一对多 联查出来就是多条的和 翻倍的
      

  8.   

        with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select sum(distinct cteP.v)
        from cteP left join cteC
        on cteP.id=cteC.parentID
      

  9.   

    create table cteP(id int,v int)
    insert into cteP values(1,6)
    create table cteC (id int,parentID int)
    insert into cteC values(1,1 )
    insert into cteC values(2,1 )
    goselect m.id,m.v from cteP m where exists(select 1 from cteC where parentID = m.id)/*id          v           
    ----------- ----------- 
    1           6(所影响的行数为 1 行)
    */select m.id,sum(m.v) v from cteP m where exists(select 1 from cteC where parentID = m.id) group by m.id
    /*id          v           
    ----------- ----------- 
    1           6(所影响的行数为 1 行)
    */select m.id,sum(m.v) v from cteP m where id in (select distinct parentID from cteC ) group by m.id
    /*
    id          v           
    ----------- ----------- 
    1           6(所影响的行数为 1 行)
    */drop table cteP , ctec
      

  10.   

    ;with cteP(id,v) as
        (
        select 1,6
        ),
        cteC (id,parentID) as
        (
        select 1,1
        union
        select 2,1
        )
        select distinct sum(cteP.v)
        from cteP inner join cteC
        on cteP.id=cteC.parentid
        group by ctec.id/*
    6
    */