Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My table tblEmployeeScan(EMPLOYEE_ID varchar(7), ScannedTime)
Everyday each employee scans 2 times, 1 time or 0 time
Now I want to crate store procedure

SQL
Create proc spMonthScanReport
@MONTH int,
@YEAR int

which returns a mothly report like below

EMPLOYEE    	  ID  	1		   	2		3	4.......	31  (All days in month)
0000001           SCN1	1/1/2014 06:00:00		2/1/2014 06:10:00
0000001           SCN2	1/1/2014 15:00:00		2/1/2014 14:00:00
0000002           SCN1 	1/1/2014 07:00:00		2/1/2014 06:10:00
0000002           SCN2	1/1/2014 15:00:00		2/1/2014 14:00:00
0000003           SCN1 	 NULL			        2/1/2014 06:10:00
0000003           SCN2 	1/1/2014 15:00:00		NULL

Please support me
Posted
Updated 7-Oct-14 19:45pm
v2
Comments
Maciej Los 8-Oct-14 1:54am    
Please, be more specific and provide more details. We can't read in your mind or direct from your screen. Use "Improve question" widget to update question and use proper formatting.
Do you want to count how many times each user 'scans' each day?
DungVanNguyen 8-Oct-14 2:31am    
No, MacieLos, We don't count, SCN1 stands for the first time person checkin, SCN1 stands for the last time person check out.

Please, read my comment to the question.

You need dynamic pivot which generates as many column as many days is in a month:
Dynamic PIVOT in Sql Server[^]
pivots with dynamic columns in sql server 2005/[^]
Script to create dynamic PIVOT queries in SQL Server[^]

Use SearchBox on the right-top corner of this site to find more examples.
 
Share this answer
 
Create Procedure spMonthScanReport (@Minth int, @Years int)
as
select EMP_ID, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31] from
(
select EMP_ID, Day(SCANNEDTIME) AS DAY1, SCANNEDTIME from tblEmployeeScan where MONTH(SCANNEDTIME)=@Minth and YEAR(SCANNEDTIME)=@Years
) AA pivot
(
MAX(SCANNEDTIME) for DAY1 in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) BB
 
Share this answer
 

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