Solved

VB.NET How to select the earliest and latest record of a set of rows based on timestamp and ID Sql server

Posted on 2008-10-08
2
381 Views
Last Modified: 2012-05-05
I have many rows being returned which contain a autonumber, qty, timestamp and foreignKeyID fields...what I need is to get the totalqty of all the rows, and the time difference between the latest timestamp and the earliest timestamp all based on the same foreign key ID

Based on these variables I need to see the totalqty/time difference ...which will give the # of units counted per minute.

I am using VB.Net and Sql Server 2005... I am just a bit fuzzy on the actual grabbing of the earliest & latest timestamp... I could use the AutoNumber field ( Max / Min)  of the table based on the foreign key...but is there a better way...

Any help or insight would be much appreciated...
0
Comment
Question by:nomar2
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 48 total points
ID: 22674870
From SQL couldn't you just do:

SELECT COUNT(*) AS Qty, MAX(timestamp) as MaxTime, Min(TimeStamp) as MinTime, ForeignKey
FROM table
GROUP BY ForeignKey

0
 

Author Comment

by:nomar2
ID: 22682090
Perfect....

I changed the code a little but this is what I came up with based on your suggestions...

SELECT     SUM(iquantity) / DATEDIFF(minute, MAX(editeddate), MIN(editedDate)) AS OverCount
FROM         tInvDetail
WHERE     (iInvoiceID = 33202)

This returns the # of items counted per minute
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

12 Experts available now in Live!

Get 1:1 Help Now