在一个表中有 ‘名称’,‘编号’,‘id’,其中不同的标号对应不同的名称类型,比如编号=‘A018'表示行政区域名称,有北京上海这些,编号=’A019‘ 表示建设状态,有建设中,完成等状态,现在我想查询 行政区域、建设状态,分两列显示,SQL语句该怎么写啊?

解决方案 »

  1.   


    try this:select case when 编号='A018' then 名称 end as 行政区域名称,
           case when 编号='A019' then 名称 end as 建设状态,
           *
    from tb
      

  2.   

    SELECT * FROM (
    select IIF( 编号='A018' , 名称 ,null) as 行政区域名称,
           IIF( 编号='A019' , 名称,NULL) as 建设状态,
    from tb
    )t
      

  3.   

    试过了 不对啊 取出来很多NULL值 而且建设状态显示的是北京上海这些地区名字
      

  4.   

    在navicat上运行直接报错
      

  5.   

    在navicat上运行直接报错你贴出表的结构,还有具体的数据,不然不好帮你
      

  6.   

    按照你的描述你这是是行转列问题?按照我的理解还有一个问题,比如,如果有下面的数据存在多个A018和A019,哪个A018和A019对应呢?已改还有一个字段来配对,假如是no;with tb(Code,[Name],no) as (
       select 'A019',N'建设中',1 union all
       select 'A018',N'北京',2 union all
          select 'A019',N'完成',3 union all
       select 'A018',N'上海',4 
    )
    select * from tb
    /*
    Code Name no
    A019 建设中 1
    A018 北京 2
    A019 完成 3
    A018 上海 4
    */
    ;with tb(Code,[Name],no) as (
       select 'A019',N'建设中',1 union all
       select 'A018',N'北京',1 union all
          select 'A019',N'完成',2 union all
       select 'A018',N'上海',2
    )
    select no,max(case when code='A018' then [Name] else '' end) as city,max(case when code='A019' then [name] else '' end) as status from tb
    group by no
    /*no city status
    1 北京 建设中
    2 上海 完成
    */