leostrut (5/1/2013)
I have a table like the following:Name Time1 Time2 Time3
leo 06:03 0615 08:13
John 06:17 10:03 11:05
Can anyone tell me how to use sql to count the time between 06:00 and 06:30 and make it like the following:
Name Time1 Time2 Time3 Total
leo 06:03 0615 08:13 2
John 06:17 10:03 11:05 1
thanks in advanced
So, what you're saying is that you have the following sample data: -
IF object_id('tempdb..#testEnvironment') IS NOT NULL
BEGIN
DROP TABLE #testEnvironment;
END;
SELECT Name, Time1, Time2, Time3
INTO #testEnvironment
FROM (VALUES('leo', '06:03', '06:15', '08:13'),
('John', '06:17', '10:03', '11:05')
)a(Name, Time1, Time2, Time3);
Name Time1 Time2 Time3
---- ----- ----- -----
leo 06:03 06:15 08:13
John 06:17 10:03 11:05
And from that, your expected result is: -
Name Time1 Time2 Time3 Total
---- ----- ----- ----- -----------
leo 06:03 06:15 08:13 2
John 06:17 10:03 11:05 1
In all honesty, I can't really see a relationship between "Total" and the "Time" columns, unless your "Total" is just a count of the number of values that are between 6:00 and 6:59 😛
Can you provide more details as to why you expect the results that you've stated please?
Also, please note how I've laid out the sample data that you provided. Now, anyone that uses SQL Server 2008 (which is the version for this specific forum section) can copy and paste that in to SSMS and have a copy of your sample data to play with. This allows people to focus on your question rather than on setting up test environments so results in more people being willing to help you.