I need a query to count the number of user instances for each distinct timestamp in SQL Server 2005 -


here data table:

userid  timestamp ------------------------ user1   2017-08-15 17:00 user1   2017-08-15 17:00 user1   2017-08-15 17:00 user2   2017-08-15 17:00 user2   2017-08-15 17:00 user3   2017-08-15 17:00 user1   2017-08-15 18:00 user1   2017-08-15 18:00 user2   2017-08-15 18:00 user2   2017-08-15 18:00 user2   2017-08-15 18:00 user3   2017-08-15 18:00 

i want result this:

userid    countoftimestamp ----------------------------- user1     3_2017-08-15 17:00 user2     2_2017-08-15 17:00 user3     1_2017-08-15 17:00 user1     2_2017-08-15 18:00 

and on.

you may notice used varchar(16) truncate timestamp down minutes (excluding seconds , milliseconds)

example

select userid       ,countoftimestamp = cast(sum(1) varchar(25))+'_'+convert(varchar(16),timestamp,20)   yourtable  group userid,convert(varchar(16),timestamp,20) 

returns

enter image description here


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -