34,597
社区成员
发帖
与我相关
我的任务
分享
USE test
GO
-->生成表tableA
if object_id('tableA') is not null
drop table tableA
Go
Create table tableA([Id] smallint,[Type] smallint,[Time7] datetime)
Insert into tableA
Select 1,0,'2013-01-03 00:00:18'
Union all Select 2,1,'2013-01-05 01:20:07'
Union all Select 3,2,'2013-01-09 07:08:02'
Union all Select 4,2,'2013-01-03 07:07:02'
Union all Select 5,2,'2013-01-02 07:07:02'
-->生成表tableC
if object_id('tableC') is not null
drop table tableC
Go
Create table tableC([TypeId] smallint,[TypeName] nvarchar(3))
Insert into tableC
Select 0,N'AAA'
Union all Select 1,N'BBB'
Union all Select 2,N'CCC'
go
DECLARE
@startDate DATETIME
,@endDate DATETIME
,@startTime DATETIME
,@endTime DATETIME
,@Type SMALLINT
SELECT
@startDate='2013-01-03'
,@endDate='2013-01-09'
,@startTime='07:00:01'
,@endTime='08:00:00'
,@Type=2
SELECT
a.Id,a.Type,b.TypeName,a.Time7
FROM tableA AS a,tableC AS b
WHERE a.Type=b.TypeId
AND Type=@Type
AND Time7 >=@startDate AND Time7<@endDate+1
AND CONVERT(DATETIME,CONVERT(VARCHAR(9),Time7,108)) BETWEEN @startTime AND @endTime
/*
Id Type TypeName Time7
------ ------ -------- -----------------------
3 2 CCC 2013-01-09 07:08:02.000
4 2 CCC 2013-01-03 07:07:02.000
*/
declare @sdate varchar(10),@edate varchar(10)
declare @stime varchar(8),@etime varchar(8)
select @sdate='2013-01-03',@edate='2013-01-10',@stime='07:00:00',@etime='08:00:00'
select a.type,a.num,c.typename from
(select Type,count(1)as num from tbA where time>@sdate and time<@edate
and convert(varchar,getdate(),24) between @stime and @etime
group by Type)a inner join tbC c on a.type=c.typeid