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
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 |
what purpose does the model database serve? : Sql server database administration
What is sleeping status in sql server?
What is difference between joins and subqueries?
What do you mean by sql server 2005 express management tools?
Can a stored procedure call itself or recursive stored procedure? How much level sp nesting is possible?
How will you collect the date from current date to last older 6 days date in sql server 2005
Where are full-text indexes stored?
What is the basic functions for master, msdb, tempdb databases?
how do we find every fifth record in a table
why would you call update statistics? : Sql server database administration
what is the cursor life span?
How to add code to the existing article (using improve article)?
Oracle (3253)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)