表A:
col1       col2
中国         100
中国人       5
中国货       20
中国心       25
法国         1
法国巴黎     4
法国铁塔     11
。    表B:
col1      
中国  
法国
。要求:按表B中的col列分组统计表A当中的col2总合。结果格式:
表C:
col1    col2
中国      150
法国      16
     

解决方案 »

  1.   

    select B.col1,(select sum(col2) from A where left(A.col1,2)=B.col1) as col2 from B
      

  2.   

    select B.col1,(select sum(col2) from A where left(A.col1,2)=B.col1) as col2 from B
      

  3.   

    --tryselect b.col1 ,sum(col2) as col2
    from b join a on  a.col1 like ''+b.col1 +'%'''
    group by b.col1
      

  4.   

    select b.col1 ,sum(col2) as col2
    from b join a on  a.col1 like b.col1 +'%'
    group by b.col1
    --可以利用上索引!
      

  5.   

    create table A(col1 nvarchar(10), col2 int)
    insert A select '中国',         100
    union all select '中国人',       5
    union all select '中国货',       20
    union all select '中国心',       25
    union all select '法国',         1
    union all select '法国巴黎',     4
    union all select '法国铁塔',     11create table B(col1 nvarchar(10))
    insert B select '中国'  
    union all select '法国'
     
    select tmp.col1, sum(tmp.col2) as col2
    from(
    select B.col1, A.col2 from A, B
    where A.col1 like B.col1+'%'
    ) tmp group by tmp.col1--result
    col1       col2        
    ---------- ----------- 
    法国         16
    中国         150(2 row(s) affected)
      

  6.   

    --tryselect b.col1 ,sum(col2) as col2
    from b join a on  a.col1 like b.col1 +'%'   --原来这里不用 ' 这个也可以  ?
    group by b.col1
      

  7.   

    declare @ta table(col1  varchar(50),     col2 int)
    insert @ta
    select '中国',         100 union all
    select '中国人',       5  union all
    select '中国货',      20 union all
    select '中国心',       25 union all
    select '法国',         1 union all
    select '法国巴黎',     4 union all
    select '法国铁塔',     11declare @tb table(col1 varchar(20))
    insert @tb    
    select '中国' union all
    select '法国'select tb.col1,数量=sum(col2)
    from @ta ta , @tb tb 
    where charindex(tb.col1,ta.col1)=1
    group by tb.col1 order by 数量 desc(所影响的行数为 7 行)
    (所影响的行数为 2 行)col1                 数量          
    -------------------- ----------- 
    中国                   150
    法国                   16(所影响的行数为 2 行)
      

  8.   

    A表中的col1列可能出现类似   铁塔法国巴黎 的值。
    关键字不一定在最前面阿
      

  9.   

    个人认为你表的结构有点问题,改为下边方式比较好
    表A: PK:代号+序号
     代号   序号     col1       col2
     001     1      中国         100
     001     2      中国人       5
     001     3      中国货       20
     001     4      中国心       25
     002     1      法国         1
     002     2      法国巴黎     4
     002     3      法国铁塔     11
    。    表B:PK:代号 外键连表A
    代号 col1      
    001  中国  
    002  法国
      

  10.   

    个人认为,一个好的表结构要比一个好的SQL语句更重要
      

  11.   

    select b.col1 ,sum(col2) as col2
    from a left join b on  a.col1 like +'%'+b.col1 +'%'
    group by b.col1
    可以
      

  12.   

    LZ有没有测试,怎么出不来结果:
    create table A(col1 nvarchar(10), col2 int)
    insert A select '中国',         100
    union all select '中国人',       5
    union all select '中国货',       20
    union all select '中国心',       25
    union all select '法国',         1
    union all select '法国巴黎',     4
    union all select '法国铁塔',     11create table B(col1 nvarchar(10))
    insert B select '中国'  
    union all select '法国'select b.col1 ,sum(col2) as col2
    from b join a on  a.col1 like b.col1 +'%'
    group by b.col1drop table a
    drop table b
    ------
    col1                 col2          
    -------------------- ----------- 
    法国                   16
    中国                   150
    (所影响的行数为 2 行)
      

  13.   

    懒猫:lz说了,中国这两字可能出现在中央的...
    我们的 得改改...
    --try
    select tb.col1,数量=sum(col2)
    from @ta ta , @tb tb 
    where charindex(tb.col1,ta.col1)>0    --关键字有可能在中间
    group by tb.col1 order by 数量 desc