Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to form the query .

I am having number of employees and they made more transaction per day.
I want First transaction time (Start time) for the for each employee using group by.

This i want to query with every 15 mins transaction starting from "08:00" to "17:15" using group by.

I am little bit confused how to form this query.

can anyone help how to form the query..?

See my sample table data:
-------------------------
EmpID Name TransactionDate
101 Abdul 2012-05-2012 08:40:30 0000
101 Abdul 2012-05-2012 09:40:30 0000
101 Abdul 2012-05-2012 12:20:30 0000
102 Raju 2012-05-2012 08:15:30 0000
102 Raju 2012-05-2012 12:40:30 0000
102 Raju 2012-05-2012 16:20:30 0000
103 Albert 2012-05-2012 08:40:30 0000
103 Albert 2012-05-2012 10:40:30 0000
103 Albert 2012-05-2012 12:20:30 0000
103 Albert 2012-05-2012 14:40:30 0000
103 Albert 2012-05-2012 15:40:30 0000
103 Albert 2012-05-2012 17:10:30 0000
103 Albert 2012-05-2012 17:15:30 0000
104 Raghu 2012-05-2012 08:00:30 0000
104 Raghu 2012-05-2012 09:40:30 0000
104 Raghu 2012-05-2012 12:20:30 0000
102 Johnson 2012-05-2012 08:00:30 0000
102 Johnson 2012-05-2012 12:40:30 0000
102 Johnson 2012-05-2012 16:20:30 0000


Expected Output
---------------
SQL
TransDate   StartTime   End Time    "Count (No. Of Transaction)
Group by employee"
2012-05-2012    8:00    8:15    2   Raghu and Johnson
2012-05-2012    8:15    8:30    1   Raju
    8:30    8:45    2   Albert and Abdul




pls. give me the correct query for every 15 mins starting from "08:00" to "17:15"
Posted

1 solution

Hi Gani,

We can get the desired result set using the CTEs to get the filtered result and STUFF function on it to concatenate the Employee names.

Note : Your test data contains invalid Date (ie2012-5-2012..)

Go through the following code.

--Creating Temp table
SQL
CREATE TABLE [dbo].[Temp](
	[EmpId] [int] NULL,
	[EmpName] [varchar](30) NULL,
	[StartTime] [datetime] NULL
) 


-- Insert Data into Temp table

SQL
INSERT INTO [Temp]([EmpId],[EmpName],[StartTime])VALUES
 (101,'Abdul','2012-05-01 08:40:30.000'),
 (101,'Abdul','2012-05-01 09:40:30.000'),
 (101,'Abdul','2012-05-01 12:20:30.000'),
 (102,'Raju','2012-05-01 08:15:30.000'),
 (102,'Raju','2012-05-01 12:40:30.000'),
 (102,'Raju','2012-05-01 16:20:30.000'),
 (103,'Albert','2012-05-01 08:40:30.000'),
 (103,'Albert','2012-05-01 10:40:30.000'),
 (103,'Albert','2012-05-01 12:20:30.000'),
 (103,'Albert','2012-05-01 14:40:30.000'),
 (103,'Albert','2012-05-01 15:40:30.000'),
 (103,'Albert','2012-05-01 17:10:30.000'),
 (103,'Albert','2012-05-01 17:15:30.000'),
 (104,'Raghu','2012-05-01 08:00:30.000'),
 (104,'Raghu','2012-05-01 09:40:30.000'),
 (104,'Raghu','2012-05-01 12:20:30.000'),
 (102,'Johnson','2012-05-01 08:00:30.000'),
 (102,'Johnson','2012-05-01 12:40:30.000'),
 (102,'Johnson','2012-05-01 16:20:30.000')



SQL
DECLARE @FromDate DATETIME ='2012-05-01'; 


SQL
-- Common Table Expressions
WITH TimeSheetCte as (
SELECT DATEADD(hh,8,@FromDate) DateValue
UNION ALL select DATEADD(mi,15,DateValue)
FROM TimeSheetCte where DATEADD(mi,15,DateValue) < DATEADD(hh,17,@FromDate)
 )
,DateRanges AS(
SELECT DateValue StartTime,DATEADD(mi,15,DateValue) EndTime FROM  TimeSheetCte
)
,UsersWithTimeSlot As(
SELECT CONVERT(VARCHAR,DateRanges.StartTime,108) StatRange,CONVERT(VARCHAR,DateRanges.EndTime,108) EndRange
,ISNULL(Temp.EmpName,'-') EmpName,
ISNULL(CONVERT(VARCHAR,Temp.StartTime,108),'-') UserStartTime
,ISNULL(CONVERT(VARCHAR,Temp.StartTime,111),'-') StartDate
FROM  DateRanges LEFT JOIN Temp ON Temp.StartTime >= DateRanges.StartTime
AND Temp.StartTime <=DateRanges.EndTime
)

--Query to Display the desired result set
SELECT COUNT(*) [No.Transactions], Info.StartDate,StatRange As StartRange,EndRange
,REPLACE(STUFF( (
SELECT ','+ EmpName FROM  UsersWithTimeSlot
WHERE StatRange = Info.StatRange AND EndRange = Info.EndRange
FOR XML PATH('')),1,1,''
),',',' and ') EmployeeName
FROM (SELECT * fROM UsersWithTimeSlot WHERE EmpName <> '-' AND UserStartTime <> '-') Info
Group By Info.StatRange,Info.EndRange,Info.StartDate
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900