我需要Duration
列,以便在上午8点到9点之间,...下午5点到下午6点我现在附上一张图片,目前我已经知道如何才能实现这一目标。
CREATE TABLE [dbo].[DurationMaster](
[Duration] [DateTime] NULL,
[FromTime] [varchar](5) NULL,
[ToTime] [varchar](5) NULL
) ON [PRIMARY]
Go
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'08:00 AM - 09:00 AM', N'08:01', N'09:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'09:00 AM - 10:00 AM', N'09:01', N'10:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'10:00 AM - 11:00 AM', N'10:01', N'11:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'11:00 AM - 12:00 PM', N'11:01', N'12:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'12:00 PM - 01:00 PM', N'12:01', N'13:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'01:00 PM - 02:00 PM', N'13:01', N'14:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'02:00 PM - 03:00 PM', N'14:01', N'15:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'03:00 PM - 04:00 PM', N'15:01', N'16:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'04:00 PM - 05:00 PM', N'16:01', N'17:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'05:00 PM - 06:00 PM', N'17:01', N'18:00')
存储过程:
CREATE PROCEDURE [EMS].[USP_FetchFieldScoreCallReport]
@StartDate datetime,
@EndDate datetime,
@UserID int,
@EntityID int
As
Begin
DECLARE @TblFormatedDate AS TABLE
(
Duration varchar(20),
FromTime smalldatetime,
ToTime smalldatetime
)
INSERT INTO @TblFormatedDate
SELECT Duration,
--'2015-11-19 '+FromTime,
CAST(@StartDate+FromTime as smalldatetime), CAST(+@EndDate+ToTime as smalldatetime)
--'2015-11-19 '+ToTime
FROM DurationMaster
--SELECT * FROM @TblFormatedDate
--select top 20 * FROM EMS.tbl_event (NOLOCK)
-- ORDER BY fld_event_created_on DESC
select CreatedBy.fld_user_full_name AS CreatedUser,FormatedDate.Duration,COUNT(Event.fld_event_id ) AS LogACallCount,
Sum(Case when fld_event_subject='Fax Sent' THEN 1 ELSE 0 END) AS FaxSent,
Sum(Case when fld_event_subject='Contract Prepared' THEN 1 ELSE 0 END) As ContractPrepared,
Sum(Case when fld_event_subject='Hot Lead' THEN 1 ELSE 0 END) As HotLead,
Sum(Case when fld_event_subject='Email Sent' THEN 1 ELSE 0 END) AS EmailSent
FROM EMS.tbl_event (NOLOCK) AS Event
INNER JOIN EMS.tbl_user AS CreatedBy (NOLOCK) ON CreatedBy.fld_user_id = Event.fld_event_created_by_id
LEFT JOIN @TblFormatedDate AS FormatedDate
ON Event.fld_event_created_on BETWEEN FormatedDate.FromTime AND FormatedDate.ToTime
WHERE fld_event_isactive=1 AND Event.fld_event_status = 'Completed' AND fld_event_created_by_id IN(@UserID)
AND Event.fld_event_istask=1 AND Event.fld_event_entity_id =100100
AND Event.fld_event_isactive = 1
AND FormatedDate.Duration is not null
GROUP BY FormatedDate.Duration, CreatedBy.fld_user_full_name
END
当axecute存储过程我得到这个网格时:
答案 0 :(得分:0)
CREATE TABLE [dbo].[DurationMaster](
[Duration] VARCHAR(20) NULL,
[FromTime] TIME NULL,
[ToTime] TIME NULL
) ON [PRIMARY]
Go
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES(N'08:00 AM - 09:00 AM', N'08:01', N'09:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'09:00 AM - 10:00 AM', N'09:01', N'10:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'10:00 AM - 11:00 AM', N'10:01', N'11:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'11:00 AM - 12:00 PM', N'11:01', N'12:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'12:00 PM - 01:00 PM', N'12:01', N'13:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'01:00 PM - 02:00 PM', N'13:01', N'14:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'02:00 PM - 03:00 PM', N'14:01', N'15:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'03:00 PM - 04:00 PM', N'15:01', N'16:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'04:00 PM - 05:00 PM', N'16:01', N'17:00')
INSERT [dbo].[DurationMaster] ([Duration], [FromTime], [ToTime]) VALUES (N'05:00 PM - 06:00 PM', N'17:01', N'18:00')
Go
CREATE PROCEDURE [EMS].[USP_FetchFieldScoreCallReport]
@StartDate datetime,
@EndDate datetime,
@UserID int,
@EntityID int
As
Begin
DECLARE @TblFormatedDate AS TABLE
(
Duration varchar(20),
FromTime time,
ToTime time
)
INSERT INTO @TblFormatedDate
SELECT Duration,
@StartDate+FromTime, @EndDate+ToTime --datetime + time = datetime. No cast needed
FROM DurationMaster
select CreatedBy.fld_user_full_name AS CreatedUser,FormatedDate.Duration,COUNT(Event.fld_event_id ) AS LogACallCount,
Sum(Case when fld_event_subject='Fax Sent' THEN 1 ELSE 0 END) AS FaxSent,
Sum(Case when fld_event_subject='Contract Prepared' THEN 1 ELSE 0 END) As ContractPrepared,
Sum(Case when fld_event_subject='Hot Lead' THEN 1 ELSE 0 END) As HotLead,
Sum(Case when fld_event_subject='Email Sent' THEN 1 ELSE 0 END) AS EmailSent
FROM EMS.tbl_event (NOLOCK) AS Event
INNER JOIN EMS.tbl_user AS CreatedBy (NOLOCK) ON CreatedBy.fld_user_id = Event.fld_event_created_by_id
INNER JOIN @TblFormatedDate AS FormatedDate
ON Event.fld_event_created_on BETWEEN FormatedDate.FromTime AND FormatedDate.ToTime
WHERE fld_event_isactive=1 AND Event.fld_event_status = 'Completed' AND fld_event_created_by_id IN(@UserID)
AND Event.fld_event_istask=1 AND Event.fld_event_entity_id =100100
AND Event.fld_event_isactive = 1
--AND FormatedDate.Duration is not null --Wih this condition is an Inner Join.. so use it!
GROUP BY FormatedDate.Duration, CreatedBy.fld_user_full_name
Order By FormatedDate.FromTime
END