Solved

tsql select count last 10 minutes from tale getdate

Posted on 2013-01-15
9
446 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
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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now