按时间分段汇总

CREATE TABLE #tb(列名1 varchar(12),时间 datetime)
INSERT INTO #tb
SELECT '03174190188','2009-11-01 07:17:39.217' UNION ALL
SELECT '015224486575','2009-11-01 08:01:17.153' UNION ALL
SELECT '013593006926','2009-11-12 08:04:46.560' UNION ALL
SELECT '013599584239','2009-11-22 08:53:27.763' UNION ALL
SELECT '013911693526','2009-11-23 08:53:51.683' UNION ALL
SELECT '013846472440','2009-11-23 08:54:57.233' UNION ALL
SELECT '013990353697','2009-11-24 08:55:25.077' UNION ALL
SELECT '013990353697','2009-11-25 08:56:01.327' UNION ALL
SELECT '013945594843','2009-11-26 08:57:02.233' UNION ALL
SELECT '013990353697','2009-11-27 08:57:29.700' UNION ALL
SELECT '013916597421','2009-11-28 08:59:49.390' UNION ALL
SELECT '03916995857','2009-11-29 09:11:05.607' UNION ALL
SELECT '015097712001','2009-11-30 09:13:50.293'


DECLARE @minDate datetime,@maxDate datetime;
SELECT @minDate = '2009-11-1',@maxDate = '2009-12-01';

DECLARE @sql varchar(8000);
SET @sql = '';
SELECT @sql=@sql+',SUM(CASE WHEN DATEDIFF(day,B.时间,'''
                      +CONVERT(varchar(10),DATEADD(day,number,@minDate),120)
                      +''')=0 THEN 1 ELSE 0 END) AS ['
                        +CONVERT(varchar(10),DATEADD(day,number,@minDate),120)+']'
FROM master.dbo.spt_values 
WHERE type = 'P' AND DATEADD(day,number,@minDate)<=@maxDate;
print @sql
DECLARE @cmd nvarchar(4000);
SET @cmd = N'
SELECT ISNULL(A.时段,''合计'') AS 时段'+@sql+',
    COUNT(列名1) AS 合计
FROM(
    SELECT 时段=RIGHT(100+number,2)+'':00~''+RIGHT(100+number+1,2)+'':00'',
        MinDate = RIGHT(100+number,2)+'':00:00'',
        MaxDate = RIGHT(100+number+1,2)+'':00:00''
    FROM master.dbo.spt_values
    WHERE type = ''P'' AND number < 24
) AS A
    LEFT JOIN (SELECT * FROM #tb 
                WHERE 时间 BETWEEN @minDate AND @maxDate) AS B
ON CONVERT(varchar(8),B.时间,108) >= A.MinDate
    AND CONVERT(varchar(8),B.时间,108) < A.MaxDate
GROUP BY A.时段 WITH ROLLUP;'

EXEC sp_executesql @cmd,N'@minDate datetime,@maxDate datetime',@minDate,@maxDate;
print @cmd

DROP TABLE #tb;

 

/*
时段              2009-11-01  2009-11-02  2009-11-03  2009-11-04  2009-11-05  2009-11-06  2009-11-07  2009-11-08  2009-11-09  2009-11-10  2009-11-11  2009-11-12  2009-11-13  2009-11-14  2009-11-15  2009-11-16  2009-11-17  2009-11-18  2009-11-19  2009-11-20  2009-11-21  2009-11-22  2009-11-23  2009-11-24  2009-11-25  2009-11-26  2009-11-27  2009-11-28  2009-11-29  2009-11-30  2009-12-01  合计
--------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
00:00~01:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
01:00~02:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
02:00~03:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
03:00~04:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
04:00~05:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
05:00~06:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
06:00~07:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
07:00~08:00     1           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           1
08:00~09:00     1           0           0           0           0           0           0           0           0           0           0           1           0           0           0           0           0           0           0           0           0           1           2           1           1           1           1           1           0           0           0           10
09:00~10:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           1           1           0           2
10:00~11:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
11:00~12:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
12:00~13:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
13:00~14:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
14:00~15:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
15:00~16:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
16:00~17:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
17:00~18:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
18:00~19:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
19:00~20:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
20:00~21:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
21:00~22:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
22:00~23:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
23:00~24:00     0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0
合计              2           0           0           0           0           0           0           0           0           0           0           1           0           0           0           0           0           0           0           0           0           1           2           1           1           1           1           1           1           1           0           13
警告: 聚合或其他 SET 操作消除了 Null 值。

(25 行受影响)
*/


 

你可能感兴趣的:(MSSQL,数据库开发,T-SQL)