I have a table Events

Events containing

cardno,time,id,name--each id has a cardno



my requirement is every day each employee swipe the card
several times

i want to calculate first and last time of each card

the output should be

name 1 2
6 7
in out in out
holiday holiday
xxx 09:30 06:30 09:40 06:45
where 1,2...... are dates for example january 1,2, etc. 6
and 7 are saturday and sunday

how it is posssible



I have a table Events Events containing cardno,time,id,name--each id has a cardno my r..

Answer / Atul Chaudhary

To achieve this in SQL Server, you can use the ROW_NUMBER() function to number each row for each cardno and day, and then select the rows with the minimum and maximum time for each cardno. Here's an example query:
SELECT
id,
cardno,
MIN(time) AS 'in',
MAX(time) AS 'out',
DATEPART(dd, time) AS 'day',
DATENAME(dw, time) AS 'weekday'
FROM Events
WHERE time IS NOT NULL
GROUP BY id, cardno, weekday
ORDER BY cardno, day

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

what purpose does the model database serve? : Sql server database administration

1 Answers  


What is sleeping status in sql server?

1 Answers  


What is difference between joins and subqueries?

1 Answers  


What do you mean by sql server 2005 express management tools?

1 Answers  


Can a stored procedure call itself or recursive stored procedure? How much level sp nesting is possible?

1 Answers  


How will you collect the date from current date to last older 6 days date in sql server 2005

4 Answers  


Where are full-text indexes stored?

1 Answers  


What is the basic functions for master, msdb, tempdb databases?

2 Answers   CSC,


how do we find every fifth record in a table

14 Answers   Aspire, HCL,


why would you call update statistics? : Sql server database administration

1 Answers  


what is the cursor life span?

5 Answers   Evalueserve, HG,


How to add code to the existing article (using improve article)?

1 Answers  


Categories