Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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.
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

775 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