tsql select count last 10 minutes from tale getdate

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
LVL 1
AFIF JABADOAsked:
Who is Participating?
 
AFIF JABADOAuthor Commented:
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
 
appariCommented:
;with mins as (Select 1 duration
union all select duration+1 from mins where duration<10)
select dateadd(mi, duration, getdate()) from mins
0
 
mbizupCommented:
Try this:

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

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Pratima PharandeCommented:
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
 
AFIF JABADOAuthor Commented:
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
 
Pratima PharandeCommented:
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
 
AFIF JABADOAuthor Commented:
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
 
AFIF JABADOAuthor Commented:
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
 
AFIF JABADOAuthor Commented:
i have test it and it work perfect :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.