Solved

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

Posted on 2014-09-12
4
212 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Problem Statement In an SAP BI BO Integration project when a BO universe is built on a BEx query, there can be an issue of unit & formatted value objects not getting generated in a BO universe for some key figures. This results in an issue whereb…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now