有两个表
T1
编号 单位 规格 数量
0001  个   1米  20
0001  个   1米  10
0002  条   2米  50
0001  条   1米  20
0002  条   2米  10
T2
编号 报价 条件
0001  10   1
0002  11   1
0001  20   0想要的结果条件为1的,编号相同的数量相加乘于报价,如下:编号 单位 规格 数量 价格
0001  个   1米  50   500
0002  条   2米  60   660在线等,谢谢!!

解决方案 »

  1.   


    select t.编号,t.单位,t.规格,t.数量 * tt.报价
    from(
    select 编号,单位,规格,sum(数量) as 数量
    from t1
    group by 编号,单位,规格)t left join
    (select 编号,min(报价) as 报价 from 
    t2 where 条件 = '1' group by 编号 )tt on t.编号 = tt.编号
      

  2.   

    T1跟T2怎麼關聯0001 个 1米 20
    0001 个 1米 10
    0002 条 2米 50
    0001 条 1米 20
    T1既是個又是條。。T2中 
    0001 10 1
    0002 11 1
    0001 20 0兩個001?
      

  3.   


    declare @T1 table (编号 varchar(4),单位 varchar(2),规格 varchar(3),数量 int)
    insert into @T1
    select '0001','个','1米',20 union all
    select '0001','个','1米',10 union all
    select '0002','条','2米',50 union all
    select '0001','条','1米',20 union all
    select '0002','条','2米',10declare @T2 table (编号 varchar(4),报价 int,条件 int)
    insert into @T2
    select '0001',10,1 union all
    select '0002',11,1 union all
    select '0001',20,0select a.*,价格=b.报价*a.数量 from @T2 b
    left join 
    (select 编号,min(单位) as 单位,max(规格)as 规格,sum(数量) as 数量 
    from @T1 a group by 编号) a
    on b.编号=a.编号 where b.条件=1/*
    编号   单位   规格   数量          价格
    ---- ---- ---- ----------- -----------
    0001 个    1米   50          500
    0002 条    2米   60          660
    */
      

  4.   


    select a.编号,a.单位,a.规格,
    sum(a.数量) 数量,sum(a.数量)*b.价格 金额
    from t1 a join t2 b 
           on a.a.编号 = b.编号 and a.单位 = b.单位 and a.规格 = b.规格 and b.条件 = 1
      

  5.   


    declare @T1 table (编号 varchar(4),单位 varchar(2),规格 varchar(3),数量 int)
    insert into @T1
    select '0001','个','1米',20 union all
    select '0001','个','1米',10 union all
    select '0002','条','2米',50 union all
    select '0001','个','1米',20 union all
    select '0002','条','2米',10declare @T2 table (编号 varchar(4),报价 int,条件 int)
    insert into @T2
    select '0001',10,1 union all
    select '0002',11,1 union all
    select '0001',20,0select a.编号,a.单位,a.规格,
        sum(a.数量) 数量,sum(a.数量)*b.报价 金额
    from @T1 a join @T2 b 
           on a.编号 = b.编号 and b.条件 = 1
    group by a.编号,a.单位,a.规格,b.报价/*****************编号   单位   规格   数量          金额
    ---- ---- ---- ----------- -----------
    0001 个    1米   50          500
    0002 条    2米   60          660(2 行受影响)
      

  6.   

    前一位的结果不正确,后位高手的结果都可以,更喜欢AcHerat的干净利落,谢谢!!
      

  7.   

    select a.编号,a.单位,a.规格,
        sum(a.数量) 数量,sum(a.数量)*b.价格 金额
    from t1 a join t2 b 
           on a.a.编号 = b.编号 and a.单位 = b.单位 and a.规格 = b.规格 and b.条件 = 1
    group by a.编号,a.单位,a.规格
      

  8.   

    select a.编号,a.单位,a.规格,
        sum(a.数量) 数量,sum(a.数量)*b.价格 金额
    from t1 a join t2 b 
           on a.a.编号 = b.编号 and a.单位 = b.单位 and a.规格 = b.规格 and b.条件 = 1
    group by a.编号,a.单位,a.规格