Click here to Skip to main content
15,891,839 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi!

Can you help me to create CheckIn / Out report In Crystal Reports

sql Table Structure is

UserIDDateTimeCheckingType
8881/1/2011 08:30:001
8891/1/2011 08:32:001
8851/1/2011 08:35:001
8881/1/2011 16:30:002
8891/1/2011 16:42:002
8851/1/2011 16:55:002


Report Should be

UserIDDateCheckIn TimeCheckOut Time
8881/1/201108:3016:30
8891/1/201108:3216:42
8851/1/201108:3516:55


--- EDIT --
Edited once again. In revision 2 i show expected output in wrong way. See revision 1 (oryginal post).
Posted
Updated 31-Jul-11 2:25am
v3
Comments
Maciej Los 30-Jul-11 9:10am    
Edited - using html tags for tables - losmac

I would use an inner join[^] for that.

SQL
SELECT tblA.[DateTime] AS [CheckIn Time], tblB.[DateTime] AS [CheckOut Time] FROM [YourTable] tblA INNER JOIN [YourTable] tblB ON tblA.UserID = tblB.UserID AND tblA.CheckingType <> tblB.CheckingType
 
Share this answer
 
v2
Comments
Maciej Los 31-Jul-11 4:58am    
I'm not sure what you mean. I think the records are in the one table and i'm really sure Shanuka Fernando should use pivot tables to view results.
This is not solution.
walterhevedeich 31-Jul-11 5:10am    
I have provided this as I'm not familiar with the PIVOT solution. I'm pretty sure though that you can do a join on the same table. You might want to post the PIVOT solution for OP's reference. I'm sure it will be helpful as well.
Maciej Los 31-Jul-11 6:35am    
OK, i'll try to show solution using JOIN on the same table and using PIVOT table.
Shanuka Fernando 1-Aug-11 2:02am    
Thanks my friend !!! can you help me to create a View out of this
walterhevedeich 1-Aug-11 2:23am    
I believe the other solution was correct. Your question was edited by someone and have not done it correctly. Fortunately, he managed to modify it again to the correct result.
OK, as i wrote, i try to show how to do the same using two different methods.

My database name: A_TEST
My data in the table Checkings looks like:

UserIDchDateTimeCheckingType
8882011-01-01 08:30:00.0001
8892011-01-01 08:32:00.0001
8852011-01-01 08:35:00.0001
8882011-01-01 16:30:00.0002
8892011-01-01 00:42:00.0002
8852011-01-01 00:55:00.0002
8882011-01-02 08:30:00.0001
8892011-01-02 08:32:00.0001
8852011-01-02 08:35:00.0001
8882011-01-02 16:30:00.0002
8892011-01-02 00:42:00.0002
8852011-01-02 00:55:00.0002


--- METHOD 1: JOIN ---
SQL
USE [A_TEST];

DECLARE @sqry NVARCHAR(2000)
DECLARE @fqry NVARCHAR(2000)
DECLARE @mqry NVARCHAR(2000)

--create subquery 
SET @sqry = 'SELECT UserID, CONVERT(DATETIME,CONVERT(NVARCHAR, YEAR([chDateTime])) + ''-'' + CONVERT(NVARCHAR, MONTH([chDateTime])) + ''-'' + CONVERT(NVARCHAR, DAY([chDateTime]))) AS [chDate], RIGHT([chDateTime],8) AS CheckInTime ' +
			'FROM [dbo].[Checkings]' +
			'WHERE CheckingType = 1'
--EXECUTE(@sqry)

--create subquery 
SET @fqry = 'SELECT UserID, CONVERT(DATETIME,CONVERT(NVARCHAR, YEAR([chDateTime])) + ''-'' + CONVERT(NVARCHAR, MONTH([chDateTime])) + ''-'' + CONVERT(NVARCHAR, DAY([chDateTime]))) AS [chDate], RIGHT([chDateTime],8) AS CheckOutTime ' +
			'FROM [dbo].[Checkings]' +
			'WHERE CheckingType = 2'
--EXECUTE(@fqry)

--show results from two subqueries on UserID for equals chDate
SET @mqry = 'SELECT tblA.[UserID], tblA.[chDate], tblA.[CheckInTime], tblB.[CheckOutTime] ' +
			'FROM (' + @sqry + ') AS tblA RIGHT OUTER JOIN (' + @fqry + ') AS tblB ON tblA.[UserID] = tblB.[UserID] ' +
			'WHERE tblA.chDate = tblB.chDate ' +
			'ORDER BY tblA.chDate'
EXECUTE(@mqry)



--- METHOD 2: PIVOT ---
SQL
USE [A_TEST];

DECLARE @sqry NVARCHAR(2000)
DECLARE @mqry NVARCHAR(2000)
DECLARE @cols NVARCHAR(2000)

-- STEP 1 - get pivoted columns names:
-- returns:
--   [1] - for CheckInTime
--   [2] - for CheckOutTime
SET @cols = STUFF((SELECT DISTINCT '],[' + CONVERT(NVARCHAR, [CheckingType])
					FROM [dbo].[Checkings]
					ORDER BY '],[' + CONVERT(NVARCHAR, [CheckingType])
			FOR XML PATH('')), 1, 2, '') + ']'
--Use: 'SELECT @cols AS [my_cols];' to see returned data

-- STEP 2 - get data for pivot table
SET @sqry = 'SELECT [UserID], CONVERT(DATETIME,CONVERT(NVARCHAR, YEAR([chDateTime])) + ''-'' + CONVERT(NVARCHAR, MONTH([chDateTime])) + ''-'' + CONVERT(NVARCHAR, DAY([chDateTime]))) AS [chDate],  RIGHT([chDateTime],8) AS chTime, CheckingType ' + 
			'FROM [dbo].[Checkings]'
--EXECUTE(@sqry)

-- STEP 3 - show results in the pivot table
-- 
SET @mqry = 'SELECT [UserId], [chDate], ' + @cols + ' ' + 
			'FROM (' + @sqry + ') AS DT ' +
			'PIVOT (MAX(DT.[chTime]) FOR DT.[CheckingType] IN ( ' + @cols  + ' )) AS PT ' +
			'ORDER BY PT.[chDate]'
EXECUTE(@mqry)



--- RESULTS: ---
results are the same in both examples, differ only column headers
--- JOIN ---

UserIDchDateCheckInTimeCheckOutTime
8852011-01-01 00:00:00.0008:35AM4:55PM
8882011-01-01 00:00:00.0008:30AM4:35PM
8892011-01-01 00:00:00.0008:32AM4:42PM
8852011-01-02 00:00:00.0008:35AM4:55PM
8882011-01-02 00:00:00.0008:30AM4:30PM
8892011-01-03 00:00:00.0008:32AM4:42PM


--- PIVOT ---

UserIDchDate12
8852011-01-01 00:00:00.0008:35AM4:55PM
8882011-01-01 00:00:00.0008:30AM4:35PM
8892011-01-01 00:00:00.0008:32AM4:42PM
8852011-01-02 00:00:00.0008:35AM4:55PM
8882011-01-02 00:00:00.0008:30AM4:30PM
8892011-01-03 00:00:00.0008:32AM4:42PM



I don't know how to use it in Crystal Reports but i'm sure the solution will be helpful. Probably, we need to create a stored procedure (i.e.: pvtCheckings) and then use it as a source of Crystal Report (how? i don't know!):
SQL
USE [A_TEST];
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE pvtCheckings 
AS
BEGIN
	SET NOCOUNT ON;
	DECLARE @sqry NVARCHAR(2000)
	DECLARE @mqry NVARCHAR(2000)
	DECLARE @cols NVARCHAR(2000)
	SET @cols = STUFF((SELECT DISTINCT '],[' + CONVERT(NVARCHAR, [CheckingType])
						FROM [dbo].[Checkings]
						ORDER BY '],[' + CONVERT(NVARCHAR, [CheckingType])
				FOR XML PATH('')), 1, 2, '') + ']'
	SET @sqry = 'SELECT [UserID], CONVERT(DATETIME,CONVERT(NVARCHAR, YEAR([chDateTime])) + ''-'' + CONVERT(NVARCHAR, MONTH([chDateTime])) + ''-'' + CONVERT(NVARCHAR, DAY([chDateTime]))) AS [chDate],  RIGHT([chDateTime],8) AS chTime, CheckingType ' + 
				'FROM [dbo].[Checkings]'
	SET @mqry = 'SELECT [UserId], [chDate], ' + @cols + ' ' + 
				'FROM (' + @sqry + ') AS DT ' +
				'PIVOT (MAX(DT.[chTime]) FOR DT.[CheckingType] IN ( ' + @cols  + ' )) AS PT ' +
				'ORDER BY PT.[chDate]'
	EXECUTE(@mqry)
END
GO

USAGE (in MS SQL Server Management Studio):
SQL
DECLARE @RC int
EXECUTE @RC = [A_TEST].[dbo].[pvtCheckings]

In both examples we need to extract part of date and time, becouse DateTime field includes date and time. Until time part is differ we can't equal results.
 
Share this answer
 
v4
Comments
walterhevedeich 31-Jul-11 8:01am    
Your solution provides the output you have provided. But it is totally different from what OP wants. Read the question again.

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