Subject | RE: [firebird-support] last of the SQL |
---|---|
Author | Svein Erling Tysvær |
Post date | 2009-09-30T14:51:16Z |
Firebird works on data sets and in a set there is no default understanding of FIRST or LAST. So there are several possible answers to your question:
This gets the last address alphabetically sorted according to the current collation:
select staff.name, max(staff.address) as address from staff
group by staff.name order by staff.name
This gets the last address depending on MoveDate, or the highest primary key if a person moved twice on the same date (OK, that's unlikely, but in different situations it may be good to know how to randomly pick one in case of equality):
select s1.name, s1.address from staff s1
where not exists(select * from staff s2
where s2.name = s1.name
and (s2.MoveDate > s1.MoveDate
or (s2.MoveDate = s1.MoveDate
and s2.PrimaryKeyField > s1.PrimaryKeyField)))
HTH,
Set
-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Ismael L. Donis Garc?a
Sent: 24. september 2009 09:51
To: firebird-support@yahoogroups.com
Subject: [firebird-support] last of the SQL
What show substitutes the show last of the SQL?
Example: select staff.name, last(staff.address) as address from staff group
by staff.name order by staff.name
Regards
=========
¦¦ ISMAEL ¦¦
=========
This gets the last address alphabetically sorted according to the current collation:
select staff.name, max(staff.address) as address from staff
group by staff.name order by staff.name
This gets the last address depending on MoveDate, or the highest primary key if a person moved twice on the same date (OK, that's unlikely, but in different situations it may be good to know how to randomly pick one in case of equality):
select s1.name, s1.address from staff s1
where not exists(select * from staff s2
where s2.name = s1.name
and (s2.MoveDate > s1.MoveDate
or (s2.MoveDate = s1.MoveDate
and s2.PrimaryKeyField > s1.PrimaryKeyField)))
HTH,
Set
-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Ismael L. Donis Garc?a
Sent: 24. september 2009 09:51
To: firebird-support@yahoogroups.com
Subject: [firebird-support] last of the SQL
What show substitutes the show last of the SQL?
Example: select staff.name, last(staff.address) as address from staff group
by staff.name order by staff.name
Regards
=========
¦¦ ISMAEL ¦¦
=========