Tableau joining multiple tables - tableau-api

Hi, I have trouble understanding the structure of this table connection.
Let's suppose that all joins are inner join. Does this picture mean:
Orders JOIN (Orders1 JOIN People) JOIN Returns?
or
Orders JOIN Orders1 JOIN (People JOIN Returns)?
I don't understand
Why Orders1 and People are both vertically aligned and both connected with Orders table.
(As my understanding, join operation is bilateral, not trilateral. My imagination is that the joining should be all represented horizontally, looking like a chain.)
I know SQL, it would be easier to explain if write a pseudo SQL script.

The example you have taken is not very good to understand the joins/relationships in tableau.
A wire/line between two tables indicate the join on two tables with some id column (one to many OR one to one OR many to many). You can check the relationship based on which field (read column) by clicking that relationship thread(line). Why I termed this example not a good one because ORDERS table joining itself may have umpteen options. You can edit that relationship in many ways.
So, in you example, ORDERS is joined with itself (ORDERS1). (the result will depend on relationship type of course). Simultaneously ORDERS is joined with PEOPLE table. Since these tables have only one field in common, this relationship has resulted in creation of just one extra column in ORDERS result. NoW PEOPLE is also connected with RETURNS where no column is common so I am not able to understand this relationship.
A watch of this 5 minute video is recommended.
Translating this relationship will be something like..
(ORDERS JOIN (PEOPLE JOIN RETURNS)) JOIN ORDERS1
(Last JOIN outside braces is on the result of braces but with field/fields from ORDERS)

Related

How to optimize a query with several join?

I have to write on paper a good physical plan for a Postgresql's query with several natural join, is it the same as treating a query with a simple join or should I use a different approach?
I am working on this one, by the way
SELECT zname
FROM Cage natural join Animal natural join DailyFeeds natural join Zookeeper
WHERE shift=’const’ AND clocation=’const’;
By Oracle
A NATURAL JOIN is a JOIN operation that creates an implicit join
clause for you based on the common columns in the two tables being
joined. Common columns are columns that have the same name in both
tables.
I think the above is answering following
is it the same as treating a query with a simple join or should I use a different approach?
I hope it helps.

Joining a large table on two conditions without timing out

I'm trying to join two tables together and one table has two identifiers that I want.
select stations.id, stations.name, count (rentals.startstation_id) as station_starts, count (rentals.endstation_id) as station_ends
from ny.station as stations
Left Join ny.rental as rentals on stations.id = rentals.startstation_id
Left Join ny.rental as rentals on station.id = rentals.endstation_id
group by 1,2
However the ny.rental table is very large and when I run this query my SQL Workbench/J crashes. The stations table is rather small.
What is the optimal way to construct this query?

SQL Natural Join

Okay. So the question that I got asked by the teacher was this:
(5 marks) Construct a SQL query on the dvdrental database that uses a natural join of two or more tables and an additional where condition. (E.g. find the titles of films rented by a particular customer.) Note the hints on the course news page if your query returns nothing.
Here is the layout of the database im working with:
http://www.postgresqltutorial.com/wp-content/uploads/2013/05/PostgreSQL-Sample-Database.png
The hint to us was this:
PostgreSQL hint:
If a natural join doesn't produce any results in the dvdrental DB, it is because many tables have the last update: timestamp field, and thus the natural join tries to join on that field as well as the intended field.
e.g.
select *
from film natural join inventory;
does not work because of this - it produces an empty table (no results).
Instead, use
select *
from film, inventory
where film.film_id = inventory.film_id;
This is what I did:
select *
from film, customer
where film.film_id = customer.customer_id;
The problem is I cannot get a particular customer.
I tried doing customer_id = 2; but it returns a error.
Really need help!
Well, it seems that you would like to join two tables that have no direct relation with each other, there's your issue:
where film.film_id = customer.customer_id
To find which films are rented by which customer you would have to join customer table with rental, then with inventory and finally with film.
The task description states
Construct a SQL query on the dvdrental database that uses a natural join of two or more tables and an additional where condition.quote

zf many to many relationship, how to find values stored in intermediary table without a second lookup

I have a many to many relationship between two tables which is represented with an intermediary table. I am using the ZF1 table relationships methodology to model the database in my application and this works fine. One thing I am struggling with is to pull data from the intermediary table when performing a many to many lookup. For exmaple:
productsTable
product_id,
product_name
customerTable
customer_id,
customer_name
salesTable
customer_id,
product_id,
date_of_sale
In this case where the sales table is the intermediary table and the many to many relationship is between customers and products. I add the referenceMap to the sales table model for products and customers and the dependent table "sales" to the product table model and the customer table model.
I can then successfully use the following code to get all the products for a given customer (or vice-versa).
$productTable = new productsTable();
$product = $productTable->find(1)->current();
$customers = $product->findManyToManyRowset('customerTable','salesTable');
But it does not include the "date_of_sale" value in the rowset returned. Is there a way of including the values from the intermediary table without doint a separate database lookup. Ican't see anything in the zf docs.
Any help would be cool.
I hope to eventually replace the zend_table with a datamapper implementation as it seems highly inefficient in terms of the number of db queries it executes which could be hugely reduced with slightly more complex SQL join queries rather than multiple simple selects but for now I'm stuck with this.
Thanks.
You can use JOIN queries in your code to make it in one call. From what I understand, you want to build this query
SELECT p.*, c.*, s.date_of_sale FROM sales AS s
INNER JOIN products AS p ON p.product_id = s.product_id
INNER JOIN customers AS c ON c.customer_id = s.customer_id
WHERE p.product_id = '1';
To achieve this, you can refer to Complex SQL query into Zend to see how I translate it to a Zend_Db Query or just use something like
$select = $this->select()->setIntegrityCheck(false);
$select->from(array('s'=>'sales'), array('date_of_sale'));
$select->join(array('p'=>'products'), 'p.product_id = s.product_id', '*');
$select->join(array('c'=>'customers'), 'c.customer_id = s.customer_id', '*');
$select->where('p.product_id = '.$productId);
Obviously, it would be better to quoteInto the where clause but I'm just too lazy.
Hope this helps !

sql query to retrieve DISTINCT rows on left join

I am developing a t-sql query to return left join of two tables, but when I just select records from Table A, it gives me only 2 records. The problem though is when I left join it Table B, it gives me 4 records. How can I reduce this to just 2 records?
One problem though is that I am only aware of one PK/FK to link these two tables.
The field you are using for the join must exist more than once in table B - this is why multiple rows are being returned in the join. In order to reduce the row count you will have to either add further fields to the join, or add a where clause to filter out rows not required.
Alternatively you could use a GROUP BY statement to group the rows up, but this may not be what you need.
Remember that the left join brings you null fields from joined table.
Also you can use select(distinct), but i can't see well you issue. Can you give us more details?