467,185 Members | 1,140 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,185 developers. It's quick & easy.

SQL how to

I have two tables :
CREATE TABLE animals
(
BHID BIGINT NOT NULL,
CONTROLLER INTEGER NOT NULL,
LOCATION INTEGER NOT NULL,
....
)
CREATE UNIQUE INDEX PRIMARY_A ON ANIMALS(BHID);

ALTER TABLE animals
ADD CONSTRAINT PRIMARY PRIMARY KEY
(BHID);
CREATE TABLE owners
(
bhid BIGINT NOT NULL,
entity_id INTEGER NOT NULL,
starting DATE NOT NULL,
percent DECIMAL(7, 4) NOT NULL DEFAULT 100,
ending DATE
)
DATA CAPTURE NONE;

CREATE UNIQUE INDEX primary_o ON owners(bhid, entity_id, starting);

ALTER TABLE owners
ADD CONSTRAINT primary PRIMARY KEY
(bhid, entity_id, starting);

I want a table with a single row for each animal along with one owner,
independent of the number of owners, 0, 1, or more. In the case that
more than one owner for a particular animal exists I would like the
owner with the largest percentage, if there is one. When the percentages
are equal (50/50., etc.), any of the owners will do. If there was not
the requirement that there only be one row per animal a LEFT OUTER JOIN
ON animals.bhid=owners.bhid would suffice.

I am at a loss as how to do this without a table returning function,
which I am loathe to write because of the performance hit.

Nov 12 '05 #1
  • viewed: 1097
Share:
1 Reply
Ian
Robert Stearns wrote:
I want a table with a single row for each animal along with one owner,
independent of the number of owners, 0, 1, or more. In the case that
more than one owner for a particular animal exists I would like the
owner with the largest percentage, if there is one. When the percentages
are equal (50/50., etc.), any of the owners will do. If there was not
the requirement that there only be one row per animal a LEFT OUTER JOIN
ON animals.bhid=owners.bhid would suffice.


Can you do something like:

select
bhid
,location
,entity_id
,percent
from (
select
a.bhid
,a.location
,o.entity_id
,o.percent
,rownumber() over (partition by o.bhid
order by o.percent desc) as owner_rank
from
animals a
left outer join owners o
on a.bhid=o.bhid
) as x
where
owner_rank = 1;


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.