SQL中行列转换小例一个

需求:

数据库存储形式

ftype 分别对应 盒 箱

需要查询出的格式为:

SQL语句:

select  a.*,b.counts,b.dw,c.counts ct, c.dw cdw,b.counts||b.dw||c.counts|| c.dw 
from ( select fname from t_test group by fname ) a
join (
select t.fname,count(t.ftype) counts,case t.ftype when '1' then '盒' when '2' then '箱' end as dw from t_test t 
where t.ftype='1'
group by t.fname, t.ftype
) b on  a.fname= b.fname
join (
select t.fname,count(t.ftype) counts,case t.ftype when '1' then '盒' when '2' then '箱' end as dw from t_test t 
where t.ftype='2'
group by t.fname, t.ftype
) c on a.fname = c.fname;

 这个就可以了。。可是还有几个缺陷

主要ftype 类型添加。语句就需要修改 拼接sql语句应该可以实现, 有空再次修改一下

posted on 2013-01-05 10:24  守护者  阅读(215)  评论(0编辑  收藏  举报