日期:2014-05-17 浏览次数:20549 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-14 14:42:23
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([省区] varchar(4),[客户] varchar(12),[回款时期] datetime,[回款金额] int)
insert [huang]
select '湖北','湖北科技公司','2013/3/4',123 union all
select '湖北','湖北农村','2013/3/6',9000 union all
select '湖南','湖南农业','2013/3/4',678 union all
select '湖北','大吃大喝公司','2013/1/4',6789 union all
select '山东','科技','2013/2/4',56777
--------------开始查询--------------------------
select [省区],[一月份的回款金额]=SUM(CASE WHEN [回款时期] BETWEEN '2013-01-01' AND '2013-01-31' THEN [回款金额] ELSE 0 END ) ,
[二月份的回款金额]=SUM(CASE WHEN [回款时期] BETWEEN '2013-02-01' AND '2013-02-28' THEN [回款金额] ELSE 0 END ) ,
[三月份的回款金额]=SUM(CASE WHEN [回款时期] BETWEEN '2013-03-01' AND '2013-03-31' THEN [回款金额] ELSE 0 END )
from [huang]
GROUP BY [省区]
----------------结果----------------------------
/*
省区