postgresql left outer join column doesn't exist - postgresql

I am newbie in postgres. I have a query
SELECT * FROM orders
LEFT OUTER JOIN order_details ON order_details.orderid= orders.orderid
an it get error
SQL error:
ERROR: column order_details.orderid doesn't exist
LINE 2: LEFT OUTER JOIN order_details ON order_details.orderid...
^
In statement:
SELECT COUNT(*) AS total FROM (SELECT * FROM orders
LEFT OUTER JOIN order_details ON order_details.orderid= orders.orderid) AS sub
What am i missing in my query?
Thanks before

Postgres is case sensitive. Your SQL statement must be in the correct case, and if the table name or column name is not lower case then you must enclose it in double quotation marks. (as shown below)
SELECT * FROM orders
LEFT OUTER JOIN order_details ON order_details."OrderID"= orders.orderid

Related

T-SQL Derived tables

I'm relatively new to derived tables when querying in From/Join clause as I always thought that Joins would eliminate the need for these subqueries. However, my question is that when I write a subquery within an inner join, do I need to specify the Joining column field within the subquery select statement to initiate a Join? I know you don't usually have to do this in a normal Join, however I wrote some sql code that won't execute unless I specify the joining column in the subquery select statement (I've bolded this). I've pasted the code below.
select pc.category_name
,product_name
,pp.list_price
,avg(quantity * (oi.list_price * (1-discount))) as Average_Revenue
,sum(quantity) as [products sold]
,sum(quantity * (oi.list_price * (1-discount))) as Revenue
,dt.Average_Category_Revenue
from production.categories as pc
inner join production.products as pp
on pc.category_id = pp.category_id
inner join sales.order_items as oi
on pp.product_id = oi.product_id
inner join (
select
category_name
,**pcc.category_id**
,avg(quantity * (oii.list_price * (1-discount))) as Average_Category_Revenue
from production.categories as pcc
inner join production.products as ppp
on pcc.category_id = ppp.category_id
inner join sales.order_items as oii
on ppp.product_id = oii.product_id
group by category_name, pcc.category_id
) as dt
on pp.category_id = dt.category_id
group by pc.category_name, product_name, pp.list_price, dt.Average_Category_Revenue
order by sum(quantity * (oi.list_price * (1-discount))) DESC

Why does this left outer join contain the error 'multi-part identifier could not be found'?

I have an error mentioning 'the multi-part identifier Employee.emp_id could not be found', when hovering over Employee.emp_id. This error occurs also on other references as well, such as Transaction.amount, Transaction.emp_id, etc.
I tried this query below:
SELECT Employee.name, Transaction.amount
FROM Employee
LEFT OUTER JOIN Transaction
ON (Employee.emp_id = Transaction.emp_id);
ORDER BY Employee.name ASC;
The main purpose of the query was to use left outer join, to state the name from Employees table and amount from the Transaction table, and list all the names into Ascending order.
Can someone please explain this error and why it has occurred?
you should alias your tables instead of using the table
SELECT E.name, T.amount
FROM Employee E
LEFT OUTER JOIN Transaction T
ON (E.emp_id = T.emp_id);
ORDER BY E.name ASC;
It looks like you have an extra semicolon.
SELECT Employee.name, Transaction.amount
FROM Employee
LEFT OUTER JOIN Transaction
ON (Employee.emp_id = Transaction.emp_id); <-- should this be here???
ORDER BY Employee.name ASC;

Inner Join referencing columns not valid

I am trying to do an inner join based on two columns and am having a problem referencing the columns correctly. I have used the following query which to the best of my knowledge is best pratice within postgresql. The two tables I am attempting to do an inner join on are mk_kw & adwords_final, both columns that are to be joined on are named "Key"
Here is the query
SELECT
*
FROM
adwords_final
INNER JOIN mk_kw ON "mk_kw.Key" = "adwords_final.Key";
Here is the result
ERROR: column "mk_kw.Key" does not exist
LINE 5: INNER JOIN mk_kw ON "mk_kw.Key" = "adwords_final.Key";
^
ERROR: column "mk_kw.Key" does not exist
SQL state: 42703
Character: 51
These columns certainly do exist within this context. Is there a better way to reference these columns?
Many Thanks.
The query should become:
SELECT
*
FROM
adwords_final
INNER JOIN mk_kw ON mk_kw.Key = adwords_final.Key;
Here is a sample of how to write inner joins:
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Orders and Customers are table names. CustomerID, CustomerName,OrderID are column names. All of them are case sensitive.
Peter Haddad's solution will still give you an error because key is a reserved word. You need to individually wrap the column name part of the query in double quotes as below:
SELECT
*
FROM
adwords_final
INNER JOIN
mk_kw ON mk_kw."Key" = adwords_final."Key";
Edit: Updated to reflect comments below

Join table variable vs join view

I have a stored procedure which is running quite slow. Therefore I want to extract some of the query in a separate view.
My code looks something like this:
DECLARE #tmpTable TABLE(..)
INSERT INTO #tmpTable (..) *query* (returns 3000 rows)
Select ... from table1
inner join table2
inner join table3
inner join #tmpTable
...
I then extract (copy-paste) the *query* and put it in a view - i.e. vView.
Doing this will then give me a different result:
Select ... from table1
inner join table2
inner join table3
inner join vView
...
Why? I can see that the vView and the #tmpTable both returns 3000 rows, so they should match (also did a except query to check).
Any comments would be much appriciated as I feel quite stuck with this..
EDITED:
This is the full query for getting the result (using #tmpTable or vView gives me different results, although the appear the same):
select dep.sid as depsid, dep.[name], COUNT(b.sid) as possiblelogins, count(ls.clientsid) as logins
from department dep
inner join relationship r on dep.sid=r.primarysid and r.relationshiptypeid=27 and r.validto is null
inner join [user] u on r.secondarysid=u.sid
inner join relationship r2 on u.sid=r2.secondarysid and r2.validto is null and r2.relationshiptypeid in (1,37)
inner join client c on r2.primarysid=c.sid
inner join ***#tmpTable or vView*** b on b.sid = c.sid
left outer join (select distinct clientsid from logonstatistics) as ls on b.sid=ls.clientsid
GROUP BY dep.sid, dep.[name],dep.isdepartment
HAVING dep.isdepartment=1
You maybe don't need the view/table if you change to this.
It joins on to client c and appears to be there only to JOIN onto logonstatistics
--remove inner join ***#tmpTable or vView*** b on b.sid = c.sid
--change JOIN
left outer join (select distinct clientsid from logonstatistics) as ls on c.sid=ls.clientsid
And change COUNT(b.sid) to COUNT(c.sid) in the SELECT clause
Otherwise, if you get different results you have two options I can see:
Table and view have different data. Have you run a line by line comparsion?
One has NULL, one has a value (especially for the sid column which will affect the JOIN)
Finally, when you says "different results" do you mean you get x2 or x3 rows? A different COUNT? What?

Is it possible to JOIN with a var from a LEFT JOIN without destrorying rows?

My code generates a large query. A simple version is
SELECT * FROM main_table as mt
JOIN user_data AS ud ON mt.user_id=ud.id
LEFT JOIN ban_Status AS bs ON ud.status_id=bs.id
JOIN AnotherTable ON bs.data=AnotherTable.id
NOTE: This code is untested.
When i remove the last join i get results. I can also change it to left join but that would be wrong. If ud.status is not null i would like a join as i always do when i do a select query from ban_Status. How do i fix this? must i write left join on every table if i left join the parent table? would that not give me side effects?
I am using sqlite ATM but will switch to tsql
Use the LEFT JOIN, but in your WHERE clause specify that either both ud.status_id is null and AnotherTable.id is null or neither is null.
SELECT * FROM main_table as mt
JOIN user_data AS ud ON mt.user_id=ud.id
LEFT JOIN ban_Status AS bs ON ud.status_id=bs.id
LEFT JOIN AnotherTable ON bs.data=AnotherTable.id
WHERE (ud.status_id is null and AnotherTable.id is null)
or (ui.status_id is not null and AnotherTable.id is not null)
That will keep you from selecting any records that have a ban_Status but don't have the additional data from the other table.