473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Calculating Min/Max Timestamp Intervals For Inserted Rows

Hello,

Is it possible to determine the Min and Max timestamp intervals for records as they are inserted (SQL Server 2005)? The table accepts inserts only (no updates) and produces this output:


Expand|Select|Wrap|Line Numbers
  1. [ID] [TxNo]         [UserID]  [TimeStamp]
  2. 1    X1866246100    i22222    2011-03-07 05:59:37.863
  3. 2    X1884627200    K33333    2011-03-07 06:03:35.740
  4. 3    X1878488900    d44444    2011-03-07 06:04:10.520
  5. 4    X1885453100    K33333    2011-03-07 06:04:29.097
  6. 5    X1878488901    d44444    2011-03-07 06:04:45.503
  7.  
So, for user K33333 I would see the time interval between record ID 2 and ID 4. There are many records besides this for a day, but the table is truncated and archived each evening so there is always one day's worth of data only. So, for this user I would need the minimum and maximum intervals over the course of a day. I've been trying to put my head around it and have this so far, but it is only giving my an overall time lapse in minutes:

Expand|Select|Wrap|Line Numbers
  1.         Select Userid, max(TimeStamp) as mTime
  2.         into #Temp1
  3.         from CSDBBRDFC001.Hope.dbo.RealTimeProd
  4.         Where convert(varchar, TimeStamp, 112) = convert(varchar, getdate(), 112)
  5.         Group by UserID
  6.  
  7.         Select a.*, b.FirstName + ' ' + b.LastName as UserName, Datediff(minute, a.mTime, getdate()) as Lapse_Time_in_Minutes
  8.         into #Temp2
  9.         from #Temp1 a LEFT JOIN [User] b
  10.         On upper(a.UserID) = upper(b.ID)
  11.         Order by Datediff(minute, a.mTime, getdate()) Desc
  12.  
  13.         Select * from #Temp2
  14.  
So, as you can see, I'm getting the MAX overall interval, but not the intervals between each insert. Any help with this would be greatly appreciated.

Thanks,
Buster
Mar 8 '11 #1

✓ answered by gershwyn

You should be able to match up each interval between updates using a self-join, doing something like below (assuming the timestamps are in a table called intervals.)

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   intervals1.userID,
  3.   Max(intervals2.timestamp) AS StartTime,
  4.   intervals1.timestamp AS StopTime
  5. FROM intervals AS intervals1
  6. INNER JOIN intervals AS intervals2
  7. ON intervals1.userID = intervals2.userID
  8. WHERE intervals2.timestamp < intervals1.timestamp
  9. GROUP BY intervals1.userID, intervals1.timestamp
  10.  
This will ignore any entries that have the same timestamp (though it would have to be exactly the same, which seems unlikely to happen) and any userIDs that only have one record.

Now, from there you should be able to calculate the differences between each (are you looking for the difference in minutes?) and use the aggregate functions to pull out min and max.

Is that in the direction you were looking for?

8 8707
Rabbit
12,516 Expert Mod 8TB
If you need min and max, you can do both in one aggregate query.
Mar 8 '11 #2
Great, can you please show me an example of how you would do this in one aggregate query?
Mar 8 '11 #3
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT MIN(FieldName), Max(FieldName) FROM Table1
Mar 8 '11 #4
OK, I guess I'm not communicating very well. I am trying to get the MIN and MAX intervals between inserts for each user. I know I can get the OVERAL interval (time lapse), but I need the MIN and MAX between EACH insert. So, here would be the output I expect:

Expand|Select|Wrap|Line Numbers
  1. userid    tsmax                      tsmin                        tsdiff_min    tsdiff_max
  2. d44444    2011-03-07 06:08:29.097      2011-03-07 06:04:10.520    2            34
  3. i22222    2011-03-07 06:11:29.097      2011-03-07 05:59:37.863    3            23
  4. K33333    2011-03-07 06:11:29.097      2011-03-07 06:03:35.740    1            123
  5.  
Hope this makes sense.

Thanks,
Buster
Mar 8 '11 #5
Rabbit
12,516 Expert Mod 8TB
The sample data you have above doesn't match what's in your sample results. Plus you have two fields which look like calculated fields but you haven't specified the formula to calculate them.
Mar 8 '11 #6
gershwyn
122 100+
You should be able to match up each interval between updates using a self-join, doing something like below (assuming the timestamps are in a table called intervals.)

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   intervals1.userID,
  3.   Max(intervals2.timestamp) AS StartTime,
  4.   intervals1.timestamp AS StopTime
  5. FROM intervals AS intervals1
  6. INNER JOIN intervals AS intervals2
  7. ON intervals1.userID = intervals2.userID
  8. WHERE intervals2.timestamp < intervals1.timestamp
  9. GROUP BY intervals1.userID, intervals1.timestamp
  10.  
This will ignore any entries that have the same timestamp (though it would have to be exactly the same, which seems unlikely to happen) and any userIDs that only have one record.

Now, from there you should be able to calculate the differences between each (are you looking for the difference in minutes?) and use the aggregate functions to pull out min and max.

Is that in the direction you were looking for?
Mar 8 '11 #7
Whew, this is toughie, not only in terms of sctual development but trying to explain:

What you showed me gives the interval for the first and last inserts, but not all the inserts in-between. I'm thinking I might need a cursor-based solution, although I would always rather prefer a set-based solution. The data that is needed is there in the timestamp intervals. So, for instance here is a select from my table:

[code]
ID TxNo UserID TimeStamp
231228 X1879796000 c333333 2011-03-08 06:00:00.000
231229 X1885506800 c333333 2011-03-08 06:01:00.000
231230 X1887174400 c333333 2011-03-08 06:03:00.000
231231 X1890189900 c333333 2011-03-08 06:05:30.000
[\CODE]

Based on the output above, c333333 has the following inserts beginning at 6:00 am:

Expand|Select|Wrap|Line Numbers
  1. 231228    X1879796000    c333333    2011-03-08 06:00:00.000
  2. 231229    X1885506800    c333333    2011-03-08 06:01:00.000
  3.  
Would have the MIN interval at 1:00.

Then, the following inserts after that:

Expand|Select|Wrap|Line Numbers
  1. 231231    X1887174400    c333333    2011-03-08 06:03:00.000
  2. 231232    X1890189900    c333333    2011-03-08 06:05:30.000
  3.  
Would have the MAX interval of 2:30.

I am looking for the MIN and MAX timestamp intervals between each subsequent insert.

Hope this makes sense.

Thanks,
Buster
Mar 9 '11 #8
Actually Gershwyn, this is close. I've been able to get teh interval for each insert (MAX(DATEPART(minute,intervals2.timestamp)) - DATEPART(minute,intervals1.timestamp) AS Interval) and am trying to figure out how to get the MIN and MAX of this. I think I'm close and really appreciate your help.
Mar 9 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Boris Popov | last post by:
Hello pgsql-general, I'm trying to implement a table with rows that are automatically deleted when the session that inserted them disconnects, sort of like our own alternative to...
13
by: Dixie | last post by:
How do I, in VBA from an access form module, add 5 rows to the top of a spreadsheet called MySpreadsheet.xls. The worksheet is called MyWorksheet and there is already data in the first 5 rows. I...
1
by: Scott Tenorman | last post by:
The table I am reading has the potential to contain millions of rows. Can I run the select command multiple times to update the dataset without those inserted rows appearing as updated?
7
by: GaryB | last post by:
I have an untyped datatable that has financial numbers and controls that were populated by code (not a simple fill from a DA). Now I want to insert subtotals into it. I wrote a sub to do so that...
2
by: Zygo Blaxell | last post by:
I have a table with a few million rows of temperature data keyed by timestamp. I want to group these rows by timestamp intervals (e.g. every 32 seconds), compute aggregate functions on the...
1
by: Ruediger Herrmann | last post by:
Hello again, as I am new to PostgreSQL (great pice of softwork btw) this probably won't be the last question. I access PostgreSQL from Java via the PostgreSQL JDBC driver. I am currently...
1
by: whitecrow | last post by:
Hi All, I have a DataGrid that is a visual representation of a DataSet. It was created like so: dataset = new DataSet(); adapter.Fill(dataset); dataGridTable.DataSource = dataset.Tables;...
1
by: new | last post by:
I am building an effective dated file that shows results to date. In part 1 of the select I I process a larges set of input and genrate records for each date in the processing period for which...
1
by: mellow1986 | last post by:
hi please suggest an idea to view the rows recently inserted? Regards, Sathish
8
by: rohitbasu77 | last post by:
Hi friends, how to see all the inserted rows of a particular date of a table. regards rohit
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.