有两个表 
TABLE1:
字段  ID   T1  T2  T3  T4  T5
值     1   小  小  中  大  大TABLE2:
字段  ID   T1  T2  T3  T4  T5
值     1   2   4   1    2   2TABLE1为尺码表,TABLE2为数量表
求:小有多少个,中有多少个,大有多少个
(存放在变量里面)请各位帮帮忙啊

解决方案 »

  1.   

    --可以查询出小有多少个,中有多少个,大有多少个
    select ID,T1,count(1) from 
    (select ID, T1 from TABLE1
    union all
    select ID,T2 from TABLE1
    union all
    select ID,T3 from TABLE1
    union all
    select ID,T4 from TABLE1
    union all
    select ID,T5 from TABLE1) a
    group by id,t1
    order by id,t1
      

  2.   

    create table gh1 (ID int,T1 varchar(2),T2 varchar(2),T3 varchar(2),T4 varchar(2),T5 varchar(2)) insert gh1(id,t1,t2,t3,t4,t5)
    select 1,'a','b','c','d','e'create table gh2 (ID int,T1 int,T2 int,T3 int,T4 int,T5 int)
    insert gh2 select 1,2,4,1,2,2
    select id,case when a.t1='a' then b.t1
    from gh1 a left join gh2 b on (a.id=b.id)
      

  3.   

    Select A.ID,
       Sum(PatIndex('%小%',A.T1)*B.T1 + PatIndex('%小%',A.T2)*B.T2 + PatIndex('%小%',A.T3)*B.T3 + PatIndex('%小%',A.T4)*B.T4 + PatIndex('%小%',A.T5)*B.T5) AS 小,
       Sum(PatIndex('%中%',A.T1)*B.T1 + PatIndex('%中%',A.T2)*B.T2 + PatIndex('%中%',A.T3)*B.T3 + PatIndex('%中%',A.T4)*B.T4 + PatIndex('%中%',A.T5)*B.T5) AS 中,
       Sum(PatIndex('%大%',A.T1)*B.T1 + PatIndex('%大%',A.T2)*B.T2 + PatIndex('%大%',A.T3)*B.T3 + PatIndex('%大%',A.T4)*B.T4 + PatIndex('%大%',A.T5)*B.T5) AS 大
    From table1 A, table2 B
    Where A.ID=B.ID
    Group By A.ID
      

  4.   


    declare @tb1 table(id int,t1 varchar(10),t2 varchar(10),t3 varchar(10),t4 varchar(10),t5 varchar(10))
    insert into @tb1
    select 1,'小','小','中','大','大' 
    declare @tb2 table(id int,t1 int,t2 int,t3 int,t4 int,t5 int)
    insert into @tb2
    select 1,2,4,1,2,2select id, 
    (case when t1='小' then t1x else 0 end+case when t2='小' then t2x else 0 end
       +case when t3='小' then t3x else 0 end+case when t4='小' then t4x else 0 end
       +case when t5='小' then t5x else 0 end),
    (case when t1='中' then t1x else 0 end+case when t2='中' then t2x else 0 end
       +case when t3='中' then t3x else 0 end+case when t4='中' then t4x else 0 end
       +case when t5='中' then t5x else 0 end),
    (case when t1='大' then t1x else 0 end+case when t2='大' then t2x else 0 end
       +case when t3='大' then t3x else 0 end+case when t4='大' then t4x else 0 end
       +case when t5='大' then t5x else 0 end) 
    from 
    (
    select a.*,b.t1 t1x,b.t2 t2x,b.t3 t3x,b.t4 t4x,b.t5 t5x from @tb1 a inner join @tb2 b on a.id=b.id
     ) b