Solved

SQL query that uses joined tables - how to write correct syntax and join tables properly

Posted on 2014-09-12
4
224 Views
Last Modified: 2014-09-12
This is in reference to a rental reservation software program we use called Vacation Rent Pro.   I am trying to create a report in the application using an imbedded SQL query designer called "User - Q" to generate the SQL syntax to query the database and display the required information.  There are 25 database tables the program uses.

Specifically, I am trying to run a report for a defined rental period (Ckindate) that returns the following information for all properties that are rented:
Property Name
Property Owner name
Tenant Name
Tenant Phone
Tenant Type
Rental Fees (extras)

I believe this requires joining multiple tables in the DB to do this.  I think those tables are:
Table: Rent
Table: Prop  
Table: Resrc  
Table: Tenant
Table: Rentfee
Table: Feetyp

FYI, the attached excel file shows the table field names for each of these tables:

Following is the SQL query I currently have, but it generates the error "There is a duplicate table alias "PROP" in the FROM clause", and I think there is probably more than one error in the way I'm trying to join the tables.

SELECT ;
 Prop.Shortname, ;
 Resrc.Name, ;
 Tenant.Name, ;
 Rentfee.Feetypid, ;
 Feetyp.Name, ;
 FROM Rent Rent LEFT JOIN Prop Prop ON Prop.Propid = Rent.Propid ;
 , Prop Prop LEFT JOIN Resrc Resrc ON Resrc.Resrcid = Prop.Ownerid ;
 , Rent Rent LEFT JOIN Rentfee Rentfee ON Rentfee.Rentid = Rent.Rentid ;
 , Rentfee Rentfee LEFT JOIN Feetyp Feetyp ON Feetyp.Feetypid = Rentfee.Feetypid ;
 WHERE ;
 (Rent.Ckindate = {^2014-09-19}) ;
 AND .T.

Can someone tell (show) me what is wrong and how to write this query?
DB-tables-names.xls
0
Comment
Question by:mycomac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40320448
SELECT
      Prop.Shortname
    , Resrc.Name
    , Tenant.Name
    , Rentfee.Feetypid
    , Feetyp.Name
FROM Rent Rent
      LEFT JOIN Prop Prop
                  ON Prop.Propid = Rent.Propid
      LEFT JOIN Resrc Resrc
                  ON Resrc.Resrcid = Prop.Ownerid
      LEFT JOIN Rentfee Rentfee
                  ON Rentfee.Rentid = Rent.Rentid
      LEFT JOIN Feetyp Feetyp
                  ON Feetyp.Feetypid = Rentfee.Feetypid
WHERE Rent.Ckindate = '2014-09-19'
-- AND .T.
;

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40320460
I'll step through the syntax changes.

1. You don't need to "terminate" each line, only use the semi-colon at the end of a query
2. There are endless formatting styles, I adhere to a "comma first" approach, which you see above, see next point
3. The select clause cannot finish with a comma, see your line 6, and this is a good reason for comma first formatting
4. lines 8,9,10: you have combined 2 forms of joining, and old fashioned way using commas e.g.
            from Rent, Prop, Resrc, Rentfee
    plus you have use "ANSI join syntax" (which is a good thing!)
    the way you did this is simply wrong I'm afraid, but in any case you only should use one method,
    and the best practice is to use "ANSI join syntax"

      quick hint: "don't use commas in the from clause"* this will help you avoid the old fashioned syntax

5. Not sure what you database actually is, and date handling differs a lot amongst the database types
    but a reasonable generic way to filter by date would be with single quotes
    not sure why you have use caret ^ and curly braces {}
6. the final line 13 isn't complete so I can't really comment beyond that

Your Query with line references:
SELECT ;
 Prop.Shortname, ;
 Resrc.Name, ;
 Tenant.Name, ;
 Rentfee.Feetypid, ;
 Feetyp.Name, ;
 FROM Rent Rent LEFT JOIN Prop Prop ON Prop.Propid = Rent.Propid ;
 , Prop Prop LEFT JOIN Resrc Resrc ON Resrc.Resrcid = Prop.Ownerid ;
 , Rent Rent LEFT JOIN Rentfee Rentfee ON Rentfee.Rentid = Rent.Rentid ;
 , Rentfee Rentfee LEFT JOIN Feetyp Feetyp ON Feetyp.Feetypid = Rentfee.Feetypid ;
 WHERE ;
 (Rent.Ckindate = {^2014-09-19}) ;
 AND .T.

Open in new window


On join syntax, there isn't a hard rule on this but I prefer nominating the "join to" table first, like this:
SELECT
      Prop.Shortname
    , Resrc.Name
    , Tenant.Name
    , Rentfee.Feetypid
    , Feetyp.Name
FROM Rent Rent
      LEFT JOIN Prop     ON Rent.Propid = Prop.Propid
      LEFT JOIN Resrc    ON Prop.Ownerid = Resrc.Resrcid
      LEFT JOIN Rentfee  ON Rent.Rentid = Rentfee.Rentid
      LEFT JOIN Feetyp   ON Rentfee.Feetypid = Feetyp.Feetypid
WHERE Rent.Ckindate = '2014-09-19'
-- AND .T.
;

Open in new window

and also note that as your table aliases are the same as the table names you really aren't gaining much, so you can leave them out.

Do you know what the database type is? (MySQL, Oracle etc)

---
* for picky observers:  IF you have to use a function inside the from clause, then a comma may be needed
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40320472
I've looked at the available documentation (available here) but there isn't much detail on the query designer, nor do they identify what the dbms type is that I could find.

It appears you have a GUI for building the queries, so I'm not sure how much of the query I've done for you will work.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40320491
Great, I assume it is working for yo now. Cheers, Paul
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question