日期:2014-05-17 浏览次数:20742 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-16 15:46:53
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([年] int,[月] int,[销售量(万)] int)
insert [huang]
select 2001,1,10 union all
select 2001,2,11 union all
select 2001,3,15 union all
select 2001,4,13 union all
select 2002,1,15 union all
select 2002,2,14 union all
select 2002,3,16 union all
select 2002,4,17
--------------开始查询--------------------------
select 年,SUM(CASE WHEN 月=1 then [销售量(万)] ELSE 0 END )'一月'
,SUM(CASE WHEN 月=2 then [销售量(万)] ELSE 0 END) '二月',
SUM(CASE WHEN 月=3 then [销售量(万)] ELSE 0 END )'三月',
SUM(CASE WHEN 月=4 then [销售量(万)] ELSE 0 END )'四月'
from [huang]
GROUP BY 年
----------------结果----------------------------
/*
年 一月 二月 三月 四月
----------- ----------