序号 城市     机器1              机器2          机器3            机器4                机器5 
           数量  面积 马力  数量  面积 马力  数量  面积 马力  数量  面积 马力  数量  面积 马力
1    北京   10   100  100    30   400  500    20   300  100    50   110  130    80   600  100===============================================================================================public List<TDStationmanageEquipment> equipment() {
List listmac = this.getHibernateTemplate().find("select deviceId from TSDevice");
String sql = "select f.city";
for(int i=0;i<listmac.size();i++){
String deviceId = listmac.get(i).toString(); //获得生产设备id编号
sql+=",e.number "+"a"+i+",e.area "+"b"+i+",e.power "+"c"+i+"" +   //后面是取的别名
" case when e.id = '"+deviceId+"'";

}
        sql+=" from TDStationmanageEquipment as e,TDStationmanageFarmerinfo as f,TSDevice as d " +
         "where d.deviceId = e.id and e.TDId = f.id";   
    return findByPage(sql);
  }
====================================================================================================
TSDevice 表 存着机器的编号 机器名字 是否使用三个字段  
TDStationmanageEquipment 存着TSDevice表的机器编号,数量 ,面积,马力
TDStationmanageFarmerinfo 存着城市的名字 等等.....主要是case when 不知道怎么用 ? 要的结果不是那样子
然后我尝试着这样用 把e.number,e.area,e.power写到then的后面就可以 只可惜then 后面只能跟一个字段!!
select f.city, 
case when e.id = 1 then e.number     // then 这里只能跟一个字段,如果可以跟2个以上,就是我要的结果了!!!
end
from t_d_stationmanage_equipment as e,t_d_stationmanage_farmerinfo as f,t_s_Device as d where d.deviceId = e.id and e.t_d_id = f.id

解决方案 »

  1.   


    --> 测试数据: @t
    declare @t table (col1 int,col2 int,col3 int,col4 int)
    insert into @t
    select 1,2,3,4 union all
    select 5,6,7,8 union all
    select 1,2,3,5 union all
    select 2,3,4,5select  
    case when t.col1 = 1 then (t.col2)end as col2,
    case when t.col1 = 1 then (t.col3)end as col3,
    case when t.col1 = 1 then (t.col4)end as col4
    from @t as t
    分开写不就结了?
      

  2.   


    then 这里只能跟一个字段,
    想跟多个字段,就要写多个case when 只不过 when 的条件 一样
      

  3.   


    关键是 在jsp页面根本就不显示case when 里面的东西 
    就职显示city一个值 why ?
      

  4.   


    这样写 语法上是对的
    但是我想要的 却不是这样的
    你这样写:
    case when t.col1 = 1 then (t.col2)end as col2,
    case when t.col1 = 1 then (t.col3)end as col3,
    case when t.col1 = 1 then (t.col4)end as col4
    查出等于1一次后end 
    如果是当=1的时候 全部执行 在最后end 就对了  
      

  5.   

    如果是当=1的时候 全部执行 在最后end  就会有语法错误
      

  6.   

    我运行了
    结果只显示city的值
    不显示then后面的值
      

  7.   


    序号 城市    机器1              机器2          机器3            机器4                机器5 
              数量  面积 马力  数量  面积 马力  数量  面积 马力  数量  面积 马力  数量  面积 马力 --可以把这个做成表头
    结果在用case when 的代码
      

  8.   

    你们终于明白楼主的意思了.楼主弄报表的吧?想这样显示效果,
    asp。net是这样弄得,放点框框,然后获取从数据库读出来的值
      

  9.   


    这种方法是可以实现的
    只是我明白case when 的用法精髓 :)
      

  10.   


    哈哈
    我有个报表的模块
    不过这个不是 谢谢
    我用的是SSH框架做的
      

  11.   

    楼主,比较一下下面两个SQL的结果,就知道了.
    declare @t table (YY int,MM int,amount int,qty int)
    insert into @t
    select 2009,1,100,1 union all
    select 2009,2,200,2 union all
    select 2009,3,300,3 union all
    select 2009,4,400,4
    --SQL 1
    select YY,
    case when t.MM = 1 then t.amount end as col2,
    case when t.MM = 1 then t.qty end as col2,
    case when t.MM = 2 then t.amount end as col2,
    case when t.MM = 2 then t.qty end as col2,
    case when t.MM = 3 then t.amount end as col2,
    case when t.MM = 3 then t.qty end as col2,
    case when t.MM = 4 then t.amount end as col2,
    case when t.MM = 4 then t.qty end as col2
    from @t as t
    --SQL 2
    select YY,
    SUM(case when t.MM = 1 then t.amount end) as col2,
    SUM(case when t.MM = 1 then t.qty end) as col2,
    SUM(case when t.MM = 2 then t.amount end) as col2,
    SUM(case when t.MM = 2 then t.qty end) as col2,
    SUM(case when t.MM = 3 then t.amount end) as col2,
    SUM(case when t.MM = 3 then t.qty end) as col2,
    SUM(case when t.MM = 4 then t.amount end) as col2,
    SUM(case when t.MM = 4 then t.qty end) as col2
    from @t as t
    GROUP  BY YY