Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sql count


sql count

Author
Message
leostrut
leostrut
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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
rohit.anshu
rohit.anshu
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
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 ..
Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2596 Visits: 8437
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 :-P

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.


Forever 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


Craig Wilkinson - Software Engineer
LinkedIn
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search