目前遇到的问题是这样表A 里存着项目名称 项目细节编号
     项目1        code001
    项目1        code005
    项目2        code002
    项目2        code003
    项目3        code004表B 存着项目字段 项目结款
     code001       500
    code005       600
    code002       1000
    code003       500
    code004       500现在想把2个表连接起来 并且想得到的结果是项目1      1100
项目2      1500
项目3      500求select的写法 目前想distinct 项目字段,sum(项目结款)from 表A 连 表B 但是不成功

解决方案 »

  1.   

    ;with cte(item,code) as
    (
    select '项目1','code001'
    union all select '项目1','code005'
    union all select '项目2','code002'
    union all select '项目2','code003'
    union all select '项目3','code004'
    ),cte1(code,amt) as
    (
    select 'code001',500
    union all select 'code005',600
    union all select 'code002',1000
    union all select 'code003',500
    union all select 'code004',500
    )select a.item,SUM(b.amt) as amt
    from cte a
    left join cte1 b on a.code=b.code
    group by a.item/*
    item amt
    ---------------------
    项目1 1100
    项目2 1500
    项目3 500
    */
      

  2.   

    ----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-10-12 14:02:41
    -- Version:
    --      Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64) 
    -- Jun 10 2013 20:09:10 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    go 
    create table [A]([项目名称] varchar(5),[项目细节编号] varchar(7))
    insert [A]
    select '项目1','code001' union all
    select '项目1','code005' union all
    select '项目2','code002' union all
    select '项目2','code003' union all
    select '项目3','code004'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([项目字段] varchar(7),[项目结款] int)
    insert [B]
    select 'code001',500 union all
    select 'code005',600 union all
    select 'code002',1000 union all
    select 'code003',500 union all
    select 'code004',500
    --------------开始查询----------------------------select * from [B]select 项目名称,SUM(项目结款)项目结款
    from [A] INNER JOIN [b] ON a.项目细节编号=b.项目字段
    GROUP BY 项目名称
    ----------------结果----------------------------
    /* 
    项目名称  项目结款
    ----- -----------
    项目1   1100
    项目2   1500
    项目3   500
    */
      

  3.   


    create table 表A
    (项目名称 varchar(10), 项目细节编号 varchar(10))insert into 表A
     select '项目1','code001' union all
     select '项目1','code005' union all
     select '项目2','code002' union all
     select '项目2','code003' union all
     select '项目3','code004'create table 表B
    (项目字段 varchar(10), 项目结款 int)insert into 表B
     select 'code001', 500 union all
     select 'code005', 600 union all
     select 'code002', 1000 union all
     select 'code003', 500 union all
     select 'code004', 500
    select a.项目名称,sum(b.项目结款) '项目结款'
     from 表A a
     inner join 表B b on a.项目细节编号=b.项目字段
     group by a.项目名称/*
    项目名称       项目结款
    ---------- -----------
    项目1        1100
    项目2        1500
    项目3        500(3 row(s) affected)
    */
      

  4.   


    create table A
    (项目名称 varchar(10), 项目细节编号 varchar(10))insert into A
     select '项目1','code001' union all
     select '项目1','code005' union all
     select '项目2','code002' union all
     select '项目2','code003' union all
     select '项目3','code004'create table B
    (项目字段 varchar(10), 项目结款 int)insert into B
     select 'code001', 500 union all
     select 'code005', 600 union all
     select 'code002', 1000 union all
     select 'code003', 500 union all
     select 'code004', 500
    --方法1.
    select a.项目名称,
           sum(b.项目结款) '项目结款'
    from A
    inner join B 
            on a.项目细节编号=b.项目字段
    group by a.项目名称/*
    项目名称 项目结款
    项目1 1100
    项目2 1500
    项目3 500
    */--方法2.
    select t.项目名称,
           SUM(项目结款) as 项目结款
    from
    (
    select a.项目名称,
           (select SUM(项目结款) 
            from B 
            where a.项目细节编号=b.项目字段) as 项目结款
    from A
    )t
    group by t.项目名称
    /*
    项目名称 项目结款
    项目1 1100
    项目2 1500
    项目3 500
    */
      

  5.   


    create table A
    (项目名称 varchar(10), 项目细节编号 varchar(10))insert into A
     select '项目1','code001' union all
     select '项目1','code005' union all
     select '项目2','code002' union all
     select '项目2','code003' union all
     select '项目3','code004'create table B
    (项目字段 varchar(10), 项目结款 int)insert into B
     select 'code001', 500 union all
     select 'code005', 600 union all
     select 'code002', 1000 union all
     select 'code003', 500 union all
     select 'code004', 500
    --方法1.
    select a.项目名称,
           sum(b.项目结款) '项目结款'
    from A
    inner join B 
            on a.项目细节编号=b.项目字段
    group by a.项目名称/*
    项目名称 项目结款
    项目1 1100
    项目2 1500
    项目3 500
    */--方法2.
    select t.项目名称,
           SUM(项目结款) as 项目结款
    from
    (
    select a.项目名称,
           (select SUM(项目结款) 
            from B 
            where a.项目细节编号=b.项目字段) as 项目结款
    from A
    )t
    group by t.项目名称
    /*
    项目名称 项目结款
    项目1 1100
    项目2 1500
    项目3 500
    */
      

  6.   

    SELECT A.项目名称,SUM(B.项目结款) AS 项目结款
    FROM A 
    INNER JOIN B ON A.项目细节编号=B.项目字段
    GROUP BY A.项目名称
      

  7.   


    select 项目名称,sum(项目结款) from (
    select * from a ,b where a.项目细节编号=b.项目字段) dddd group by 项目名称
      

  8.   


    ;with cte(item,code) as
    (
    select '项目1','code001'
    union all select '项目1','code005'
    union all select '项目2','code002'
    union all select '项目2','code003'
    union all select '项目3','code004'
    ),cte1(code,amt) as
    (
    select 'code001',500
    union all select 'code005',600
    union all select 'code002',1000
    union all select 'code003',500
    union all select 'code004',500
    )select item,SUM(amt) as totol from(
    select a.item,b.* from cte a,cte1 b where a.code=b.code
    ) c group by item