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: -
[ID] [TxNo] [UserID] [TimeStamp]
-
1 X1866246100 i22222 2011-03-07 05:59:37.863
-
2 X1884627200 K33333 2011-03-07 06:03:35.740
-
3 X1878488900 d44444 2011-03-07 06:04:10.520
-
4 X1885453100 K33333 2011-03-07 06:04:29.097
-
5 X1878488901 d44444 2011-03-07 06:04:45.503
-
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: -
Select Userid, max(TimeStamp) as mTime
-
into #Temp1
-
from CSDBBRDFC001.Hope.dbo.RealTimeProd
-
Where convert(varchar, TimeStamp, 112) = convert(varchar, getdate(), 112)
-
Group by UserID
-
-
Select a.*, b.FirstName + ' ' + b.LastName as UserName, Datediff(minute, a.mTime, getdate()) as Lapse_Time_in_Minutes
-
into #Temp2
-
from #Temp1 a LEFT JOIN [User] b
-
On upper(a.UserID) = upper(b.ID)
-
Order by Datediff(minute, a.mTime, getdate()) Desc
-
-
Select * from #Temp2
-
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
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.) - SELECT
-
intervals1.userID,
-
Max(intervals2.timestamp) AS StartTime,
-
intervals1.timestamp AS StopTime
-
FROM intervals AS intervals1
-
INNER JOIN intervals AS intervals2
-
ON intervals1.userID = intervals2.userID
-
WHERE intervals2.timestamp < intervals1.timestamp
-
GROUP BY intervals1.userID, intervals1.timestamp
-
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
If you need min and max, you can do both in one aggregate query.
Great, can you please show me an example of how you would do this in one aggregate query?
- SELECT MIN(FieldName), Max(FieldName) FROM Table1
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: -
userid tsmax tsmin tsdiff_min tsdiff_max
-
d44444 2011-03-07 06:08:29.097 2011-03-07 06:04:10.520 2 34
-
i22222 2011-03-07 06:11:29.097 2011-03-07 05:59:37.863 3 23
-
K33333 2011-03-07 06:11:29.097 2011-03-07 06:03:35.740 1 123
-
Hope this makes sense.
Thanks,
Buster
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.
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.) - SELECT
-
intervals1.userID,
-
Max(intervals2.timestamp) AS StartTime,
-
intervals1.timestamp AS StopTime
-
FROM intervals AS intervals1
-
INNER JOIN intervals AS intervals2
-
ON intervals1.userID = intervals2.userID
-
WHERE intervals2.timestamp < intervals1.timestamp
-
GROUP BY intervals1.userID, intervals1.timestamp
-
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?
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: -
231228 X1879796000 c333333 2011-03-08 06:00:00.000
-
231229 X1885506800 c333333 2011-03-08 06:01:00.000
-
Would have the MIN interval at 1:00.
Then, the following inserts after that: -
231231 X1887174400 c333333 2011-03-08 06:03:00.000
-
231232 X1890189900 c333333 2011-03-08 06:05:30.000
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
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;...
|
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...
|
by: mellow1986 |
last post by:
hi
please suggest an idea to view the rows recently inserted?
Regards,
Sathish
|
by: rohitbasu77 |
last post by:
Hi friends,
how to see all the inserted rows of a particular date of a table.
regards
rohit
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |