473,396 Members | 2,147 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,396 software developers and data experts.

Get previous record

Please see DDL and INSERT statements below.

Let's say that some process throws out the second row, where the
Clocktime = '02/01/2005 12:34'

Without the use of a cursor, how can I retrieve the PREVIOUS value for
that employee? Pseudo SQL might be something like:

SELECT
*
FROM
tblTest
WHERE
fldCLocktime = THE-ONE-IMMEDIATELY-BEFORE '02/01/2005 12:34'
AND
fldEmployeeID = 1

TIA

Edward

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblTest]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
drop table [dbo].[tblTest]
GO

CREATE TABLE [dbo].[tblTest] (
[fldTestID] [int] IDENTITY (1, 1) NOT NULL ,
[fldEmployeeID] [int] NULL ,
[fldClocktime] [datetime] NULL ,
) ON [PRIMARY]
GO

INSERT INTO tblTest
(fldEmployeeID,
fldClocktime)
VALUES
(1,
'01/01/2005 12:34')

INSERT INTO tblTest
(fldEmployeeID,
fldClocktime)
VALUES
(1,
'02/01/2005 12:34')

INSERT INTO tblTest
(fldEmployeeID,
fldClocktime)
VALUES
(1,
'03/01/2005 12:34')

Dec 5 '05 #1
3 6785
MC
Here si one way of doing it.

SELECT *
FROM tblTest
WHERE
fldCLocktime = (select max(ClockTime) from tblTest where clocktime <
'02/01/2005 12:34' and fldEmployeeID = 1)
AND
fldEmployeeID = 1

MC

<te********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Please see DDL and INSERT statements below.

Let's say that some process throws out the second row, where the
Clocktime = '02/01/2005 12:34'

Without the use of a cursor, how can I retrieve the PREVIOUS value for
that employee? Pseudo SQL might be something like:

SELECT
*
FROM
tblTest
WHERE
fldCLocktime = THE-ONE-IMMEDIATELY-BEFORE '02/01/2005 12:34'
AND
fldEmployeeID = 1

TIA

Edward

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblTest]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
drop table [dbo].[tblTest]
GO

CREATE TABLE [dbo].[tblTest] (
[fldTestID] [int] IDENTITY (1, 1) NOT NULL ,
[fldEmployeeID] [int] NULL ,
[fldClocktime] [datetime] NULL ,
) ON [PRIMARY]
GO

INSERT INTO tblTest
(fldEmployeeID,
fldClocktime)
VALUES
(1,
'01/01/2005 12:34')

INSERT INTO tblTest
(fldEmployeeID,
fldClocktime)
VALUES
(1,
'02/01/2005 12:34')

INSERT INTO tblTest
(fldEmployeeID,
fldClocktime)
VALUES
(1,
'03/01/2005 12:34')

Dec 5 '05 #2

MC wrote:
Here si one way of doing it.

SELECT *
FROM tblTest
WHERE
fldCLocktime = (select max(ClockTime) from tblTest where clocktime <
'02/01/2005 12:34' and fldEmployeeID = 1)
AND
fldEmployeeID = 1


Hi MC

Your response put me onto another way that doesn't use a sub-query:

SELECT TOP 1 *
FROM tblTest WHERE fldClocktime <
'02/01/2005 12:34' and fldEmployeeID = 1
ORDER BY fldClocktime DESC

Both methods work fine - thanks for your help.

Edward

Dec 5 '05 #3
MC
Yes, it works if you're running the query for one employee. I thought you
needed something for all employees or something like that. Oh well, as long
as you managed to solve the problem :).

MC
<te********@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

MC wrote:
Here si one way of doing it.

SELECT *
FROM tblTest
WHERE
fldCLocktime = (select max(ClockTime) from tblTest where clocktime <
'02/01/2005 12:34' and fldEmployeeID = 1)
AND
fldEmployeeID = 1


Hi MC

Your response put me onto another way that doesn't use a sub-query:

SELECT TOP 1 *
FROM tblTest WHERE fldClocktime <
'02/01/2005 12:34' and fldEmployeeID = 1
ORDER BY fldClocktime DESC

Both methods work fine - thanks for your help.

Edward

Dec 5 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working...
5
by: Derek Cooper | last post by:
I hope you can help me. I posted this in the microsoft sql server newsgroup a few days ago and got no response so I thought I'd try here. If I can provide any clarification I'll be glad to do so....
5
by: Steve Strik | last post by:
My Problem: I have created a database here at work that is exhibiting some very strange behaviour. Essentially the database is structured in a manner where one table is a master record table...
1
by: allyn44 | last post by:
Hello, I have a table that has null fields that need to be filled in with the value of the previous record (example below) id date 1 2/2/02 2 3 4/4/02 4
0
by: KelHemp | last post by:
Greetings, I've been using this site for lots of access help in the past, and it's very helpful! I have a new complexity for you all. Reworking a form to record 70-80 years of oil production on...
8
by: Jeff | last post by:
A client wants a press of the Enter key in a field on a continuous form to grab the value of that field from the previous record. But if they have typed a value and then hit Enter it shouldn't. ...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
7
by: thread | last post by:
Hi all i need to build progression calculator for a record and for this i need to have the possiblity to get the information for the previous record. is it posible to do it or i will need to use...
1
nev
by: nev | last post by:
Good day to all! I was trying to search the internet about this problem but I still can't seem to stumble upon an answer. I am using a BindingSource on my program and everything is ok except that...
16
by: zoeb | last post by:
Hi, I am a complete novice to Access VBA and looking for some help to select a record. I am looking to perform an operation on the previous record - i.e. adding a new blank field, and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.