我有一个视图中一列是check和unchecked,如何把他们转换成0,1

解决方案 »

  1.   

    在查询的时候写成:case when 列名='check' then 0 else 1 end 
      

  2.   

    select  case when 视图一列 = 'check' then 0 else 1 end from 视图
      

  3.   

    谢谢了!好还想问一下,视图中如何统计每一列0的个数
    例如
    a  b  c
    1  2   0
    0  0   1
    0  1   0
    1  1   1
    0  0   0
    2  2   1
    统计的结果是
    a  b   c
    3  2    2
      

  4.   

    select count(a) a,count(b) b,count(c) c
    from 表这个和视图无关,跟表操作是一样的
      

  5.   

    select sum(case when a=0 then 1 else 0 end) as a
     sum(case when b=0 then 1 else 0 end) as b,
     sum(case when c=0 then 1 else 0 end) as c
    from tb
      

  6.   

    忘了count会统计0,谢谢阿汤哥提醒CREATE TABLE #tb (a int ,b int ,c int )
     INSERT INTO #TB select '1', '2', '0'
           union all select '0', '0', '1'
           union all select '0', '1', '0'
           union all select '1', '1', '1'
           union all select '0', '0', '0'
           union all select '2', '2', '1'
     
     
     select sum(case when a=0 then 1 else 0 end) AS a,
      sum(case when b=0 then 1 else 0 end) as b,
      sum(case when c=0 then 1 else 0 end) as c
     from #tb
     /*
     a           b           c
     ----------- ----------- -----------
     3           2           3
     
     (1 行受影响)
     */
      

  7.   

    太感谢了,大哥http://forum.csdn.net/PointForum/Manage/TopicManageView.aspx?forumID=55b4468c-6fde-444f-bde0-6e82082b1b73&topicID=a5f9ed6b-0b19-4e4b-a352-5aebdc8bee42&date=2012-10-16+21%3a07%3a25&v=13们!这就给分