Question

I have joined a new job where I am required to use FileMaker (and gradually transition systems to other databases). I have been a DB Admin of a MS SQL Server database for ~2 years, and I am very well versed in PL/SQL and T-SQL. I am trying to pan my SQL knowledge to FMP using the ExecuteSQL functionaloty, and I'm kinda running into a lot of small pains :)

I have 2 tables: Movies and Genres. The relevant columns are:

Movies(MovieId, MovieName, GenreId, Rating)

Genres(GenreId, GenreName) I'm trying to find the movie with the highest rating in each genre. The SQL query for this would be:

SELECT M.MovieName

    FROM Movies M INNER JOIN Genres G ON M.GenreId=G.GenreId

    WHERE M.Rating=

   (

         SELECT MAX(Rating) FROM Movies WHERE GenreId = M.GenreId

   ) 

I translated this as best as I could to an ExecuteSQL query:

ExecuteSQL ("

SELECT M::MovieName FROM Movies M INNER JOIN Genres G ON M::GenreId=G::GenreId

WHERE M::Rating =

(SELECT MAX(M2::Rating) FROM Movies M2 WHERE M2::GenreId = M::GenreId)

"; "" ; "")

I set the field type to Text and also ensured values are not stored. But all I see are '?' marks.

What am I doing incorrectly here? I'm sorry if it's something really stupid, but I'm new to FMP and any suggestions would be appreciated.

Thank you!

-- Ram

UPDATE: Solution and the thought process it took to get there:

Thanks to everyone that helped me solve the problem. You guys made me realize that traditional SQL thought process does not exactly pan to FMP, and when I probed around, what I realized is that to best use SQL knowledge in FMP, I should be considering each column independently and not think of the entire result set when I write a query. This would mean that for my current functionality, the JOIN is no longer necessary. The JOIN was to bring in the GenreName, which is a different column that FMP automatically maps. I just needed to remove the JOIN, and it works perfectly. TL;DR: The thought process context should be the current column, not the entire expected result set.

Once again, thank you @MissJack, @Chuck (how did you even get that username?), @pft221 and @michael.hor257k

Was it helpful?

Solution

I've found that FileMaker is very particular in its formatting of queries using the ExecuteSQL function. In many cases, standard SQL syntax will work fine, but in some cases you have to make some slight (but important) tweaks.

I can see two things here that might be causing the problem...

ExecuteSQL ("

SELECT M::MovieName FROM Movies M INNER JOIN Genres G ON
M::GenreId=G::GenreId

WHERE M::Rating =

(SELECT MAX(M2::Rating) FROM Movies M2 WHERE M2::GenreId = M::GenreId)

"; "" ; "")
  1. You can't use the standard FMP table::field format inside the query.

    Within the quotes inside the ExecuteSQL function, you should follow the SQL format of table.column. So M::MovieName should be M.MovieName.

  2. I don't see an AS anywhere in your code.

    In order to create an alias, you must state it explicitly. For example, in your FROM, it should be Movies AS M.

I think if you fix those two things, it should probably work. However, I've had some trouble with JOINs myself, as my primary experience is with FMP, and I'm only just now becoming more familiar with SQL syntax.

Because it's incredibly hard to debug SQL in FMP, the best advice I can give you here is to start small. Begin with a very basic query, and once you're sure that's working, gradually add more complicated elements one at a time until you encounter the dreaded ?.

There's a number of great posts on FileMaker Hacks all about ExecuteSQL:

  • Since you're already familiar with SQL, I'd start with this one: The Missing FM 12 ExecuteSQL Reference. There's a link to a PDF of the entire article if you scroll down to the bottom of the post.

  • I was going to recommend a few more specific articles (like the series on Robust Coding, or Dynamic Parameters), but since I'm new here and I can't include more than 2 links, just go to FileMaker Hacks and search for "ExecuteSQL". You'll find a number of useful posts.

NB If you're using FMP Advanced, the Data Viewer is a great tool for testing SQL. But beware: complex queries on large databases can sometimes send it into fits and freeze the program.

OTHER TIPS

The first thing to keep in mind when working with FileMaker and ExecuteSQL() is the difference between tables and table occurrences. This is a concept that's somewhat unique to FileMaker. Succinctly, tables store the data, but table occurrences define the context of that data. Table occurrences are what you're seeing in FileMaker's relationship graph, and the ExecuteSQL() function needs to reference the table occurrences in its query.

I agree with MissJack regarding the need to start small in building the SQL statement and use the Data Viewer in FileMaker Pro Advanced, but there's one more recommendation I can offer, which is to use SeedCode's SQL Explorer. It does require the adding of table occurrences and fields to duplicate the naming in your existing solution, but this is pretty easy to do and the file they offer includes a wizard for building the SQL query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top