a   b
1   足球    
1   男2   篮球
2   女3   篮球
3   男4   篮球
4   男a 问卷ID  b选项   有多少男的选足球  有多少女的选篮球交叉统计成下面的:
        男(计数)   百分比     女(计数)   百分比   总计(计数)  百分比
足球
篮球

解决方案 »

  1.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([a] int,[b] varchar(4))
    insert [tb]
    select 1,'足球' union all
    select 1,'男' union all
    select 2,'篮球' union all
    select 2,'女' union all
    select 3,'篮球' union all
    select 3,'男' union all
    select 4,'篮球' union all
    select 4,'男'
    goselect a.b,
    sum(case when b.b='男' then 1 else 0 end) [男(计数)],
    ltrim(cast(sum(case when b.b='男' then 1 else 0 end)*100.0/c.cnt as dec(18,2)))+'%'  [百分比],
    sum(case when b.b='女' then 1 else 0 end) [女(计数)],
    ltrim(cast(sum(case when b.b='女' then 1 else 0 end)*100.0/c.cnt as dec(18,2)))+'%'  [百分比],
    count(1) [总计(计数)],
    ltrim(cast(count(1)*100.0/c.cnt as dec(18,2)))+'%' [百分比]
    from
    (select rn=row_number() over(partition by a order by getdate()),* from tb) a
    join
    (select rn=row_number() over(partition by a order by getdate()),* from tb) b
    on a.a=b.a and a.rn=1 and b.rn=2
    join(select count(1) cnt from tb) c on 1=1
    group by a.b,c.cnt/**
    b    男(计数)       百分比                                        女(计数)       百分比                                        总计(计数)      百分比
    ---- ----------- ------------------------------------------ ----------- ------------------------------------------ ----------- ------------------------------------------
    篮球   2           25.00%                                     1           12.50%                                     3           37.50%
    足球   1           12.50%                                     0           0.00%                                      1           12.50%(2 行受影响)
    **/
      

  2.   

    select a.b,
    sum(case when b.b='男' then 1 else 0 end) [男(计数)],
    ltrim(cast(sum(case when b.b='男' then 1 else 0 end)*100.0/c.cnt as dec(18,2)))+'%'  [百分比],
    sum(case when b.b='女' then 1 else 0 end) [女(计数)],
    ltrim(cast(sum(case when b.b='女' then 1 else 0 end)*100.0/c.cnt as dec(18,2)))+'%'  [百分比],
    count(1) [总计(计数)],
    ltrim(cast(count(1)*100.0/c.cnt as dec(18,2)))+'%' [百分比]
    from
    (select rn=row_number() over(partition by a order by getdate()),* from tb) a
    join
    (select rn=row_number() over(partition by a order by getdate()),* from tb) b
    on a.a=b.a and a.rn=1 and b.rn=2
    join(select count(1)/2 cnt from tb) c on 1=1
    group by a.b,c.cnt/**
    b    男(计数)       百分比                                        女(计数)       百分比                                        总计(计数)      百分比
    ---- ----------- ------------------------------------------ ----------- ------------------------------------------ ----------- ------------------------------------------
    篮球   2           50.00%                                     1           25.00%                                     3           75.00%
    足球   1           25.00%                                     0           0.00%                                      1           25.00%(2 行受影响)**/
      

  3.   

    if object_id('ta') is not null 
    drop table ta
    create table ta(id int,xm varchar(10),xb varchar(10))
    insert into ta
    select 1, '足球','男' union all
    select 2, '篮球', '女' union all
    select 3, '篮球', '男' union all
    select 4, '篮球', '男'select 项目='足球',
    男计数=sum(case when xm='足球' and xb='男' then 1 else 0 end),男百分=sum(case when xm='足球' and xb='男' then 1 else 0 end)*1.0/sum(case when xm='足球' then 1 else 0 end),
    女计数=sum(case when xm='足球' and xb='女' then 1 else 0 end),女百分=sum(case when xm='足球' and xb='女' then 1 else 0 end)*1.0/sum(case when xm='足球' then 1 else 0 end),
    合计数=sum(case when xm='足球' then 1 else 0 end)
    from ta union all
    select 项目='篮球',
    男计数=sum(case when xm='篮球' and xb='男' then 1 else 0 end),男百分=sum(case when xm='篮球' and xb='男' then 1 else 0 end)*1.0/sum(case when xm='篮球' then 1 else 0 end),
    女计数=sum(case when xm='篮球' and xb='女' then 1 else 0 end),女百分=sum(case when xm='篮球' and xb='女' then 1 else 0 end)*1.0/sum(case when xm='篮球' then 1 else 0 end),
    合计数=sum(case when xm='篮球' then 1 else 0 end)
    from ta 
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([a] int,[b] varchar(4))
    insert [tb]
    select 1,'足球' union all
    select 1,'男' union all
    select 2,'篮球' union all
    select 2,'女' union all
    select 3,'篮球' union all
    select 3,'男' union all
    select 4,'篮球' union all
    select 4,'男'
    go
    select b,
    sum(case when c='男' then 1 else 0 end ) as [男(计数)],
    sum(case when c='男' then 1 else 0 end )*100/count(1) as [百分比],
    sum(case when c='女' then 1 else 0 end ) as [女(计数)],
    sum(case when c='女' then 1 else 0 end )*100/count(1) as [百分比],
    count(1) as [总计(计数)],
    count(1)*100/(select count(1) from tb where b  in('男','女')) as [百分比]
    from
    (
    select t1.a as id,t1.b as b,t2.b as c
    from tb t1 , tb t2 
    where t1.b not in('男','女') and t2.b in('男','女') and  t1.a=t2.a
    ) t
    group by b/*
    b    男(计数)       百分比         女(计数)       百分比         总计(计数)      百分比         
    ---- ----------- ----------- ----------- ----------- ----------- ----------- 
    篮球   2           66          1           33          3           75
    足球   1           100         0           0           1           25(所影响的行数为 2 行)*/
      

  5.   


    --SQL2005
    --> 生成测试数据表: [tb]
    IF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb] ([a] [int],[b] [nvarchar](10))
    INSERT INTO [tb]
    SELECT '1','足球' UNION ALL
    SELECT '1','男' UNION ALL
    SELECT '2','篮球' UNION ALL
    SELECT '2','女' UNION ALL
    SELECT '3','篮球' UNION ALL
    SELECT '3','男' UNION ALL
    SELECT '4','篮球' UNION ALL
    SELECT '4','男'--SELECT * FROM [tb]-->SQL查询如下:
    DECLARE @s VARCHAR(MAX)
    ;WITH t AS 
    (
    SELECT rn=ROW_NUMBER()OVER(PARTITION BY a ORDER BY a),*
    FROM tb 
    ),
    t1 AS
    (
    SELECT * FROM t PIVOT(MAX(b) FOR rn IN([1],[2]))b
    )
    SELECT @s=ISNULL(@s,'')
     +',SUM(CASE [2] WHEN '''+[2]+''' THEN 1 ELSE 0 END) AS ['+[2]+'计数]' 
     +',CAST(SUM(CASE [2] WHEN '''+[2]+''' THEN 1 ELSE 0 END)*100./COUNT(1) AS DEC(9,2)) AS 百分比'
    FROM t1
    GROUP BY [2]EXEC('
    WITH t AS 
    (
    SELECT rn=ROW_NUMBER()OVER(PARTITION BY a ORDER BY a),*
    FROM tb 
    ),
    t1 AS
    (
    SELECT * FROM t PIVOT(MAX(b) FOR rn IN([1],[2]))b
    )
    SELECT [1] AS 项目名'+@s+'
    ,COUNT(1) AS 总计数,
    CAST(COUNT(1)*100./(SELECT COUNT(1) FROM t1) AS DEC(9,2)) AS  百分比
    FROM t1 
    GROUP BY [1]'
    )
    /*
    项目名        男计数         百分比                                     女计数         百分比                                     总计数         百分比
    ---------- ----------- --------------------------------------- ----------- --------------------------------------- ----------- ---------------------------------------
    篮球         2           66.67                                   1           33.33                                   3           75.00
    足球         1           100.00                                  0           0.00                                    1           25.00(2 行受影响)
    */
      

  6.   

                男计数      N %        女计数     N%      总计     N % 
    足球         0         0.0%         0        0.0%      0      0.0% 
    篮球         0         0.0%         0        0.0%      0      0.0% 
    乒乓球       1         100.0%       1        50.0%     2      66.67% 
    桌球         0         0.0%         1        50.0%     1      33.33% 
    总计         1         100%         2        100%      3       100% 具体算法 看这个数据就明白, 最下面还需要一个总计,
    这个数据跟问题提供的数据不相同,只表明运算方式