it will be very difficult to explain this in words so I will attempt small example to explain you what i want:
for example i have this SQL-Server table
+-------+-------+------+
| TestID| Start | End |
+-------+-------+------+
| 1 | DateA | DateB|
| 2 | DateA | DateB|
| 3 | DateA | DateB|
| 4 | DateA | |
+-------+-------+------+
What i want is this table:
$Rest = 1000
+-------+-------+------+----------+-----------+----------+
| TestID| Start | End | Testtime | Totaltime | Resttime |
+-------+-------+------+----------+-----------|----------|
| 1 | DateA | DateB| 214 | 214 | 786 |
| 2 | DateA | DateB| 100 | 314 | 686 |
| 3 | DateA | DateB| 200 | 514 | 486 |
| 4 | DateA | | | | |
+-------+-------+------+----------+-----------+----------+
i have the problem to understand what i have to combine
here is the code to get the first SQL-Server table
SELECT TestID, Start, End, DATEDIFF(hour, Start, End) AS Testtime
FROM Testresults
WHERE TesttableID = 1
Each Row has the TesttableID = 1
thx for help.
Edit : SQL Server Version: 9.0.5057
Edit : I get results but not the right one, they are switched in Totaltime and Resttime
SELECT t1.TestID,
t1.start,
t1.end,
t1.TesttableID,
DATEDIFF(hour,t1.start,t1.end) as Testtime,
(SELECT SUM(DATEDIFF(hour,t2.start,t2.end))
FROM Testresults t2
WHERE t2.TestID <= t1.TestsID AND t2.TesttableID = 1 ) AS Totaltime,
(SELECT 1000-SUM(DATEDIFF(hour,t2.start,t2.end))
FROM Testresults t2
WHERE t2.TestID <= t1.TestIDAND t2.TesttableID = 1 ) AS Resttime FROM Testresults t1 WHERE t1.TesttableID = 1
What i get is These Results, they switched..:
+-------+-------+------+----------+-----------+----------+
| TestID| Start | End | Testtime | Totaltime | Resttime |
+-------+-------+------+----------+-----------|----------|
| 1 | DateA | DateB| 214 | 514 | 486 |
| 2 | DateA | DateB| 100 | 300 | 700 |
| 3 | DateA | DateB| 200 | 200 | 800 |
| 4 | DateA | | | | |
+-------+-------+------+----------+-----------+----------+