Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
'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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

947 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

23 Experts available now in Live!

Get 1:1 Help Now