Joining a large table on two conditions without timing out - postgresql

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?

Related

Tableau joining multiple tables

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)

How to do indexing of two join tables in Postgres?

I have two very big tables for example tab1 and tab2 in Postgres. I need to perform join and group by operation on these tables, to make my query fast i need indexing. Is there any way how to use index over join and group by queries ?? (as I heard indexing is not possible over join and group by)

Mysql Multiple Left join logic

Can someone explain multiple left join logic?
For example i have 3 tables: Company, company_text, company_rank.
Company has 4 records (id's: 1,2,3,4), company_text has 4 records with company names (1-a,2-b,3-c, 4-d), company_rank has following (1-1st, 2-2nd, 3-3rd). Note that company_rank table is not having 4th record. Now i want records of all companies using LEFT join. In case if there is no rank, display as 'zzzz' and sort in the descending order of rank and when rank is null sort by descending order of id.
select * from company
LEFT JOIN company_text ON company.id = company_text.id
LEFT JOIN company_rank ON company_text.id = company_rank.id
order by isnull(company_rank.rank,'zzzz'), rank desc
Will this work?
Basically i am trying to understand how LEFT JOIN works if there are many left joins? This doc. has good info on joins: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html but it don't have information on how multiple LEFT JOINS work? In case if multiple joins are present, how records data will be pulled?

SQLPLUS Table Trouble

I've been using SQLPLUS lately and one of my tasks was to display a set of values from two tables (stocks, orderitems). I have done this part, but I am stuck on the last part of the question which states: "including the stocks that no order has been placed on them so far".
Here is the statement:
`select Stocks.StockNo, Stocks.Description, OrderItems.QtyOrd
from Stocks INNER JOIN OrderItems
ON Stocks.StockNo = OrderItems.StockNo;`
and I have gotten the correct results for this part, but the second part is eluding me, as the curernt statement doesn't display the 0 values for QtyOrd.
Any help would be appreciated.
You likely want to use a LEFT OUTER JOIN otherwise the INNER JOIN will exclude Stocks which don't have any Orders. You might also consider grouping by Stock, in order to SUM the overall quantities for each stock?
SELECT Stocks.StockNo, Stocks.Description, SUM(OrderItems.QtyOrd) AS QtyOrd
FROM Stocks
LEFT OUTER JOIN OrderItems
ON Stocks.StockNo = OrderItems.StockNo
GROUP BY Stocks.StockNo, Stocks.Description;

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?