Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a records like below in a table, my problem is I need to get ??? values from using database query or C# code. Kindly advise?

What is "???" column? Previous Out[minus]Current In

XML
<table>
  Date         In          Out        ???
10/26/2012  16:29:26    16:29:29
10/26/2012  16:31:21    16:31:25    0:01:52
10/26/2012  16:49:40    16:49:48    0:18:15
</table>
Posted
Updated 26-Oct-12 6:18am
v2
Comments
Bhushan Shah1988 26-Oct-12 9:05am    
Please brief properly.
Tell us what you want?

1) You need to figure out the pattern that exists. It is really quite simple.
2) You need to figure out how to calculate based on the pattern found in step 1.
3) Write sql code to generate that column's data.
 
Share this answer
 
Pretty easy to do-
1. Get TOP 2 records from SQL

SQL
SELECT TOP 2 Out,In FROM tbl ORDER BY Out DESC


2. Get the values of In column in 2nd row and Out column in 1st row.
3. Get the difference between 2 values.
 
Share this answer
 
SELECT CONVERT(varchar,(Out - in), 108) FROM Table
 
Share this answer
 
hi

use Time(7) in the design of database.
 
Share this answer
 
SQL
SELECT Date
      ,In
      ,Out
      ,CONVERT(varchar, In - (SELECT MAX (OUT) FROM Table RD WHERE RD.OUT< MD.OUT AND RD.Date = MD.Date GROUP BY Date ), 108 ) [???]
FROM Table MD


This will give you expected output
 
Share this answer
 
Comments
abdulsafran 28-Oct-12 12:52pm    
Hi Guys, thanks for your answers, but still my problem is not solved, I need that once enter the In and Out, I need to calculate previous record Out[minus]Current record IN to the ??? column. That's all. Kindly check and advise.
abdulsafran 28-Oct-12 12:55pm    
??? Column = previous record Out Time - Current record In Time
abdulsafran 28-Oct-12 12:56pm    
How to check previous record?
abdulsafran 28-Oct-12 13:32pm    
Hi ganeshdpatil, what is RD and MD in your query? Thanks!
ganeshdpatil 28-Oct-12 14:40pm    
Hi abdul RD and MD are alias for table. I was using table in two queries that's why to understand properly I gave alias. You just replace Table with your Table name and you will get desired output. I have tested this query.

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