473,772 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating Min/Max Timestamp Intervals For Inserted Rows

5 New Member
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
8 8754
Rabbit
12,516 Recognized Expert Moderator MVP
If you need min and max, you can do both in one aggregate query.
Mar 8 '11 #2
BusterKarmul
5 New Member
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 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. SELECT MIN(FieldName), Max(FieldName) FROM Table1
Mar 8 '11 #4
BusterKarmul
5 New Member
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 Recognized Expert Moderator MVP
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 New Member
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
BusterKarmul
5 New Member
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
BusterKarmul
5 New Member
Actually Gershwyn, this is close. I've been able to get teh interval for each insert (MAX(DATEPART(m inute,intervals 2.timestamp)) - DATEPART(minute ,intervals1.tim estamp) 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
3229
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 pg_stat_activity. Is it possible and what approach should I be trying to achieve such a thing? Thanks! --
13
7782
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 simply need to insert 5 blank rows at the top and move the rest of the data down so it starts at row 6. dixie
1
1451
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
3927
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 uses InsertAt(myrow,i). When I trace through the sub it is doing the insertat and the value of i is correct. But when I bind my dataset to a datagrid all of my inserted rows are at the end? code is below. What am I doing wrong? thanks, G
2
720
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 columns, and ultimately feed the result into a graph-drawing web thingy. I'm trying a few different ways to get what seems to be the same data, and seeing some odd behavior from the query planner. The table looks like this:
1
2600
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 building a little framework that provides basic CRUD operations by mapping class properties to database columns. All my primary keys are artificial and built by sequences (datat type bigserial). Now I need to (re-)read the row that was...
1
1715
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; dataGridTable.TableStyles.Clear(); DataGridTableStyle tableStyle = new DataGridTableStyle(); dataGridTable.TableStyles.Add(tableStyle);
1
2329
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 there has been activity In part II I then want to select the most recent effective dated record (if any and whether it existed before this run or was generated by an earlier insert) and sum it with the record for this date
1
1565
by: mellow1986 | last post by:
hi please suggest an idea to view the rows recently inserted? Regards, Sathish
8
11178
by: rohitbasu77 | last post by:
Hi friends, how to see all the inserted rows of a particular date of a table. regards rohit
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.