如果一个表有字段type,type有值y or n,怎么用select语句返回一条记录,该记录包含y的总和,n的总和

解决方案 »

  1.   

    Select type,cn=count(*) from 表 group by type
      

  2.   

    select sum(case type when 'y' then 1 else 0 end) [Y Total],
    sum(case type when 'n' then 1 else 0 end) [N Total] from tb
      

  3.   

    如果是想统计这两种记录的各自数量
    select [type],count(1) [count] from 表 group by [type]
    ---------
    type  count
    y      ?
    n      ?
      

  4.   

    --try
    select 
    sum(case when type='y' then 1 else 0 end),
    sum(case when type='n' then 1 else 0 end)
    from tbName
      

  5.   

    create table T(type char(1))
    insert T select 'y'
    insert T select 'y'
    insert T select 'y'
    insert T select 'n'select 
    y_count=sum(case when type='y' then 1 else 0 end),
    n_count=sum(case when type='n' then 1 else 0 end)
    from T--result
    y_count     n_count     
    ----------- ----------- 
    3           1(1 row(s) affected)
      

  6.   

    create table T(type char(1))
    insert T select 'y'
    insert T select 'y'
    insert T select 'y'
    insert T select 'n'select
    type,
    數量=count(*)
    from T
    group by type
    order by 2 desc--result
    type 數量          
    ---- ----------- 
    y    3
    n    1(2 row(s) affected)
      

  7.   

    create table T(type char(1))
    insert T select 'y'
    insert T select 'y'
    insert T select 'y'
    insert T select 'n'select
    type,
    數量=count(*)
    from T
    group by type
    order by 2 desc--result
    type 數量          
    ---- ----------- 
    y    3
    n    1(2 row(s) affected)
      

  8.   

    select count(*) from table group by type
    你标题起的和这题的难度完全反比额
      

  9.   

    thks,要求是只返回一条记录