ERROR: invalid reference to FROM-clause entry for table "oth" - sql

I have a problem with this query
SELECT DISTINCT(oth.book) FROM book_meta_keywords oth,
(SELECT bmk.meta_keyword AS metaKeyword, bmk.book AS book FROM books b
INNER JOIN customers_books cvb ON cvb.book = b.id
INNER JOIN book_meta_keywords bmk ON bmk.book = b.id
WHERE cvb.customer = 1 ) AS allCustomerPurchasedBooksMeta
INNER JOIN books b ON b.id = oth.book
WHERE oth.meta_keyword = allCustomerPurchasedBooksMeta.metaKeyword AND oth.book != allCustomerPurchasedBooksMeta.book AND b.status = 'GOOD'
I am getting below error for this query.
ERROR: invalid reference to FROM-clause entry for table "oth"
LINE 6: INNER JOIN books b ON b.id = oth.book
^
HINT: There is an entry for table "oth", but it cannot be referenced from this part of the query.
, Time: 0.002000s
But if I run the below query it works
SELECT DISTINCT(oth.book) FROM book_meta_keywords oth,
(SELECT bmk.meta_keyword AS metaKeyword, bmk.book AS book FROM books b
INNER JOIN customers_books cvb ON cvb.book = b.id
INNER JOIN book_meta_keywords bmk ON bmk.book = b.id
WHERE cvb.customer = 1 ) AS allCustomerPurchasedBooksMeta
WHERE oth.meta_keyword = allCustomerPurchasedBooksMeta.metaKeyword AND oth.book != allCustomerPurchasedBooksMeta.book
Can anyone help me why... query is basically trying to get similar books based on purchased books based on their meta keywords.
thanks.

This is your FROM clause:
FROM
book_meta_keywords oth,
(SELECT ... FROM ... WHERE ...) AS allCustomerPurchasedBooksMeta
INNER JOIN books b ON b.id = oth.book
You are mixing explicit and implicit joins (the latter is denoted by the comma). Don't. They have different prescendence rules and the query planner ends up evaluating the the second condiiton before oth was seen.
As for how to solve this: assuming that the logic is indeed what you want, that's a lateral join:
FROM
book_meta_keywords oth
CROSS JOIN LATERAL (SELECT ... FROM ... WHERE ...) AS allCustomerPurchasedBooksMeta
INNER JOIN books b ON b.id = oth.book
I suspect, however, that your query could be further simplified. You might want to ask another question for this, explaning the purpose of the query and providing a minimum reproducible example.

You are missing join
SELECT DISTINCT oth.book FROM book_meta_keywords oth join
(SELECT bmk.meta_keyword AS metaKeyword, bmk.book AS book FROM books b
INNER JOIN customers_books cvb ON cvb.book = b.id
INNER JOIN book_meta_keywords bmk ON bmk.book = b.id
WHERE cvb.customer = 1 ) AS allCustomerPurchasedBooksMeta
on oth.meta_keyword = allCustomerPurchasedBooksMeta.metaKeyword and
oth.book != allCustomerPurchasedBooksMeta.book
INNER JOIN books b ON b.id = oth.book
WHERE b.status = 'GOOD'

Well it can work :
SELECT DISTINCT oth.book
FROM book_meta_keywords oth
INNER JOIN books b ON b.id = oth.book
, (SELECT bmk.meta_keyword AS metaKeyword, bmk.book AS book
FROM books b
INNER JOIN customers_books cvb ON cvb.book = b.id
INNER JOIN book_meta_keywords bmk ON bmk.book = b.id
WHERE cvb.customer = 1 ) AS allCustomerPurchasedBooksMeta
WHERE oth.meta_keyword = allCustomerPurchasedBooksMeta.metaKeyword
AND oth.book != allCustomerPurchasedBooksMeta.book
AND b.status = 'GOOD'
But does this do what you need...

Related

My left join in oracle sql is not returning every element of the left table

i'm trying to get all the elements on the table B_ARTICULOS and make some calculations joining other tables (where some elements of B_ARTICULOS are not present) and I know I have to use a left join for that but I dont know what i'm doing wrong.
With this query I dont get every B_ARTICULOS, only those that are listed on the other tables
SELECT a.id, a.nombre,
CASE WHEN a.id IN dc.id_articulo THEN dc.cantidad ELSE 0 END CANTIDAD_COMPRAS,
CASE WHEN a.id IN dc.id_articulo THEN dc.cantidad * a.costo ELSE 0 END MONTO_COMPRAS,
CASE WHEN a.id IN dv.id_articulo THEN dv.cantidad ELSE 0 END CANTIDAD_VENTAS,
CASE WHEN a.id IN dv.id_articulo THEN dv.cantidad * a.precio ELSE 0 END MONTO_VENTAS
FROM B_ARTICULOS a
LEFT JOIN B_DETALLE_COMPRAS dc ON a.id = dc.id_articulo
JOIN B_COMPRAS c ON dc.id_compra = c.id
JOIN B_DETALLE_VENTAS dv ON dv.id_articulo = a.id
JOIN B_VENTAS v ON v.id = dv.id_venta
WHERE a.id IS NOT NULL;
The only explanation which I can see here is that you should be left joining to some of those other tables, beyond the second B_DETALLE_COMPRAS table. Assuming you would use left joins everywhere:
SELECT
a.id,
a.nombre,
CASE WHEN a.id IN dc.id_articulo THEN dc.cantidad ELSE 0 END CANTIDAD_COMPRAS,
CASE WHEN a.id IN dc.id_articulo THEN dc.cantidad * a.costo ELSE 0 END MONTO_COMPRAS,
CASE WHEN a.id IN dv.id_articulo THEN dv.cantidad ELSE 0 END CANTIDAD_VENTAS,
CASE WHEN a.id IN dv.id_articulo THEN dv.cantidad * a.precio ELSE 0 END MONTO_VENTAS
FROM B_ARTICULOS a
LEFT JOIN B_DETALLE_COMPRAS dc ON a.id = dc.id_articulo
LEFT JOIN B_COMPRAS c ON dc.id_compra = c.id
LEFT JOIN B_DETALLE_VENTAS dv ON dv.id_articulo = a.id
LEFT JOIN B_VENTAS v ON v.id = dv.id_venta
WHERE a.id IS NOT NULL;
You have to use left join too for other tables, like so:
...
FROM B_ARTICULOS a
LEFT JOIN B_DETALLE_COMPRAS dc ON a.id = dc.id_articulo
LEFT JOIN B_COMPRAS c ON dc.id_compra = c.id
LEFT JOIN B_DETALLE_VENTAS dv ON dv.id_articulo = a.id
LEFT JOIN B_VENTAS v ON v.id = dv.id_venta
...
Personally, I make subqueries for all the related tables that will be left joined to the main table. I grouped those tables then left join them to the main, like so:
...
FROM B_ARTICULOS a
LEFT JOIN (
SELECT dc.*
FROM B_DETALLE_COMPRAS dc
JOIN B_COMPRAS c ON dc.id_compra = c.id
) dc on a.id = dc.id_articulo
LEFT JOIN (
SELECT dv.*
FROM B_DETALLE_VENTAS dv
JOIN B_VENTAS v ON v.id = dv.id_venta
) dv ON dv.id_articulo = a.id
...

avoid repeating condition in select query

I have the following query to be executed on postgresql
SELECT COUNT(DISTINCT id_client) FROM contract c
INNER JOIN bundle b ON c.bundle_id = b.id
INNER JOIN payment_method pm ON pm.id = c.payment_method_id
WHERE country_id=1 AND b.platform_id=1 AND pm.name <> 'RIB'
AND CONDITION_1
AND id_client NOT IN (
SELECT id_client FROM contract c1
INNER JOIN bundle b1 ON (c1.bundle_id = b1.id)
INNER JOIN payment_method pm1 ON pm1.id = c1.payment_method_id
WHERE c1.country_id=1 AND b1.platform_id=1 AND pm1.name <> 'RIB'
AND CONDITION_2);
I don't like it because it's the same query repeated twice except of CONDITION_1 and CONDITION_2 (and I have another example where it's repeated 3 times).
It's also very slow as well.
I tried to rewrite it as the following:
WITH
filter_cpm AS (
SELECT * FROM contract c
INNER JOIN bundle b ON b.id = c.bundle_id
INNER JOIN payment_method pm ON pm.id = c.payment_method_id
WHERE c.country_id = 1 AND b.platform_id = 1 AND pm.name <> 'RIB'
)
SELECT COUNT(DISTINCT id_client) FROM filter_cpm
WHERE CONDITION_1
AND id_client NOT IN (
SELECT id_client FROM filter_cpm
WHERE CONDITION_2);
Now it's DRY but it's two times slower.
How can I re-write the query to have the same (or better) performance?
EDIT: I cannot join two conditions with AND. For example if CONDITION_1 and CONDITION_2 are VIP, then I want to select clients who were re-qualified from NOT VIP to VIP.
You can select from the common table expression twice, using an outer join:
WITH filter_cpm AS (SELECT *
FROM CONTRACT c
INNER JOIN BUNDLE b
ON b.ID = c.BUNDLE_ID
INNER JOIN PAYMENT_METHOD pm
ON pm.ID = c.PAYMENT_METHOD_ID
WHERE c.COUNTRY_ID = 1 AND
b.PLATFORM_ID = 1 AND
pm.NAME <> 'RIB')
SELECT COUNT(DISTINCT fc1.ID_CLIENT)
FROM filter_cpm fc1
LEFT OUTER JOIN filter_cpm fc2
ON fc2.ID_CLIENT = fc1.ID_CLIENT AND
CONDITION_2
WHERE fc1.CONDITION_1 AND
fc2.ID_CLIENT IS NULL
Best of luck.

Left Join Yielding no results

I'm coming across an issue where when I write a query as so:
SELECT a.v, b.w, c.x, d.y, e.z
FROM a
JOIN b
on a.id = b.id
LEFT JOIN c
on a.id = c.id
LEFT JOIN d
on b.code=d.code
JOIN e
on a.n = e.n
WHERE
a.zone = 10
WITH (nolock)
I get several hundred results, but when I modify it to this:
SELECT a.v, b.w, c.x, d.y, e.z
FROM a
JOIN b
on a.id = b.id
LEFT JOIN c
on a.id = c.id AND c.n = 0
LEFT JOIN d
on b.code=d.code AND d.n = 0
JOIN e
on a.n = e.n
WHERE
a.zone = 10
WITH (nolock)
I get zero results.
From my understanding of SQL and left joins, I feel that getting any results with the first query means I should definitely get at least one result with the second, if only one where fields from c and d are null. Does PROGRESS implement outer joins in an unusual manner?
You might want to figure out what the c.n and d.n values are. Try something like this to figure out where your starting point is.
SELECT c.n, d.n, count(*)
FROM a
JOIN b
on a.id = b.id
LEFT JOIN c
on a.id = c.id
LEFT JOIN d
on b.code=d.code
JOIN e
on a.n = e.n
WHERE
a.zone = 10
group by c.n, d.n
Then you can adjust your query based on the result

MS Access 2010 Query Equivalent

Its my second day with MS Access, i am trying to update an existing application.
And this includes updating some queries. I never knew it was going to be so complex. the parenthesis issue in Access is really disturbing and i hit the wall, i get the "syntax error" error. My SQL query is something like this :
Select ….(Something)
Into …. (Some Table)
From A
Inner join B on A.ID=B.ID
LEFT OUTER JOIN STAR as C on C.ID = A.ID
AND C.Data = ’DEMO1’
AND C.POS= ’POS1’
LEFT OUTER JOIN STAR as D on D.ID = A.ID
AND D.Data = ’DEMO2’
AND D.POS= ‘POS2’
LEFT OUTER JOIN STAR as E on E.ID = A.ID
AND E.Data = ’DEMO3’
AND E.POS= ‘POS3’
And in access, its equivalent that i am trying is :
Select ….
Into ….
From (((A
Inner join B on A.ID=B.ID)
LEFT OUTER JOIN STAR as C (on C.ID = A.ID
AND C.Data = ’DEMO1’
AND C.POS= ’POS1’)
LEFT OUTER JOIN STAR as D (on D.ID = A.ID
AND D.Data = ’DEMO2’
AND D.POS= ‘POS2’)
LEFT OUTER JOIN STAR as E on E.ID = A.ID
AND E.Data = ’DEMO3’
AND E.POS= ‘POS3’
consider
Select ….
Into ….
From
(A Inner join B on A.ID=B.ID) LEFT OUTER JOIN
STAR as C on
C.ID = A.ID AND
((C.Data = ’DEMO1’ AND C.POS= ’POS1’) OR
(C.Data = ’DEMO2’ AND C.POS= ’POS2’) or
(C.Data = ’DEMO3’ AND C.POS= ’POS3’))
Finally got my way through parenthesis.
Select ….
Into ….
From (((A
Inner join B on A.ID=B.ID)
LEFT OUTER JOIN STAR as C on (C.ID = A.ID
AND C.Data = ’DEMO1’
AND C.POS= ’POS1’))
LEFT OUTER JOIN STAR as D on (D.ID = A.ID
AND D.Data = ’DEMO2’
AND D.POS= ‘POS2’))
LEFT OUTER JOIN STAR as E on (E.ID = A.ID
AND E.Data = ’DEMO3’
AND E.POS= ‘POS3’)

Basic join and aggregate?

I have table A, table B and table C.
I want to select 2 fields from table A, one field from table B (an inner join between these two I'm assuming) and the Count for each record where the identifier on table B is found on table C.
So I'll have:
OpeartiveId | OperativeNumber | JobLocation | CountOfJobIdInWorkTable
Edit:
Operative
OperativeId
OperativeNumber
Jobs
JobId
JobLocation
Work
JobId
OperativeId
Assuming you also want records from a and b that have no matching records in c, you'll need an outer join:
select a.pk_a, b.pk_b, count(c.pk_c)
from a
inner join b on a.pk_a = b.pk_a
left outer join c on b.pk_b = c.pk_b
group by a.pk_a, b.pk_b;
Translate this to your actual schema and try it:
select a.one, a.two, b.three, count(c.id)
from a
join b on a.id=b.id
left join c on c.bid=b.id
group by a.one, a.two, b.three, b.id
Not sure if this is what you looking for. looking at the filed you provided i came up with this.
select o.operativeId, o.OperativeNumber, j.JobLocation, COUNT(w.jobId)
from dbo.Operative o
Inner join works w
ON o.OperativeID = w.OperativeId
INNER JOIN jobs j
ON w.jobId = j.jobId
GROUP by o.operativeId, o.OperativeNumber, j.JobLocation
select a.a,a.b,b.a,(select count(*) from c where c.b_id = b.id)
from a
join b on a.b_id = b.id