22,210
社区成员
发帖
与我相关
我的任务
分享
商品编号 体积 过帐时间 接收时间
b001 0.10 2011-3-26 11:54:20 2011-3-26 12:56:10
b002 0.20 2011-3-26 13:54:12 2011-3-26 13:56:10
b003 0.15 2011-3-26 13:54:43 2011-3-26 13:56:10
b007 0.75 2011-3-26 13:54:45 2011-3-26 13:56:10
b007 0.08 2011-3-26 13:54:48 2011-3-26 13:56:10
b009 0.68 2011-3-26 13:54:50 2011-3-26 13:56:10
b066 0.38 2011-3-26 13:54:51 2011-3-26 13:56:10
b044 0.08 2011-3-26 13:54:53 2011-3-26 13:56:10
b009 1.13 2011-3-26 13:54:54 2011-3-26 13:56:10
b009 0.38 2011-3-26 13:54:47 2011-3-26 14:56:10
b009 0.03 2011-3-26 14:57:27 2011-3-26 15:58:39
b003 0.01 2011-3-26 15:56:57 2011-3-26 15:58:39
b009 0.02 2011-3-26 15:57:02 2011-3-26 15:58:39
b007 0.03 2011-3-26 15:57:04 2011-3-26 15:58:39
b009 0.03 2011-3-26 15:57:06 2011-3-26 15:58:39
b007 0.03 2011-3-26 15:57:08 2011-3-26 15:58:39
b009 0.02 2011-3-26 15:57:10 2011-3-26 15:58:39
b007 0.02 2011-3-26 15:57:11 2011-3-26 15:58:39
b009 0.64 2011-3-26 15:57:14 2011-3-26 15:58:39
b007 0.64 2011-3-26 15:57:16 2011-3-26 15:58:39
b009 0.26 2011-3-26 15:57:18 2011-3-26 15:58:39
b007 0.38 2011-3-26 15:57:20 2011-3-26 15:58:39
b009 0.02 2011-3-26 15:57:21 2011-3-26 15:58:39
b007 0.14 2011-3-26 15:57:25 2011-3-26 15:58:39
b044 1.57 2011-3-26 15:57:35 2011-3-26 15:58:39
b066 0.72 2011-3-26 15:57:35 2011-3-26 15:58:39
b007 0.02 2011-3-26 19:57:00 2011-3-26 15:58:39
b009 3.92 2011-3-26 16:23:07 2011-3-26 16:32:01
b009 18.03 2011-3-26 16:31:34 2011-3-26 16:32:01
b007 8.49 2011-3-26 20:31:34 2011-3-26 16:32:01
b007 1.80 2011-3-26 22:23:07 2011-3-26 23:32:01
过帐时间 汇总体积
9
10
11 0.1
12
13 3.8172
14 0.03
15 4.526
16 21.9545
17
18
19 0.02
20 8.487
21
22 1.8
23
接收时间 体积汇总
9
10
11
12 0.1
13 3.4404
14 0.3768
15 4.576
16 30.4415
17
18
19
20
21
22
23 1.8
服务器: 消息 8120,级别 16,状态 1,行 39
列 'a.过帐时间' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
go
select top 15 h=identity(int,9,1) into # from sysobjects
go
create table tb([商品编号] varchar(4),[体积] numeric(18,4),[过帐时间] datetime,[接收时间] datetime)
insert tb
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
go
select top 15 h=identity(int,9,1) into # from sysobjects
go
select a.h [过帐时间],isnull(ltrim(sum(b.[体积])),'')[汇总体积]
from # a left join tb b on a.h=datepart(hh,b.[过帐时间])
group by a.h
select a.h [接收时间],isnull(ltrim(sum(b.体积)),'')[汇总体积]
from # a left join tb b on a.h=datepart(hh,b.[接收时间])
group by a.h
go
drop table tb,#
/*
过帐时间 汇总体积
----------- -----------------------------------------
9
10
11 0.1000
12
13 3.8300
14 0.0300
15 4.5300
16 21.9500
17
18
19 0.0200
20 8.4900
21
22 1.8000
23
警告: 聚合或其他 SET 操作消除了空值。
(15 行受影响)
接收时间 汇总体积
----------- -----------------------------------------
9
10
11
12 0.1000
13 3.4500
14 0.3800
15 4.5800
16 30.4400
17
18
19
20
21
22
23 1.8000
警告: 聚合或其他 SET 操作消除了空值。
(15 行受影响)
*/
DECLARE @tb TABLE
(
商品编号 VARCHAR(10) ,
体积 FLOAT ,
过帐时间 VARCHAR(30) ,
接收时间 VARCHAR(30)
)
INSERT @tb
SELECT 'b001' ,
0.10 ,
'2011-3-26 11:54:20' ,
'2011-3-26 12:56:10'
UNION ALL
SELECT 'b002' ,
0.20 ,
'2011-3-26 13:54:12' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b003' ,
0.15 ,
'2011-3-26 13:54:43' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b007' ,
0.75 ,
'2011-3-26 13:54:45' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b007' ,
0.08 ,
'2011-3-26 13:54:48' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b009' ,
0.68 ,
'2011-3-26 13:54:50' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b066' ,
0.38 ,
'2011-3-26 13:54:51' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b044' ,
0.08 ,
'2011-3-26 13:54:53' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b009' ,
1.13 ,
'2011-3-26 13:54:54' ,
'2011-3-26 13:56:10'
UNION ALL
SELECT 'b009' ,
0.38 ,
'2011-3-26 13:54:47' ,
'2011-3-26 14:56:10'
UNION ALL
SELECT 'b009' ,
0.03 ,
'2011-3-26 14:57:27' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b003' ,
0.01 ,
'2011-3-26 15:56:57' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
0.02 ,
'2011-3-26 15:57:02' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.03 ,
'2011-3-26 15:57:04' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
0.03 ,
'2011-3-26 15:57:06' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.03 ,
'2011-3-26 15:57:08' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
0.02 ,
'2011-3-26 15:57:10' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.02 ,
'2011-3-26 15:57:11' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
0.64 ,
'2011-3-26 15:57:14' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.64 ,
'2011-3-26 15:57:16' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
0.26 ,
'2011-3-26 15:57:18' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.38 ,
'2011-3-26 15:57:20' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
0.02 ,
'2011-3-26 15:57:21' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.14 ,
'2011-3-26 15:57:25' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b044' ,
1.57 ,
'2011-3-26 15:57:35' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b066' ,
0.72 ,
'2011-3-26 15:57:35' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b007' ,
0.02 ,
'2011-3-26 19:57:00' ,
'2011-3-26 15:58:39'
UNION ALL
SELECT 'b009' ,
3.92 ,
'2011-3-26 16:23:07' ,
'2011-3-26 16:32:01'
UNION ALL
SELECT 'b009' ,
18.03 ,
'2011-3-26 16:31:34' ,
'2011-3-26 16:32:01'
UNION ALL
SELECT 'b007' ,
8.49 ,
'2011-3-26 20:31:34' ,
'2011-3-26 16:32:01'
UNION ALL
SELECT 'b007' ,
1.80 ,
'2011-3-26 22:23:07' ,
'2011-3-26 23:32:01'
--no.1
SELECT a.row ,
ISNULL(b.体积, 0) AS 汇总体积
FROM ( SELECT row
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY 商品编号 ) AS row
FROM @tb
) a
WHERE a.row BETWEEN 9 AND 23
) a
LEFT JOIN ( SELECT SUBSTRING(过帐时间, 11, 2) AS 过帐时间 ,
SUM(体积) AS 体积
FROM @tb
GROUP BY SUBSTRING(过帐时间, 11, 2)
) b ON a.row = b.过帐时间
--no.2
SELECT a.row ,
ISNULL(b.体积, 0) AS 体积汇总
FROM ( SELECT row
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY 商品编号 ) AS row
FROM @tb
) a
WHERE a.row BETWEEN 9 AND 23
) a
LEFT JOIN ( SELECT SUBSTRING(接收时间, 11, 2) AS 接收时间 ,
SUM(体积) AS 体积
FROM @tb
GROUP BY SUBSTRING(接收时间, 11, 2)
) b ON a.row = b.接收时间
row 汇总体积
-------------------- ----------------------
9 0
10 0
11 0.1
12 0
13 3.83
14 0.03
15 4.53
16 21.95
17 0
18 0
19 0.02
20 8.49
21 0
22 1.8
23 0
(15 行受影响)
row 体积汇总
-------------------- ----------------------
9 0
10 0
11 0
12 0.1
13 3.45
14 0.38
15 4.58
16 30.44
17 0
18 0
19 0
20 0
21 0
22 0
23 1.8
(15 行受影响)
DATEPART(hh,a.接收时间)--这里是a.接收时间
select * into #t from (select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23 )t
select
ltrim(isnull(DATEPART(hh,a.过帐时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'') as 汇总体积
from
#T full join tb a
on
DATEPART(hh,a.过帐时间)=#T.time
group by
DATEPART(hh,a.过帐时间),#T.time
union all
select '过帐时间','体积汇总'
union all
select
ltrim(isnull(DATEPART(hh,a.接收时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'')
from
#T full join tb a
on
DATEPART(hh,a.接收时间)=#T.time
group by
DATEPART(hh,a.接收时间),#T.time
--我这里怎么是好的 ?
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([商品编号] varchar(4),[体积] numeric(18,4),[过帐时间] datetime,[接收时间] datetime)
insert [tb]
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
--------------开始查询--------------------------
---构建时间表
select * into #t from (select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23 )t
select
ltrim(isnull(DATEPART(hh,a.过帐时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'') as 汇总体积
from
#T full join tb a
on
DATEPART(hh,a.过帐时间)=#T.time
group by
DATEPART(hh,a.过帐时间),#T.time
union all
select '过帐时间','体积汇总'
union all
select
ltrim(isnull(DATEPART(hh,a.接收时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'')
from
#T full join tb a
on
DATEPART(hh,a.接收时间)=#T.time
group by
DATEPART(hh,接收时间),#T.time
drop table #t
/*
(31 行受影响)
(15 行受影响)
过帐时间 汇总体积
------------ -----------------------------------------
9
10
11 0.1000
12
13 3.8300
14 0.0300
15 4.5300
16 21.9500
17
18
19 0.0200
20 8.4900
21
22 1.8000
23
过帐时间 体积汇总
9
10
11
12 0.1000
13 3.4500
14 0.3800
15 4.5800
16 30.4400
17
18
19
20
21
22
23 1.8000
警告: 聚合或其他 SET 操作消除了 Null 值。
(31 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([商品编号] varchar(4),[体积] numeric(18,4),[过帐时间] datetime,[接收时间] datetime)
insert [tb]
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
--------------开始查询--------------------------
---构建时间表
select * into #t from (select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23 )t
select
ltrim(isnull(DATEPART(hh,a.过帐时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'') as 汇总体积
from
#T full join tb a
on
DATEPART(hh,a.过帐时间)=#T.time
group by
DATEPART(hh,过帐时间),#T.time
union all
select '过帐时间','体积汇总'
union all
select
ltrim(isnull(DATEPART(hh,a.接收时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'')
from
#T full join tb a
on
DATEPART(hh,a.接收时间)=#T.time
group by
DATEPART(hh,接收时间),#T.time
drop table #t
/*
(31 行受影响)
(15 行受影响)
过帐时间 汇总体积
------------ -----------------------------------------
9
10
11 0.1000
12
13 3.8300
14 0.0300
15 4.5300
16 21.9500
17
18
19 0.0200
20 8.4900
21
22 1.8000
23
过帐时间 体积汇总
9
10
11
12 0.1000
13 3.4500
14 0.3800
15 4.5800
16 30.4400
17
18
19
20
21
22
23 1.8000
警告: 聚合或其他 SET 操作消除了 Null 值。
(31 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([商品编号] varchar(4),[体积] numeric(18,4),[过帐时间] datetime,[接收时间] datetime)
insert [tb]
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
--------------开始查询--------------------------
---构建时间表
select * into #t from (select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23 )t
select
ltrim(isnull(DATEPART(hh,a.过帐时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'') as 汇总体积
from
#T full join tb a
on
DATEPART(hh,a.过帐时间)=#T.time
group by
DATEPART(hh,过帐时间),#T.time
union all
select '过帐时间','体积汇总'
union all
select
ltrim(isnull(DATEPART(hh,a.接收时间),#T.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'')
from
#T full join tb a
on
DATEPART(hh,a.接收时间)=#T.time
group by
DATEPART(hh,接收时间),#T.time
服务器: 消息 8120,级别 16,状态 1,行 39
列 'a.过帐时间' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
那是我构建的一个时间表
--2000改成
select * into #t from (select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23 )t
服务器: 消息 156,级别 15,状态 1,行 36
在关键字 'with' 附近有语法错误。
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-03-28 17:11:27
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([商品编号] varchar(4),[体积] numeric(18,4),[过帐时间] datetime,[接收时间] datetime)
insert [tb]
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
--------------开始查询--------------------------
---构建时间表
;with f as
(select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23
)
select
ltrim(isnull(DATEPART(hh,a.过帐时间),f.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'') as 汇总体积
from
f full join tb a
on
DATEPART(hh,a.过帐时间)=f.time
group by
DATEPART(hh,过帐时间),f.time
union all
select '过帐时间','体积汇总'
union all
select
ltrim(isnull(DATEPART(hh,a.接收时间),f.time)) as 过帐时间,
ISNULL(ltrim(sum(体积)),'')
from
f full join tb a
on
DATEPART(hh,a.接收时间)=f.time
group by
DATEPART(hh,接收时间),f.time
----------------结果----------------------------
/*
过帐时间 汇总体积
------------ -----------------------------------------
9
10
11 0.1000
12
13 3.8300
14 0.0300
15 4.5300
16 21.9500
17
18
19 0.0200
20 8.4900
21
22 1.8000
23
过帐时间 体积汇总
9
10
11
12 0.1000
13 3.4500
14 0.3800
15 4.5800
16 30.4400
17
18
19
20
21
22
23 1.8000
警告: 聚合或其他 SET 操作消除了 Null 值。
(31 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-03-28 17:11:27
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([商品编号] varchar(4),[体积] numeric(18,4),[过帐时间] datetime,[接收时间] datetime)
insert [tb]
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
--------------开始查询--------------------------
---构建时间表
;with f as
(select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23
)
select
isnull(DATEPART(hh,a.过帐时间),f.time) as 过帐时间,
ISNULL(ltrim(sum(体积)),'')
from
f full join tb a
on
DATEPART(hh,a.过帐时间)=f.time
group by
DATEPART(hh,过帐时间),f.time
----------------结果----------------------------
/*
(31 行受影响)
过帐时间
----------- -----------------------------------------
9
10
11 0.1000
12
13 3.8300
14 0.0300
15 4.5300
16 21.9500
17
18
19 0.0200
20 8.4900
21
22 1.8000
23
警告: 聚合或其他 SET 操作消除了 Null 值。
(15 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-03-28 17:11:27
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([商品编号] varchar(4),[体积] numeric(4,2),[过帐时间] datetime,[接收时间] datetime)
insert [tb]
select 'b001',0.10,'2011-3-26 11:54:20','2011-3-26 12:56:10' union all
select 'b002',0.20,'2011-3-26 13:54:12','2011-3-26 13:56:10' union all
select 'b003',0.15,'2011-3-26 13:54:43','2011-3-26 13:56:10' union all
select 'b007',0.75,'2011-3-26 13:54:45','2011-3-26 13:56:10' union all
select 'b007',0.08,'2011-3-26 13:54:48','2011-3-26 13:56:10' union all
select 'b009',0.68,'2011-3-26 13:54:50','2011-3-26 13:56:10' union all
select 'b066',0.38,'2011-3-26 13:54:51','2011-3-26 13:56:10' union all
select 'b044',0.08,'2011-3-26 13:54:53','2011-3-26 13:56:10' union all
select 'b009',1.13,'2011-3-26 13:54:54','2011-3-26 13:56:10' union all
select 'b009',0.38,'2011-3-26 13:54:47','2011-3-26 14:56:10' union all
select 'b009',0.03,'2011-3-26 14:57:27','2011-3-26 15:58:39' union all
select 'b003',0.01,'2011-3-26 15:56:57','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:02','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:04','2011-3-26 15:58:39' union all
select 'b009',0.03,'2011-3-26 15:57:06','2011-3-26 15:58:39' union all
select 'b007',0.03,'2011-3-26 15:57:08','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:10','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 15:57:11','2011-3-26 15:58:39' union all
select 'b009',0.64,'2011-3-26 15:57:14','2011-3-26 15:58:39' union all
select 'b007',0.64,'2011-3-26 15:57:16','2011-3-26 15:58:39' union all
select 'b009',0.26,'2011-3-26 15:57:18','2011-3-26 15:58:39' union all
select 'b007',0.38,'2011-3-26 15:57:20','2011-3-26 15:58:39' union all
select 'b009',0.02,'2011-3-26 15:57:21','2011-3-26 15:58:39' union all
select 'b007',0.14,'2011-3-26 15:57:25','2011-3-26 15:58:39' union all
select 'b044',1.57,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b066',0.72,'2011-3-26 15:57:35','2011-3-26 15:58:39' union all
select 'b007',0.02,'2011-3-26 19:57:00','2011-3-26 15:58:39' union all
select 'b009',3.92,'2011-3-26 16:23:07','2011-3-26 16:32:01' union all
select 'b009',18.03,'2011-3-26 16:31:34','2011-3-26 16:32:01' union all
select 'b007',8.49,'2011-3-26 20:31:34','2011-3-26 16:32:01' union all
select 'b007',1.80,'2011-3-26 22:23:07','2011-3-26 23:32:01'
--------------开始查询--------------------------
---构建时间表
;with f as
(select 9 as time union all select 10 union all select 11 union all select 12 union all select 13 union all
select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all
select 19 union all select 20 union all select 21 union all select 22 union all select 23
)
select
isnull(DATEPART(hh,a.过帐时间),f.time) as 过帐时间,
ISNULL(sum(体积),0)
from
f full join tb a
on
DATEPART(hh,a.过帐时间)=f.time
group by
DATEPART(hh,过帐时间),f.time
----------------结果----------------------------
/*
(31 行受影响)
过帐时间
----------- ---------------------------------------
9 0.00
10 0.00
11 0.10
12 0.00
13 3.83
14 0.03
15 4.53
16 21.95
17 0.00
18 0.00
19 0.02
20 8.49
21 0.00
22 1.80
23 0.00
警告: 聚合或其他 SET 操作消除了 Null 值。
(15 行受影响)
*/