SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On



rows group by second Expand / Collapse
Author
Message
Posted Friday, July 03, 2009 2:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 11:27 PM
Points: 42, Visits: 227
Dear all,

I have a table with TranIn, TranOut colums with datetime stored in YYYY-MM-DD HH:MM:SS:MSS format.
i need to count all the transactions group by every second up to 10 seconds. lets say

1 second -- 2 transactions
2 seconds -- 0 transactions
3 seconds -- 1 transactions
4 seconds -- 0 transactions
5 seconds -- 0 transactions
.
.
10seconds -- 1 transactions

create table #tmp(nos int,TranIn datetime, TranOut datetime)
insert into #tmp
select 1,'2009-07-02 05:41:02.123', '2009-07-02 05:41:02.567' union all
select 2,'2009-07-02 20:12:03.120', '2009-07-02 20:12:04.270' union all
select 3,'2009-07-02 14:47:54.060', '2009-07-02 14:47:59.776' union all
select 4,'2009-07-02 16:50:02.320', '2009-07-02 16:50:11.850' union all
select 5,'2009-07-02 13:55:33.135', '2009-07-02 13:55:45.000, union all
select 6,'2009-07-02 14:47:24.567', '2009-07-02 14:47:28.456' union all
select 7,'2009-07-02 19:12:12.345', '2009-07-02 19:12:27.450'

I ignore the millisecond anyway. Please help me with this query.

Post #747105
Posted Friday, July 03, 2009 3:13 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 3:45 PM
Points: 1,644, Visits: 2,285
Hi,
your data seems not to lead to the expected result:

SELECT 
DATEDIFF(ss,TranIn,TranOut) AS SECONDS,
COUNT(*) AS CNT
FROM #tmp
GROUP BY DATEDIFF(ss,TranIn,Tranout)

/* result
SECONDS CNT
0 1
1 1
4 1
5 1
9 1
12 1
15 1
*/

Reason for that is the first row just has a little more than 0.4 seconds, so you'll get the count "1" for 0 seconds.
If you have difficulties to modify the code shown above to meet your requirements, let us know.




Lutz

A pessimist is an optimist with experience.

How to get fast answers to your question
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs
Post #747113
Posted Friday, July 03, 2009 3:28 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:19 PM
Points: 1,261, Visits: 1,501
First, I want to thank you for posting the DDL and DML for the test data.

Now, I have a question. What second are you looking for... elapsed, TranIn, or TranOut?

If your looking for elapsed, try this:
;with CTE AS
( -- get the elapsed time in seconds for each transaction, with a count by # of seconds
select sec = DateDiff(second, TranIn, TranOut), qty = count(*)
from #tmp
group by DateDiff(second, TranIn, TranOut)
)
, Tally AS -- if you already have a tally table, you can omit it here and use it below
( -- build a tally table of numbers between 1 and 10
select Number from master.dbo.spt_values where [Type] = 'P' and number between 1 and 10
)
-- now bring them all together
select [Second] = Tally.Number,
[Quantity of Transactions] = case when CTE.sec is null then 0 else CTE.qty end,
[Description] = convert(varchar(2), Tally.Number) +
' second' +
case when Tally.Number = 1 then '' else 's' end +
' -- ' +
convert(varchar(10), case when CTE.sec is null then 0 else CTE.qty end) +
' transactions'
from Tally
LEFT OUTER JOIN CTE ON Tally.Number = CTE.sec
where Tally.Number between 1 and 10 -- this line is only needed if using your own tally table.



Wayne
For better assistance in answering your questions, click here
For performance problems, please read this.
Post #747117
Posted Friday, July 03, 2009 3:46 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 11:27 PM
Points: 42, Visits: 227
Thank you Lutz and Wayne. Yes I am looking for the diffrence in seconds in TranIn and TranOut columns
Post #747119
Posted Friday, July 03, 2009 3:53 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 3:45 PM
Points: 1,644, Visits: 2,285
jymoorthy (7/3/2009)
Thank you Lutz and Wayne. Yes I am looking for the difference in seconds in TranIn and TranOut columns

What rule for rounding values would you use?
The example you provided has a value of some 0.4 seconds. Would you count this as "1 sec" or as "0 sec"? Would you always round to the next full second or using financial rounding method?

@Wayne: good catch! Didn't notice the OP wanted a list with all seconds, including NULL values... I was a little more focused on the rounding issue...




Lutz

A pessimist is an optimist with experience.

How to get fast answers to your question
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs
Post #747121
Posted Friday, July 03, 2009 4:00 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 11:27 PM
Points: 42, Visits: 227
Would you count this as "1 sec" or as "0 sec"?


I want to count in 0 as i specified in my post, " ignore the milliseconds" its difficult to calculate in milliseconds and convert into seconds. that will be more complicated to calculate...

Thanks again.
Post #747123
Posted Friday, July 03, 2009 4:16 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 3:45 PM
Points: 1,644, Visits: 2,285
I'm sorry for not asking all questions I have at once...
Your example has two rows with 12 rsp 15 seconds.
How would you handle those?
a) add it up in the "10 seconds group"
b) ignore the values (that's actually happening when you use Wayne's code)
c) show it as separate rows

Note: if sample data and expected result actually match it makes it a lot easier to test the solutions. Just a note, not offensive in any kind!




Lutz

A pessimist is an optimist with experience.

How to get fast answers to your question
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs
Post #747127
Posted Friday, July 03, 2009 7:48 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:19 PM
Points: 1,261, Visits: 1,501
lmu92 (7/3/2009)
I'm sorry for not asking all questions I have at once...
Your example has two rows with 12 rsp 15 seconds.
How would you handle those?
a) add it up in the "10 seconds group"
b) ignore the values (that's actually happening when you use Wayne's code)
c) show it as separate rows

Note: if sample data and expected result actually match it makes it a lot easier to test the solutions. Just a note, not offensive in any kind!


Lutz,
I did it the way I did because of this original comment:
i need to count all the transactions group by every second up to 10 seconds.

I interpreted that to mean just 1-10, ignore anything greater.

Of course, if the poster would have supplied expected results from the supplied test data then all of these questions would have been answered, and we wouldn't have to work on assumptions and ask a bunch of clarifying questions.


Wayne
For better assistance in answering your questions, click here
For performance problems, please read this.
Post #747136
Posted Saturday, July 04, 2009 12:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 11:27 PM
Points: 42, Visits: 227
Thanks Lutz and Wayne. I am sorry for not giving all the required details at once.

I am looking for the results upto 10 seconds only. (even, if there is a new requirement, i can add it later by myself). In 99% cases the transaction would finish within 1 second. after 15 seconds, its timed out . I might have made a mistake in my test data by adding 15 seoncds treansaction. i am sorry for that.

Here i am preparing a delayed transaction list, which will show as i required in my original post. Thanks for your time.

Post #747154
« Prev Topic | Next Topic »


Permissions Expand / Collapse