项目编号 年 月 到位 完成
1 2000 1 45 879
2 2000 2 4578 1321
3 2000 3 789 789
4 2000 4 4578 1321
5 2000 5 45128 111321
项目编号为 nvarchar
年月 为 int
到位 完成 为 float
现在想通过转置的方式
项目编号 年 ? 1月 2月 3月 4月 5月
1 2000 到位 45 4578 789 4578 1321
2 2000 完成 879 1321 789 45128 111321
1 2000 1 45 879
2 2000 2 4578 1321
3 2000 3 789 789
4 2000 4 4578 1321
5 2000 5 45128 111321
项目编号为 nvarchar
年月 为 int
到位 完成 为 float
现在想通过转置的方式
项目编号 年 ? 1月 2月 3月 4月 5月
1 2000 到位 45 4578 789 4578 1321
2 2000 完成 879 1321 789 45128 111321
解决方案 »
- 有关SQL Server 2005锁的机制分析 的相关问题!请高手们帮帮忙!谢谢!
- 这算是MS-SQL的一个BUG吗?
- sql中保存图片,字段应该为什么类型
- 求一sql语句??
- 写一条sql语句,找回被误删的id!
- 一个基础的SQL语句问题,结果基础类的版块没人会,只好在这里问了。如何使用表名进行联合查询
- 紧急求助:关于两个数据库的相同表的表结构(字段)如何比较?
- 如何把数据表的值横向罗列出来
- select出的临时表的结果集会不会应为临时表的销毁而无效?
- 求日立显示器件(苏州)有限公司软件工程师(数据库编程)笔试题目!分数全部奉送!
- 求根据登录名查询对应的数据库用户名的SQL语句
- sllserSET LOCK_TIMEOUT
-- Author :SQL77(只为思齐老)
-- Date :2010-03-04 19:54:38
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([项目编号] int,[年] int,[月] int,[到位] int,[完成] int)
insert #TB
select 1,2000,1,45,879 union all
select 2,2000,2,4578,1321 union all
select 3,2000,3,789,789 union all
select 4,2000,4,4578,1321 union all
select 5,2000,5,45128,111321
--------------开始查询--------------------------
SELECT [年],
SUM(CASE WHEN 月=1 THEN 到位 ELSE 0 END)AS '1',
SUM(CASE WHEN 月=2 THEN 到位 ELSE 0 END)AS '2',
SUM(CASE WHEN 月=3 THEN 到位 ELSE 0 END)AS '3',
SUM(CASE WHEN 月=4 THEN 到位 ELSE 0 END)AS '4',
SUM(CASE WHEN 月=5 THEN 到位 ELSE 0 END)AS '5',
FLAGFROM
(
select [项目编号] ,[年] ,[月] ,[到位],'到位' FLAG from #TB
UNION ALL
select [项目编号] ,[年] ,[月] ,[完成],'完成' from #TB
)AS T GROUP BY [年],FLAG
----------------结果----------------------------
/* (所影响的行数为 5 行)年 1 2 3 4 5 FLAG
----------- ----------- ----------- ----------- ----------- ----------- ----
2000 45 4578 789 4578 45128 到位
2000 879 1321 789 1321 111321 完成(所影响的行数为 2 行)
*/
-- Author :SQL77(只为思齐老)
-- Date :2010-03-04 19:54:38
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([项目编号] int,[年] int,[月] int,[到位] int,[完成] int)
insert #TB
select 1,2000,1,45,879 union all
select 2,2000,2,4578,1321 union all
select 3,2000,3,789,789 union all
select 4,2000,4,4578,1321 union all
select 5,2000,5,45128,111321
--------------开始查询--------------------------
SELECT [年],FLAG,
SUM(CASE WHEN 月=1 THEN 到位 ELSE 0 END)AS '1',
SUM(CASE WHEN 月=2 THEN 到位 ELSE 0 END)AS '2',
SUM(CASE WHEN 月=3 THEN 到位 ELSE 0 END)AS '3',
SUM(CASE WHEN 月=4 THEN 到位 ELSE 0 END)AS '4',
SUM(CASE WHEN 月=5 THEN 到位 ELSE 0 END)AS '5'
FROM
(
select [项目编号] ,[年] ,[月] ,[到位],'到位' FLAG from #TB
UNION ALL
select [项目编号] ,[年] ,[月] ,[完成],'完成' from #TB
)AS T GROUP BY [年],FLAG
----------------结果----------------------------
/* (所影响的行数为 5 行)年 FLAG 1 2 3 4 5
----------- ---- ----------- ----------- ----------- ----------- -----------
2000 到位 45 4578 789 4578 45128
2000 完成 879 1321 789 1321 111321(所影响的行数为 2 行)
*/
insert TB
select 1,2000,1,45,879 union all
select 2,2000,2,4578,1321 union all
select 3,2000,3,789,789 union all
select 4,2000,4,4578,1321 union all
select 5,2000,5,45128,111321
goselect 项目编号 = 1 , 年 , flag = '到位',
max(case 月 when 1 then 到位 else 0 end) [1月],
max(case 月 when 2 then 到位 else 0 end) [2月],
max(case 月 when 3 then 到位 else 0 end) [3月],
max(case 月 when 4 then 到位 else 0 end) [4月],
max(case 月 when 5 then 到位 else 0 end) [5月]
from tb group by 年
union
select 项目编号 = 2 , 年 , flag = '完成',
max(case 月 when 1 then 完成 else 0 end) [1月],
max(case 月 when 2 then 完成 else 0 end) [2月],
max(case 月 when 3 then 完成 else 0 end) [3月],
max(case 月 when 4 then 完成 else 0 end) [4月],
max(case 月 when 5 then 完成 else 0 end) [5月]
from tb group by 年drop table tb/*
项目编号 年 flag 1月 2月 3月 4月 5月
----------- ----------- ---- ----------- ----------- ----------- ----------- -----------
1 2000 到位 45 4578 789 4578 45128
2 2000 完成 879 1321 789 1321 111321(所影响的行数为 2 行)
*/