日期:2014-05-17 浏览次数:20771 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-01-13 13:22:12
-- 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: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([RecordDate] datetime,[Value1] numeric(2,1),[Value2] numeric(2,1))
insert [huang]
select '2013-01-10 11:20:30',5.0,6.0 union all
select '2013-01-10 11:20:32',7.0,8.0 union all
select '2013-01-11 12:20:30',4.0,3.0 union ALL
select '2013-01-11 12:20:32',9.0,5.0 union ALL
select '2013-02-11 12:20:32',9.0,5.0 union ALL
select '2014-01-11 12:20:32',9.0,5.0
--------------开始查询--------------------------
select SUBSTRING(CONVERT(VARCHAR(10),[RecordDate],120),PATINDEX('%-%',CONVERT(VARCHAR(10),[RecordDate],120))+1,2)[月份],MAX([Value1])MaxValue1,MIN([Value1])MinValue1,AVG([Value1])AvgValue
from [huang]
WHERE LEFT(CONVERT(VARCHAR(10),[RecordDate],120),4)='2013'
GROUP BY SUBSTRING(CONVERT(VARCHAR(10),[RecordDate],120),PATINDEX('%-%',CONVERT(VARCHAR(10),[RecordDate],120))+1,2)
----------------结果----------------------------
/*
月份 &