目前有一张 SQL SERVER 数据表,如下:
AUTOID date 连续量
1 2008-1-1 101
2 2008-1-2 102
3 2008-1-3 104
4 2008-1-4 105
5 2008-1-5 118
6 2008-1-6 121
7 2008-1-7 133
………………………………
52 2008-4-6 321
53 2008-4-7 433
字段连续量 按照AUTOID 字段不断增长。
求:按日,周,月,季统计 的sql 语句。
如:2008年1月份的连续量总共为多少(也就是2008.1月最末时间的数值减去1月最早时间的数值)。
AUTOID date 连续量
1 2008-1-1 101
2 2008-1-2 102
3 2008-1-3 104
4 2008-1-4 105
5 2008-1-5 118
6 2008-1-6 121
7 2008-1-7 133
………………………………
52 2008-4-6 321
53 2008-4-7 433
字段连续量 按照AUTOID 字段不断增长。
求:按日,周,月,季统计 的sql 语句。
如:2008年1月份的连续量总共为多少(也就是2008.1月最末时间的数值减去1月最早时间的数值)。
解决方案 »
- 求助!!SQL语句
- sql2005like前缀查找无效?
- 主從表右聯查找,不足5條紀錄地補全5條,補的紀錄其它字條段為空,至補上關聯ID
- 求教一个sql的问题!
- 数据冲突,如何解决?
- 请教如何设置SQL SERVER通过internet远端连接登录得到?
- 急:求SQL语句:对于一个表中有一个字段为国家,其中有一百多个国家名,我如何把这张表生成一个临时表,根据用户选择的国家,把选择的国
- EXEC执行动态SQL时设置变量的问题
- 请教数据库问题:[ODBC SQL Server Driver]连接占线导致另一个hstmt什么意思?
- 100分的问题---如何通过应用程序在sql server建库?
- 一次查询得到多个统计数据
- select好象要有多复杂就多复杂啊
--------------------按周
select datepart(week,date1),sum(l) from t1 group by datepart(week,date1)
--------------------按月
select datepart(month,date1),sum(l) from t1 group by datepart(month,date1)
--------------------按季度
select datepart(quarter,date1),sum(l) from t1 group by datepart(quarter,date1)
@type int,--0 日 1周 2月 3季
@indatetime datetime
as
begin if @type=0
begin
select 连续量 into #tmpD from auto
where
[date]=(select max([date]) from auto where year([date])=datepart(year,@indatetime) and month([date])=datepart(month,@indatetime) and day([date])=datepart(day,@indatetime))
or
[date]=(select min([date]) from auto where year([date])=datepart(year,@indatetime) and month([date])=datepart(month,@indatetime) and day([date])=datepart(day,@indatetime)) if @@rowcount=2
select a.连续量-b.连续量 as '差值' from #tmpD a,#tmpD b where b.连续量<a.连续量
else
select 0 as '差值'
drop table #tmpD
end
else if @type=1
begin
select 连续量 into #tmpW from auto
where
[date]=(select max([date]) from auto where datepart(ww,[date])=datepart(ww,@indatetime))
or
[date]=(select min([date]) from auto where datepart(ww,[date])=datepart(ww,@indatetime)) if @@rowcount=2
select a.连续量-b.连续量 as '差值' from #tmpW a,#tmpW b where b.连续量<a.连续量
else
select 0 as '差值'
drop table #tmpW
end
else if @type=2
begin
select 连续量 into #tmpM from auto
where
[date]=(select max([date]) from auto where year([date])=datepart(year,@indatetime) and month([date])=datepart(month,@indatetime))
or
[date]=(select min([date]) from auto where year([date])=datepart(year,@indatetime) and month([date])=datepart(month,@indatetime)) if @@rowcount=2
select a.连续量-b.连续量 as '差值' from #tmpM a,#tmpM b where b.连续量<a.连续量
else
select 0 as '差值'
drop table #tmpM
end
else if @type=3
begin
select 连续量 into #tmpQ from auto
where
[date]=(select max([date]) from auto where datepart(qq,[date])=datepart(qq,@indatetime))
or
[date]=(select min([date]) from auto where datepart(qq,[date])=datepart(qq,@indatetime)) if @@rowcount=2
select a.连续量-b.连续量 as '差值' from #tmpQ a,#tmpQ b where b.连续量<a.连续量
else
select 0 as '差值'
drop table #tmpQ
end
end
DATEPART
返回代表指定日期的指定日期部分的整数。语法
DATEPART ( datepart , date ) 参数
datepart是指定应返回的日期部分的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。日期部分 缩写
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
Hour hh
minute mi, n
second ss, s
millisecond ms
week (wk, ww) 日期部分反映对 SET DATEFIRST 作的更改。任何一年的 1 月 1 日定义了 week 日期部分的开始数字,例如:DATEPART(wk, 'Jan 1, xxxx') = 1,此处 xxxx 代表任一年。weekday (dw) 日期部分返回对应于星期中的某天的数,例如:Sunday = 1、Saturday = 7。weekday 日期部分产生的数取决于 SET DATEFIRST 设定的值,此命令设定星期中的第一天。date是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。对 1753 年 1 月 1 日之后的日期用datetime 数据类型。更早的日期存储为字符数据。当输入 datetime 值时,始终将其放入引号中。因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。如果只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049 (默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。有关时间值指定的更多信息,请参见时间格式。有关日期指定的更多信息,请参见 datetime 和 smalldatetime。 返回类型
int注释
DAY、MONTH、和 YEAR 函数分别是 DATEPART(dd, date)、DATEPART(mm, date)、和 DATEPART(yy, date) 的同义词。示例
GETDATE 函数返回当前日期;然而,比较时并不总是需要完整的日期信息(通常只是对日期的一部分进行比较)。此示例显示 GETDATE 及 DATEPART 的输出。SELECT GETDATE() AS 'Current Date'
GO下面是结果集:Current Date
---------------------------
Feb 18 1998 11:46PM SELECT DATEPART(month, GETDATE()) AS 'Month Number'
GO下面是结果集:Month Number
------------
2 此示例假设日期是 5 月 29 日。SELECT DATEPART(month, GETDATE())
GO下面是结果集:-----------
5 (1 row(s) affected)在此示例中,以数字表示日期。注意:SQL Server 将 0 解释为 01/01/1900。SELECT DATEPART(m, 0), DATEPART(d, 0), DATEPART(yy, 0)下面是结果集:----- ------ ------
1 1 1900datename也行.DATENAME
返回代表指定日期的指定日期部分的字符串。语法
DATENAME ( datepart , date )参数
datepart是指定应返回的日期部分的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。日期部分 缩写
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
Hour hh
minute mi, n
second ss, s
millisecond ms
weekday (dw) 日期部分返回星期几(星期天、星期一等)。是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。对 1753 年 1 月 1 日之后的日期用datetime 数据类型。更早的日期存储为字符数据。当输入 datetime 值时,始终将其放入引号中。因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。有关指定日期的更多信息,请参见 datetime 和 smalldatetime。有关时间值指定的更多信息,请参见时间格式。 如果只指定年份的最后两位数字,则小于或等于 two digit year cutoff 配置选项的值的最后两位数字的值所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2050(默认),则 49 被解释为 2049,50 被解释为 1950。为避免模糊,请使用四位数字的年份。返回类型
nvarchar注释
SQL Server 自动在字符和 datetime 值间按需要进行转换,例如,当将字符值与 datetime 值进行比较时。 示例
此示例从 GETDATE 返回的日期中提取月份名。SELECT DATENAME(month, getdate()) AS 'Month Name'下面是结果集:Month Name
------------------------------
February
1. 有如下数据表一张。
tDateTime 连续增量字段
2008-11-28 9:53:49 17658.266
2008-11-28 9:57:33 17660.906
2008-11-28 10:00:13 17662.787
2008-11-28 10:03:24 17665.031
2008-11-28 10:06:24 17667.152
2008-11-28 10:09:24 17669.271
2008-11-28 10:12:24 17671.385
2008-11-28 10:15:27 17673.535
2008-11-28 10:18:29 17675.676
2008-11-28 10:21:26 17677.76
2008-11-28 10:24:27 17679.887
……………………………………………………
……………………………………………………
2009-1-18 1:09:48 68922.773
2009-1-18 1:11:48 68924.195
2009-1-18 1:13:48 68925.625
2009-1-18 1:15:48 68927.055
2009-1-18 1:17:48 68928.484
2009-1-18 1:19:51 68929.945
2009-1-18 1:21:51 68931.375
2009-1-18 1:23:51 68932.82
2009-1-18 1:25:54 68934.2732. 想通过SQL 语句统计出如下表格
2.1 在某时间段内按日统计,如选择了在2008-11-28 到2009-1-1 之间想查看每天的增量各为多少。
得到如下表格tDateTime 当日增量 我自己写的注释
2008-11-28 597.41 28号那天的最大值减去28号当天的最小值
2008-11-29 596.844 29号那天的最大值减去29号当天的最小值
2008-11-30 597.096 30号那天的最大值减去30号当天的最小值
2008-12-1 596.996 1号那天的最大值减去1号当天的最小值
2008-12-2 596.963 2号那天的最大值减去2号当天的最小值
2008-12-3 596.987 3号那天的最大值减去3号当天的最小值
2008-12-4 596.984 4号那天的最大值减去4号当天的最小值
2008-12-5 596.938 5号那天的最大值减去5号当天的最小值
2008-12-6 596.926 6号那天的最大值减去6号当天的最小值
2.2 在某时间段内按月统计,如选择了在2008-11-28 到2009-1-1 之间想查看每天的增量各为多少。
得到如下表格
2008-11 1791.35 11月最大时间的值减去11月最早时间的值
2008-12 14327.629 12月最大时间的值减去12月最早时间的值
2009-1 55000 1月最大时间的值减去1月最早时间的值
2009-2 477300 2月最大时间的值减去2月最早时间的值
2.3 按周统计
2.4 按年统计
create table test(autoid int PRIMARY KEY identity(1,1),date datetime,连续量 int)
insert test select '2009-1-1',102
union all select '2009-1-2',103
union all select '2009-1-3',105
union all select '2009-1-4',107
union all select '2009-1-5',133
union all select '2009-1-6',145
union all select '2009-1-7',167
union all select '2009-1-8',168
union all select '2009-1-9',169
union all select '2009-1-10',170
union all select '2009-1-14',171
union all select '2009-1-16',172
union all select '2009-1-18',173
union all select '2009-1-22',174
union all select '2009-1-23',179
union all select '2009-1-26',182
union all select '2009-1-28',191
union all select '2009-1-29',192
union all select '2009-1-31',193
go
select * from test
declare @max int
declare @min int
declare @s int
set @max=(select 连续量 from test t where day(t.date)=31)
set @min=(select 连续量 from test t where day(t.date)=1)
set @s=@max-@min
select @s [一月递增量]
autoid date 连续量
----------- ------------------------------------------------------ -----------
1 2009-01-01 00:00:00.000 102
2 2009-01-02 00:00:00.000 103
3 2009-01-03 00:00:00.000 105
4 2009-01-04 00:00:00.000 107
5 2009-01-05 00:00:00.000 133
6 2009-01-06 00:00:00.000 145
7 2009-01-07 00:00:00.000 167
8 2009-01-08 00:00:00.000 168
9 2009-01-09 00:00:00.000 169
10 2009-01-10 00:00:00.000 170
11 2009-01-14 00:00:00.000 171
12 2009-01-16 00:00:00.000 172
13 2009-01-18 00:00:00.000 173
14 2009-01-22 00:00:00.000 174
15 2009-01-23 00:00:00.000 179
16 2009-01-26 00:00:00.000 182
17 2009-01-28 00:00:00.000 191
18 2009-01-29 00:00:00.000 192
19 2009-01-31 00:00:00.000 193(所影响的行数为 19 行)一月递增量
-----------
91(所影响的行数为 1 行)
按天统计的表,
按月统计的表,
按季节统计的表,
按年统计的表。
下面代码能实现同月内查询,但是两个月的话就存在问题,因为两个月日期编号1-31 有重复的。怎么改,谢谢。
select day(tDateTime),max(num)-min(num) from beehistory where tdateTime between '2008-08-01' and '2008-08-26' group by day(tDateTime) order by day(tDateTime)