473,406 Members | 2,467 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.

How to obtain just the last record grouped by

Sorry for my english
I have a table that contains data of career about the person (staff)

like this ...
EMATR EANID EMEID EGIID ecc. .. ecc. .. ecc. .. ecc. ..
1 1999 12 31 2002 12 31 1
1 2003 1 1 0 0 0 3
2 1999 12 31 2002 12 31 1
2 2003 1 1 0 0 0 3
4 1999 12 31 2000 7 31 1
5 1999 12 31 2001 1 31 1
6 1999 12 31 2002 12 31 1
6 2003 1 1 0 0 0 3
7 1999 12 31 2002 12 31 1

where
EMATR is a not unique key (person ID)
EANID, EMEID, EGIID (assembled) are the date of the last advance of
career

I want extract from the table below only the actual position,
therefore a view that return this rows

EMATR EANID EMEID EGIID ecc. .. ecc. .. ecc. .. ecc. ..
1 2003 1 1 0 0 0 3
2 2003 1 1 0 0 0 3
4 1999 12 31 2000 7 31 1
5 1999 12 31 2001 1 31 1
6 2003 1 1 0 0 0 3
7 1999 12 31 2002 12 31 1

I hope of to have been explain
Thank you from Maximiliano (italy)
RE-Sorry for my english
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 20 '05 #1
1 1672
On Wed, 1 Dec 2004 18:07:43 +0000 (UTC), Massimiliano Malloni wrote:
Sorry for my english
I have a table that contains data of career about the person (staff)

like this ...
EMATR EANID EMEID EGIID ecc. .. ecc. .. ecc. .. ecc. ..
1 1999 12 31 2002 12 31 1
1 2003 1 1 0 0 0 3
2 1999 12 31 2002 12 31 1
2 2003 1 1 0 0 0 3
4 1999 12 31 2000 7 31 1
5 1999 12 31 2001 1 31 1
6 1999 12 31 2002 12 31 1
6 2003 1 1 0 0 0 3
7 1999 12 31 2002 12 31 1

where
EMATR is a not unique key (person ID)
EANID, EMEID, EGIID (assembled) are the date of the last advance of
career

I want extract from the table below only the actual position,
therefore a view that return this rows

EMATR EANID EMEID EGIID ecc. .. ecc. .. ecc. .. ecc. ..
1 2003 1 1 0 0 0 3
2 2003 1 1 0 0 0 3
4 1999 12 31 2000 7 31 1
5 1999 12 31 2001 1 31 1
6 2003 1 1 0 0 0 3
7 1999 12 31 2002 12 31 1

I hope of to have been explain
Thank you from Maximiliano (italy)
RE-Sorry for my english


Hi Maximiliano,

I advise you to change this table design. Dates should be stored in one
column, using the datetime format. Any other format tends to become a real
pain when you need to query the dates or do logic with them. A much better
design would be (using English columns names - all Italian words I know
are for food <g>)

CREATE TABLE Maximiliano
(PersonID int NOT NULL
,StartDate datetime NOT NULL
,EndDate datetime DEFAULT NULL
,other columns .....
,PRIMARY KEY (PersonID, StartDate)
,UNIQUE (PersonID, EndDate)
,FOREIGN KEY (PersonID) REFERENCES Persons
,CHECK (StartDate < EndDate)
)

Your data would know look like this:

PersonID StartDate EndDate other columns
1 1999-12-31 2002-12-31 1
1 2003-01-01 NULL 3
2 1999-12-31 2002-12-31 1
2 2003-01-01 NULL 3
4 1999-12-31 2000-07-31 1
5 1999-12-31 2001-01-31 1
6 1999-12-31 2002-12-31 1
6 2003-01-01 NULL 3
7 1999-12-31 2002-12-31 1
The query to return the most recent row for each person:

SELECT PersonID, StartDate, EndDate, other columns
FROM Maximiliano AS a
WHERE NOT EXISTS
(SELECT *
FROM Maximiliano AS b
WHERE b.PersonID = a.PersonID
AND b.StartDate > a.StartDate)

If you need to do it with your current table design, replace a.StartDate
with the following complicated expression:

DATEADD(dd, a.EGIID - 1, DATEADD(mm, a.EMEID - 1, DATEADD(yy, a.EANID -
2000, '20000101')))

and do the same for b.StartDate (this time prefixing the three columns
with b of course)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2

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

Similar topics

5
by: Alistair | last post by:
hi peoples and masterful gurus (a little bit of grovelling never hurt anyone...apart from the sore knees) I'm after a little bit of advice on how to do something. basically a history of the...
3
by: Eliu Montoya | last post by:
Hello everybody, Does anyone know an easy way to insert a record (without a value for the primary key because it is a sequence) and at the same time obtain the value of primary key that was...
1
by: Rick B | last post by:
I am attempting to get the row number of a record after doing a "array = ds.tbl_name.select("Row = 'lookingfor'")" below is a screen shot of the array contents. What is needed to obtain the row...
0
by: Spencer Lee | last post by:
I have encountered a very unique problem (for me): I created a data access page, with 3 levels of groups. I put the file on the web folder (intranet), and it seems like everything is working...
6
by: MLH | last post by:
I have a table - tblCorrespondence. It houses records of correspondence initiated here in our office. Records are appended whenever it is recognized that some type of outbound letter needs to be...
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
2
by: schapopa | last post by:
Hi, I want to create query where I could group records by quarters, and get the last record in each group. e.g Create Table MyTable ( Value , date )
0
by: wilko | last post by:
Hi, Does anybody have any design ideas for flattening multiple measurement records into fact table columns. Our current design imports the measurement records into a staging table and then uses...
5
by: Davo1977 | last post by:
Analysing text files to obtain statistics on their content You are to write a Perl program that analyses text files to obtain statistics on their content. The program should operate as follows: ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...
0
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...

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.