Solved

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

Posted on 2014-09-12
4
222 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 48

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 48

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 48

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 48

Expert Comment

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

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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