有表T1:
编号 投票项目
110  AA
110  BB
110  CC
110  DD
66   SE
66   AB
...
表T2:
投票项目 票数
AA      1
BB      1
AA      1
查询当T1.编号=110结果:
投票项目 百分比 票数
AA     66.67% 2
BB     33.33% 1
CC     0%     0
DD     0%     0

解决方案 »

  1.   

    对不起,条件没有说完整:
    表T2: 
    编号 投票项目 票数 
    110 AA      1 
    110 BB      1 
    110 AA      1 
    其中T1.编号=T2.编号
      

  2.   

    create table T1(编号 int , 投票项目 varchar(10))
    insert into t1 values(110 , 'AA') 
    insert into t1 values(110 , 'BB') 
    insert into t1 values(110 , 'CC') 
    insert into t1 values(110 , 'DD') 
    insert into t1 values(66  , 'SE') 
    insert into t1 values(66  , 'AB') 
    create table T2(投票项目 varchar(10) , 票数 int)
    insert into t2 values('AA' ,     1 )
    insert into t2 values('BB' ,     1 )
    insert into t2 values('AA' ,     1 )declare @编号 as int
    set @编号 = 110select t1.* , 
           百分比 = isnull(cast(cast((select sum(票数) from t2 where 投票项目 = t1.投票项目) * 100.00 / (select sum(票数) from t2) as decimal(18,2)) as varchar) + '%','0%'),
           票数   = isnull((select sum(票数) from t2 where 投票项目 = t1.投票项目) , 0)
    from t1 where 编号 = @编号drop table t1 , t2
    /*
    编号          投票项目       百分比                             票数          
    ----------- ---------- ------------------------------- ----------- 
    110         AA         66.67%                          2
    110         BB         33.33%                          1
    110         CC         0%                              0
    110         DD         0%                              0(所影响的行数为 4 行)*/
      

  3.   

    编号不管用吧,得根据 t1.编号 = t2.编号 and t1.投票项目=t2.投票项目 来吧?
      

  4.   

    create table T1(编号 int , 投票项目 varchar(10))
    insert into t1 values(110 , 'AA') 
    insert into t1 values(110 , 'BB') 
    insert into t1 values(110 , 'CC') 
    insert into t1 values(110 , 'DD') 
    insert into t1 values(66  , 'SE') 
    insert into t1 values(66  , 'AB') 
    create table T2(编号 int , 投票项目 varchar(10) , 票数 int)
    insert into t2 values(110 , 'AA' ,     1 )
    insert into t2 values(110 , 'BB' ,     1 )
    insert into t2 values(110 , 'AA' ,     1 )declare @编号 as int
    set @编号 = 110select t1.* , 
           百分比 = isnull(cast(cast((select sum(票数) from t2 where 投票项目 = t1.投票项目 and 编号 = t2.编号) * 100.00 / (select sum(票数) from t2 where 编号 = t2.编号) as decimal(18,2)) as varchar) + '%','0%'),
           票数   = isnull((select sum(票数) from t2 where 投票项目 = t1.投票项目 and 编号 = t2.编号) , 0)
    from t1 where 编号 = @编号drop table t1 , t2
    /*
    编号          投票项目       百分比                             票数          
    ----------- ---------- ------------------------------- ----------- 
    110         AA         66.67%                          2
    110         BB         33.33%                          1
    110         CC         0%                              0
    110         DD         0%                              0(所影响的行数为 4 行)*/
      

  5.   

    实在对不起!!!!!
    表T2应该为这样: 
    表T2: 
    编号 投票项目 票数 
    110 AA      1 
    110 BB      1 
    110 AA      1 
    66  DG      1
    ...
    其中T1.编号=T2.编号
      

  6.   

    是的,根据:
     t1.编号 = t2.编号 and t1.投票项目=t2.投票项目
      

  7.   

    楼主试试看行不:
    select 投票项目
    sum(select 票数 from t2 where 编号=110 group by 投票项目)/count(select 票数 from t2 where 编号=110 group by 投票项目)  as 百分比
    count(select 票数 from t2 where 编号=110 group by 投票项目) as 票数 
    from t1 inner join t2
    on t1.编号=t2.编号