Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sql count Expand / Collapse
Author
Message
Posted Wednesday, May 1, 2013 11:50 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 20, 2013 12:23 PM
Points: 2, Visits: 6

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
Post #1448696
Posted Thursday, May 2, 2013 12:12 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 7, 2013 9:20 PM
Points: 13, Visits: 80
option 1 :
create a CURSOR.For each row in the table, read the columns into varibales and check for the range and then update a counter,later update the counter before jumping on to the next row.

option2:
use CASE statement to update the counter Column which whill be something like:
update
set =
case when [time1 & time2&time3 in range] then 3
when [time1 & time2 in range] OR [time 1 & time3 in range] OR [time2 &3 in range] then 2,... like wise ..
Post #1448700
Posted Thursday, May 2, 2013 5:44 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 1:30 AM
Points: 2,386, Visits: 7,622
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.



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1448771
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse