Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 12 total
Thread Selecting single individuals
Tue, Aug 7 2007 4:14 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

The sql below is selecting individuals from a career table. At present I get one entry for each occurrence of an individual (ie if someone has been promoted there will be two entries). The rules for the database are that if someone leaves the company or is promoted then the date _Left is set.

What I want to achieve (preferably in a single statement - ie no scripts) is only one entry for each individual, and where there are multiple entries in the underlying table all I want is the one without an _Left date, or where they all have _Left dates, the most recent.

SELECT
_ID,
_Forename +' '+_Surname AS _Name,
W._JobTitle,
W._Left,
IF(W._Left IS NULL THEN TRUE ELSE FALSE) AS _StillThere,
W._fkSites,
_Surname+', '+_Forename AS _SortName
FROM Contacts
JOIN Career W ON W._fkContacts = _ID
WHERE
W._fkCompanies = :CompanyID

Roy Lambert
Wed, Aug 8 2007 3:40 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Roy,

<< What I want to achieve (preferably in a single statement - ie no scripts)
is only one entry for each individual, and where there are multiple entries
in the underlying table all I want is the one without an _Left date, or
where they all have _Left dates, the most recent. >>

Try this:

SELECT
_ID,
_Forename +' '+_Surname AS _Name,
MAX(W._Left) as Left,
W._JobTitle,
IF(MAX(W._Left) IS NULL THEN TRUE ELSE FALSE) AS _StillThere,
W._fkSites,
_Surname+', '+_Forename AS _SortName
FROM Contacts
JOIN Career W ON W._fkContacts = _ID
WHERE
W._fkCompanies = :CompanyID
GROUP BY 1, 2, 4, 6, 7;

--
Tim Young
Elevate Software
www.elevatesoft.com

Thu, Aug 9 2007 3:28 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Tim

Thanks but no prize, still get multiple entries for an individual

But what is

> GROUP BY 1, 2, 4, 6, 7;

The result set is used to populate a couple of grids - current employees and ex-employees so what I've done is order it by date left so the current staff are at the top and then whilst looping stuff the id into a sorted stringlist, if its already there don't put into the grid - it works and is reasonably fast.

Roy Lambert
Thu, Aug 9 2007 2:48 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Roy,

<< Thanks but no prize, still get multiple entries for an individual >>

Sorry, it's very hard to give an answer to these types of questions without
metadata and some understanding of what the tables look like in terms of the
rows and uniqueness of the various columns.

<< But what is

GROUP BY 1, 2, 4, 6, 7; >>

It's the way DBISAM allows you to use ordinal SELECT column positions in
ORDER BY or GROUP BY clauses.

--
Tim Young
Elevate Software
www.elevatesoft.com

Fri, Aug 10 2007 4:10 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Tim


>Sorry, it's very hard to give an answer to these types of questions without
>metadata and some understanding of what the tables look like in terms of the
>rows and uniqueness of the various columns.

I know and you do  very good job most of the time

Roy Lambert
Fri, Aug 10 2007 5:35 AMPermanent Link

Chris Erdal
Roy Lambert <roy.lambert@skynet.co.uk> wrote in
news:F457E6D7-A972-4865-8C5E-A36DBC863699@news.elevatesoft.com:

> Thanks but no prize, still get multiple entries for an individual
>
> But what is
>
>> GROUP BY 1, 2, 4, 6, 7;
>
> The result set is used to populate a couple of grids - current
> employees and ex-employees so what I've done is order it by date left
> so the current staff are at the top and then whilst looping stuff the
> id into a sorted stringlist, if its already there don't put into the
> grid - it works and is reasonably fast.
>
> Roy Lambert

Roy,

 What about Tim's SQL with

 GROUP BY 1
 ORDER BY 1, 2

IIUC this won't work in EDB but it might in DBISAM Smile

--
Chris
(XP-Pro + Delphi 7 Architect + DBISAM 4.25 build 4 + EDB 1.04 build 3)

Fri, Aug 10 2007 12:13 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Roy,

<< I know and you do very good job most of the time >>

Not really, but thanks for saying so anyways.  I'm better at the internals
than I am at actually using the SQL. Smiley

--
Tim Young
Elevate Software
www.elevatesoft.com

Fri, Aug 10 2007 1:42 PMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Tim


Yeah but at least you understand the gibberish - for me its still a foreign tounge - generally spoken after to much ale

Roy Lambert
Mon, Aug 13 2007 4:02 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Roy,

<< Yeah but at least you understand the gibberish - for me its still a
foreign tounge - generally spoken after to much ale >>

It's actually quite nice when you start using it a bit more, and it
translates to a lot of different development environments, etc.

--
Tim Young
Elevate Software
www.elevatesoft.com

Fri, Aug 17 2007 1:22 PMPermanent Link

John
Why don't you select into a memory table, and then select distinct from the memory table?

Just a thought from a novice... Smile

Roy Lambert <roy.lambert@skynet.co.uk> wrote:

The sql below is selecting individuals from a career table. At present I get one entry for
each occurrence of an individual (ie if someone has been promoted there will be two
entries). The rules for the database are that if someone leaves the company or is promoted
then the date _Left is set.

What I want to achieve (preferably in a single statement - ie no scripts) is only one
entry for each individual, and where there are multiple entries in the underlying table
all I want is the one without an _Left date, or where they all have _Left dates, the most
recent.

SELECT
_ID,
_Forename +' '+_Surname AS _Name,
W._JobTitle,
W._Left,
IF(W._Left IS NULL THEN TRUE ELSE FALSE) AS _StillThere,
W._fkSites,
_Surname+', '+_Forename AS _SortName
FROM Contacts
JOIN Career W ON W._fkContacts = _ID
WHERE
W._fkCompanies = :CompanyID

Roy Lambert
Page 1 of 2Next Page »
Jump to Page:  1 2
Image