473,322 Members | 1,671 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,322 software developers and data experts.

Help with a Join - join only first or max?

csk

Hopefully someone will have some ideas on how to do this. I'm
officially stumped.

I have two entities to join. Simplified descriptions follow:

The first has names and addresses (vwPersonAddress) keyed by PersonID
(it is actually a view on two tables, but it works exactly as I want
it to, so all good there).

vwPersonAddress
--------------------
personID (PK)
addrType (PK)
fname
lname
addr1
city
st
entity 2 is a table that lists licenses and companies and is 1 to many
with vwPersonAddress (a person can have multiple licenses).

vwLicCo
---------
personID (PK)
licenseID (PK)
licNum
CompanyName (can be null)

Now the odd part, I want to join them - but only get the first or max
company name from entity 2 for a given person. In other words, the
customer doesn't care WHAT company name I put in the output, as long
as it's only 1 (doesn't create extra records) and belongs to that
person. OH - and to keep it interesting, not every person will have a
company name in that second table...

I've played around quite a bit with all the join types and not found a
way to say, "Join these two tables, outer join on table 2 but if there
IS a match, only give me one"... that 'give me only one' bit is why I
was looking at max() by the way.

What I'm doing right now is running my output query on
vwPersonAddress, then as I create a data file (programmatically) doing
another query FOR EACH ROW on vwLicCo and just grabbing the first
companyName, if any. As you might guess, performance is less than
stellar. ;-)

Any thoughts?
Jul 23 '05 #1
3 1437
On Fri, 10 Dec 2004 12:24:16 -0700, csk wrote:
Hopefully someone will have some ideas on how to do this. I'm
officially stumped.

I have two entities to join. Simplified descriptions follow:
Hi csk,

In the future, please post actual CREATE TABLE scripts, including all
constraints. And add in some sample data (as INSERT statements) plus
expected output, to clarify what you mean and to enable easy testing.

See www.aspfaq.com/5006
The first has names and addresses (vwPersonAddress) keyed by PersonID
(it is actually a view on two tables, but it works exactly as I want
it to, so all good there).

vwPersonAddress
--------------------
personID (PK)
addrType (PK)
fname
lname
addr1
city
st
I'm confused. Is personID the PK (as you state in the narrative) or is
there a compound key of personID + addrType (as suggested in the list
above)? This is where a CREATE TABLE statement with constraints would have
been better!

entity 2 is a table that lists licenses and companies and is 1 to many
with vwPersonAddress (a person can have multiple licenses).

vwLicCo
---------
personID (PK)
licenseID (PK)
licNum
CompanyName (can be null)
If vwPersonAddress has personID/addrType as compound key, then this table
needs addrType as well to make a 1 to many relationship.

I guess that actually, both vwLicCo vwPersonAddress are related one to
many with a third table, Persons. They are not directly related. (A
licence belongs to a person, not to a person's home address or work
address).

Now the odd part, I want to join them - but only get the first or max
company name from entity 2 for a given person. In other words, the
customer doesn't care WHAT company name I put in the output, as long
as it's only 1 (doesn't create extra records) and belongs to that
person. OH - and to keep it interesting, not every person will have a
company name in that second table...
Do you still want to list the person (with company name NULL) or should
the person be completely omitted? This is where sample data and expected
output would have been better!

I've played around quite a bit with all the join types and not found a
way to say, "Join these two tables, outer join on table 2 but if there
IS a match, only give me one"... that 'give me only one' bit is why I
was looking at max() by the way.


I'll have to start making assumptions and wild guesses and I can't run any
tests, but I'll give it a shot.

If you want only the companyname, try

SELECT A.fname, A.lname, MAX(L.CompanyName)
FROM vwPersonAddress AS A
INNER JOIN vwLicCo AS L
ON L.personID = A.personID
GROUP BY A.personID, A.fname, A.lname

If you need the licencenumber as well:

SELECT A.fname, A.lname, L.CompanyName, L.licNum
FROM vwPersonAddress AS A
INNER JOIN vwLicCo AS L
ON L.personID = A.personID
WHERE NOT EXISTS
(SELECT *
FROM vwLicCo AS L2
WHERE L2.personID = L.personID
AND L2.CompanyName > L.CompanyName)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
csk
On Fri, 10 Dec 2004 20:44:49 +0100, Hugo Kornelis
<hugo@pe_NO_rFact.in_SPAM_fo> wrote:
On Fri, 10 Dec 2004 12:24:16 -0700, csk wrote:
Hopefully someone will have some ideas on how to do this. I'm
officially stumped.

I have two entities to join. Simplified descriptions follow:


Hi csk,

In the future, please post actual CREATE TABLE scripts, including all
constraints. And add in some sample data (as INSERT statements) plus
expected output, to clarify what you mean and to enable easy testing.

See www.aspfaq.com/5006


My apologies, I should've looked for the FAQ first.
I was attempting to distill a much more complex problem into something
a bit more simple.

In retrospect, my example wasn't close enough to what I want to do,
but you've given me an idea to mull over. If it doesn't work I'll be
back with more pertinent/useful info.

Thank you!

Jul 23 '05 #3
>> Any thoughts? <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.

You also need to read ISO-11179 so you will stop putting those silly
"vw-' prefixes on VIEW names. What does a Volkswagen have to do with
this table??

Why is a license id totally different from a mere license number?
Surely you know better than to use IDENTITY or some other nonrelational
exposed physical locator in a real table.

CREATE TABLE PersonnelLocations
(ssn CHAR(9) NOT NULL -- or other industry standard
REFERENCES Personnel (ssn)
ON UPDATE CASCADE
ON DELETE CASCADE,
san INTEGER NOT NULL, san = Standard Address Number
REFERENCES Addresses (san)
ON UPDATE CASCADE,
...
PRIMARY KEY (ssn, san) );

CREATE TABLE LicenseHolders
(ssn CHAR(9) NOT NULL,
license_nbr INTEGER NOT NULL,
PRIMARY KEY (ssn, license_nbr)
company_name VARCHAR (35) DEFAULT '{{not a company}}' NOT NULL
...);
I want to join them - but only get the first or max company name

from entity 2 for a given person. In other words, the customer doesn't
care WHAT company name I put in the output, as long as it's only 1
(doesn't create extra records [sic]) and belongs to that
person. OH - and to keep it interesting, not every person will have a
company name in that second table... <<

You did not tell us what you want as output. Names? License numbers?
Also, rows are not records -- huge differences. Maybe this?

SELECT P.last_name, P.first_name, H.license_nbr, MAX(H.company_name)
FROM Personnel AS P
LEFT OUTER JOIN
LicenseHolders AS H
ON P.ssn = H.ssn
GROUP BY P.last_name, P.first_name, H.license_nbr;

Jul 23 '05 #4

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

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
4
by: trint | last post by:
Ok, This script is something I wrote for bringing up a report in reporting services and it is really slow...Is their any problems with it or is their better syntax to speed it up and still provide...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
4
by: Mark | last post by:
the Following bit of code doesn't work. It seems to respond to the second, starting with 'add iif statement for Good Practice', but not to the first, starting 'add iif statement for archived' ...
2
by: Terry Olsen | last post by:
I need to get information from 3 tables in an MDB file. I need all the columns in the first table. I need 2 columns in the 2nd table where it's primary key matches a column in the first table....
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: theintrepidfox | last post by:
Dear Group I'd be grateful if you can provide me with a hint for the following: Fields Table Contact ContactID Firstname Lastname Fields Table ContactMethod
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.