Solved

tsql select count last 10 minutes from tale getdate

Posted on 2013-01-15
9
455 Views
Last Modified: 2013-01-28
i have a table contain records and column datetimein

i want to select the last 10 min from getdate()

for example let say  the current date is  2013-01-15 1:09 PM
i want to get the number of records in each time s

 2013-01-15 1:09 PM numberofsms
 2013-01-15 1:08 PM
 2013-01-15 1:07 PM
 2013-01-15 1:06 PM
 2013-01-15 1:05 PM
 2013-01-15 1:04 PM
 2013-01-15 1:03 PM
 2013-01-15 1:02 PM
 2013-01-15 1:01 PM
 2013-01-15 1:00 PM
0
Comment
Question by:afifosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38777636
;with mins as (Select 1 duration
union all select duration+1 from mins where duration<10)
select dateadd(mi, duration, getdate()) from mins
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38777642
Try this:

SELECT *
FROM YourTable
WHERE datediff("n",YourField, getdate() ) < 10

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38777650
try this

SELECT count(*),convert(varchar(16),[CREATED_TIMESTAMP],121)
FROM tablename
WHERE [CREATED_TIMESTAMP] >= dateadd(minute,-10,getdate()) and
 [CREATED_TIMESTAMP] <= getdate()
AND Created_By ='FaxActivityMonitor'
Group by convert(varchar(16),[CREATED_TIMESTAMP],121)
Order by convert(varchar(16),[CREATED_TIMESTAMP],121) Desc
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Author Comment

by:afifosh
ID: 38777668
pratima_mcs this is your result


(No column name)      (No column name)
1      2013-01-15 13:21
2      2013-01-15 13:18
2      2013-01-15 13:16
1      2013-01-15 13:12


my table format

datetimein      msisdn      reqport      message
2011-01-09 07:52:54.907      506193110      84448      Mails1 has failed!
2011-01-09 07:57:56.423      506193110      84448      Mails1 has failed!
2011-01-09 08:02:57.547      506193110      84448      Mails1 has failed!

the old solution not compatible with my table
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38777694
what exact result you need , my solution provides count(*) for the in last 10 min , for each min

what change you required in this ?
0
 
LVL 1

Author Comment

by:afifosh
ID: 38777731
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 0 THEN 1 ELSE 0 END) AS  'Midnight',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 1 THEN 1 ELSE 0 END) AS  '1:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 2 THEN 1 ELSE 0 END) AS  '2:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 3 THEN 1 ELSE 0 END) AS  '3:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 4 THEN 1 ELSE 0 END) AS  '4:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 5 THEN 1 ELSE 0 END) AS  '5:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 6 THEN 1 ELSE 0 END) AS  '6:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 7 THEN 1 ELSE 0 END) AS  '7:00 AM',
SUM(CASE WHEN DATEPART(hh, c.datetimein) = 8 THEN 1 ELSE 0 END) AS  '8:00 AM',

this one count record in each hours i want to count in each 5 minutes in each hours

i should care maybe i didn't recive any record in 1:44 min so i should get this record and with value 0

for example

getdate (hh) divide hours in minutes slot  and count the number of record group by date days hh mm  

and i can change the interval of minute slot 5 or 10 or 1 min..

and i want to select referring to the getdate()
0
 
LVL 1

Author Comment

by:afifosh
ID: 38777985
select dateadd(minute, 5 + (datediff(minute, 0, datetimein) / 5) * 5, 0),
       COUNT(*)
  FROM [radiomix].[dbo].[chat]
group by dateadd(minute, 5 + (datediff(minute, 0, datetimein) / 5) * 5, 0)
order by dateadd(minute, 5 + (datediff(minute, 0, datetimein) / 5) * 5, 0) desc

the problem in the result may i don't have any new record during 5 min.. i want to genrate the date with time.. ???
result :

(No column name)      (No column name)
2013-01-15 15:10:00.000      1
2013-01-15 14:45:00.000      2
2013-01-15 14:35:00.000      5
2013-01-15 14:30:00.000      1
2013-01-15 14:25:00.000      3
2013-01-15 14:20:00.000      1
2013-01-15 14:15:00.000      3
2013-01-15 14:10:00.000      1
2013-01-15 14:05:00.000      2
2013-01-15 13:55:00.000      2
2013-01-15 13:50:00.000      1
2013-01-15 13:45:00.000      2


maybe i will union all or cross join with generated

DECLARE
  @StartTime datetime = '2013-01-15 00:00:00',
  @EndTime datetime = '2013-01-16 00:00:00',
  @Interval int = 300;
SELECT
  StartTime = DATEADD(SECOND, (number - 1) * @Interval, @StartTime),
  EndTime   = DATEADD(SECOND, (number - 0) * @Interval, @StartTime),
  Duration  = @Interval
FROM master..spt_values
WHERE type = 'P'
  AND number BETWEEN 1 AND DATEDIFF(SECOND, @StartTime, @Endtime) / @Interval

UNION ALL

SELECT
  DATEADD(SECOND, -Duration, EndTime),
  EndTime,
  Duration
FROM (
  SELECT
    EndTime = @EndTime,
    Duration = DATEDIFF(SECOND, @StartTime, @Endtime) % @Interval
) s
WHERE Duration > 0

order by StartTime desc
0
 
LVL 1

Accepted Solution

by:
afifosh earned 0 total points
ID: 38813580
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER VIEW [dbo].[d]
AS
SELECT     DATEADD(SECOND, (number - 1) * 60, DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0)) AS startd
FROM         master.dbo.spt_values
WHERE     (type = 'P') AND (DATEPART(hh, DATEADD(SECOND, (number - 1) * 60, DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0))) BETWEEN DATEPART(hh, 
                      DATEADD(hh, - 3, GETDATE())) AND DATEPART(hh, GETDATE())) AND (DAY(DATEADD(SECOND, (number - 1) * 60, DATEADD(d, DATEDIFF(d, 0, 
                      GETDATE()), 0))) = DAY(GETDATE())) AND (DATEADD(SECOND, (number - 1) * 60, DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0)) < GETDATE())


GO

Open in new window




ALTER VIEW [dbo].[View_1]
AS
WITH dd AS (SELECT     DATEADD(mi, DATEDIFF(mi, 0, datetimein) + 1, 0) AS d, COUNT(1) AS sms
                            FROM         dbo.chat
                            WHERE     (datetimein BETWEEN DATEADD(minute, - 300, GETDATE()) AND GETDATE())
                            GROUP BY DATEADD(minute, DATEDIFF(mi, 0, datetimein) + 1, 0))
    SELECT     dbo.d.startd, ISNULL(dd_1.sms, 0) AS smscount
     FROM         dbo.d LEFT OUTER JOIN
                            dd AS dd_1 ON dd_1.d = dbo.d.startd

Open in new window

0
 
LVL 1

Author Closing Comment

by:afifosh
ID: 38826229
i have test it and it work perfect :D
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question