日期:2014-05-18 浏览次数:20407 次
GO IF OBJECT_ID('TB')IS NOT NULL DROP TABLE TB GO CREATE TABLE TB( NAME VARCHAR(10), NANDU VARCHAR(10), VALUE INT ) GO INSERT TB SELECT '张三','难度一',18 UNION ALL SELECT '李四','难度二',23 UNION ALL SELECT '李四','难度三',12 UNION ALL SELECT '王五','难度一',16 UNION ALL SELECT '王五','难度二',11 UNION ALL SELECT '张三','难度四',9 UNION ALL SELECT '张三','难度二',15 UNION ALL SELECT '李四','难度四',11 go create view v_name as select NAME AS 姓名, isnull(sum(case when NANDU='难度一' then VALUE end),0) as 难度一, isnull(sum(case when NANDU='难度二' then VALUE end),0) as 难度二, isnull(sum(case when NANDU='难度三' then VALUE end),0) as 难度三, isnull(sum(case when NANDU='难度四' then VALUE end),0) as 难度四 from TB group by NAME go select * from v_name /* 姓名 难度一 难度二 难度三 难度四 李四 0 23 12 11 王五 16 11 0 0 张三 18 15 0 9 */ --给你写了一个例子
------解决方案--------------------
楼主是想把行数据转为列数据吗,是的话,如果是SQL2005 可以用 Pivot 语句。