Using FileMaker's SQL UNION to combine data

At Pause On Error I was scheduled to lead a discussion about using the SQL API in FileMaker Pro but nixed it to make room for other presenters. Instead I talked about FileMaker MVC with Ernest and data modeling with David Graham of BitTailor. Because of the popularity of the topic, however, I thought I'd address a few of the questions I received in hallways and at the bar here.

One data modeling question that arose is how to deal with generalizations, such as storing contact data for a person. We can choose to store email addresses and phone numbers in the same table or different tables. Storing them in the same table makes it more difficult to provide distinct interfaces tailored for entering data or a specific type. Storing them in different tables means that you can't readily present them in a concise view when necessary. I've often suggested using a hybrid approach, that uses a generalized supertype table to store common information, and specialized subtype tables to store information specific to each contact type. This approach solves each problem, but does so through increased complexity.

On a FileMaker mailing list a question arose from someone taking the separate tables approach and presented an alternative way of addressing the problem.

Given that we have three tables:

  • CONTACT with fields: IDContactPK, FirstName, LastName
  • PHONENUMBER with fields: IDPhoneNumbersPK, IDContactFK, PhoneNumber
  • EMAILADDRESS with fields: IDEmailAddressesPK, IDContactFK, EmailAddress

How can we get data back like so:

ContactID, FirstName, LastName, ContactInfo
1, John, Doe, john@example.com
1, John, Doe, jdoe@example.com
1, John, Doe, 407-555-1212

Of the many approaches available, they were specifically looking for a SQL solution. Recognizing that the data they wanted to combine was in separate tables, and in particular the ContactInfo column was derived from two different source tables, use of a UNION seemed to make the most sense. Here's the SQL that makes it possible.

SELECT CONTACT.IDContactPK, CONTACT.FirstName, CONTACT.LastName, EMAILADDRESS.EmailAddress AS ContactInfo
FROM CONTACT JOIN EMAILADDRESS ON CONTACT.IDContactPK = EMAILADDRESS.IDContactFK WHERE CONTACT.IDContactPK = '1'
UNION
SELECT CONTACT.IDContactPK, CONTACT.FirstName, CONTACT.LastName, PHONENUMBER.PhoneNumber AS ContactInfo
FROM CONTACT JOIN PHONENUMBER ON CONTACT.IDContactPK = PHONENUMBER.IDContactFK WHERE CONTACT.IDContactPK = '1'

Combined with some CSS formatting and presented in a web viewer, we solve the presentation problem when we want to combine the data from both sources.

One of FileMaker Pro's major advantages is the ability to rename elements without breaking your application logic. When you use the SQL API in FileMaker Pro you sacrifice this benefit. Fortunately with FileMaker Pro 10 we have a handy way of making our SQL code just as robust as native FMP functionality. A future post will show one method for doing so.

Thanks a lot for this

Thanks a lot for this information! it is very useful Corn

Still not sold on internal SQL

The two main issues that I've had which prevent me from fully embracing internal SQL functionality are: 1) it requires a plug-in, and 2) no support for outer joins.

FileMaker relationships are inherently outer join in nature, but FileMaker only supports inner joins in SQL. In the example you show above, you wouldn't see any contacts who don't have both a phone and an e-mail addresses. I'm wrestling with this in a project now and I hope that I'm missing some simple workaround.

Insightful as always. Thanks Corn!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p><div> <br><img>
  • Lines and paragraphs break automatically.

More information about formatting options

Verification
This question is for testing whether you are a human visitor and to prevent automated spam submissions.