PostgreSQL delete with inner join - sql

DELETE B.*
FROM m_productprice B
INNER JOIN m_product C ON B.m_product_id = C.m_product_id
WHERE C.upc = '7094' AND B.m_pricelist_version_id = '1000020'
i am getting the following error PostgreSQL 8.2.11
ERROR: syntax error at or near "B"
LINE 1: DELETE B.* from m_productprice B INNER JOIN m_product C ON ...
i tried giving
DELETE B from m_productprice B INNER JOIN m_product C ON B....
ERROR: syntax error at or near "B"
i tried giving
ERROR: syntax error at or near "INNER"
LINE 1: DELETE from m_productprice B INNER JOIN m_product C ON B.m_...
what is the problem with my query?

DELETE
FROM m_productprice B
USING m_product C
WHERE B.m_product_id = C.m_product_id AND
C.upc = '7094' AND
B.m_pricelist_version_id='1000020';
or
DELETE
FROM m_productprice
WHERE m_pricelist_version_id='1000020' AND
m_product_id IN (SELECT m_product_id
FROM m_product
WHERE upc = '7094');

This worked for me:
DELETE from m_productprice
WHERE m_pricelist_version_id='1000020'
AND m_product_id IN (SELECT m_product_id
FROM m_product
WHERE upc = '7094');

If you have more than one join you could use comma separated USING statements:
DELETE
FROM
AAA AS a
USING
BBB AS b,
CCC AS c
WHERE
a.id = b.id
AND a.id = c.id
AND a.uid = 12345
AND c.gid = 's434sd4'
Reference

Another form that works with Postgres 9.1+ is combining a Common Table Expression with the USING statement for the join.
WITH prod AS (select m_product_id, upc from m_product where upc='7094')
DELETE FROM m_productprice B
USING prod C
WHERE B.m_product_id = C.m_product_id
AND B.m_pricelist_version_id = '1000020';

Just use a subquery with INNER JOIN, LEFT JOIN or smth else:
DELETE FROM m_productprice
WHERE m_product_id IN
(
SELECT B.m_product_id
FROM m_productprice B
INNER JOIN m_product C
ON B.m_product_id = C.m_product_id
WHERE C.upc = '7094'
AND B.m_pricelist_version_id = '1000020'
)
to optimize the query,
use NOT EXISTS instead of IN
and WITH for large subqueries

Essentially everything mentioned here is mentioned in the docs, but no-one is specifying exactly what. So this is what the current (v15) DELETE docs says:
Notes
PostgreSQL lets you reference columns of other tables in the WHERE condition by specifying the other tables in the USING clause. For example, to delete all films produced by a given producer, one can do:
DELETE FROM films USING producers
WHERE producer_id = producers.id AND producers.name = 'foo';
What is essentially happening here is a join between films and producers, with all successfully joined films rows being marked for deletion. This syntax is not standard. A more standard way to do it is:
DELETE FROM films
WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
In some cases the join style is easier to write or faster to execute than the sub-select style.

Related

How to join Multiple

This is the code example
for three tables and I made a link on them
Now I want to add the age column from table D
SELECT A.COD,a.namee, B.NAMEE,C.NAMEE
FROM ((A INNER JOIN B ON A.COD = B.COD)
LEFT JOIN C ON A.COD = C.COD)
I mean, this code is expected
SELECT A.COD, a.name , B.NAME,C.NAME ,D.Age
FROM ((A INNER JOIN B ON A.COD = B.COD)
LEFT JOIN C ON A.COD = C.COD) , D
But in access, an error message appears, the text of the message says that the JOIN method is not supported
Is there a way to solve this?
Access does not allow to do a direct cross-join (operator ",") involving an SQL expression that includes a different type of join. The solution is as simple as enclosing the first operation between parentheses and add another SELECT, as follows:
SELECT T_A.COD, T_A.Name_ , T_B.Name_, T_C.Name_, T_D.Age
FROM
(
SELECT T_A.COD, T_A.Name_ , T_B.Name_, T_C.Name_
FROM
( T_A
INNER JOIN
T_B
ON T_A.COD = T_B.COD)
LEFT JOIN
T_C
ON T_A.COD = T_C.COD
)
, T_D
My advice is that you always enclose in a SELECT every individual join operation (with the exception of cross-joins) as good programming practice. There is no problem in doing a series of cross-joins because the cross-join operator is associative.

LEFT JOIN IN BigQuery Syntax error: Expected end of input but got keyword ON at [7:1]

I am attempting a two left joins for on order_revenue_delta_history on comcat_product_d and sales_location_d.
I keeping getting the Syntax error: Expected end of input but got keyword ON at [7:1]. I tried putting it in (), and adding AND after b. so im not sure what it is I am doing wrong.
This is my code so far below. I haven't finished the filter yet which I wanted it to show quantity sold >=1. Just trying to get it to run. Am I doing the whole join wrong? I am trying to join multiple datasets.
SELECT
a. ll_quantity_sold,
b.product_type
FROM
`slb-it-sp-valuecapture-prod.ods_vc.order_revenue_delta_history` a
LEFT JOIN `slb-it-sp-valuecapture-prod.ear_aa_108.comcat_product_d` b,
`slb-it-sp-valuecapture-prod.ear_aa_108.sales_location_d` c
ON
( a.ll_product_id = b.product_id
AND a.location_id = c.location_id )
GROUP BY
product_type
LIMIT
1000
Try the following.
your left join is incorrect. You cannot join tables all together on single on.
SELECT
a.ll_quantity_sold,
b.product_type
FROM
`slb-it-sp-valuecapture-prod.ods_vc.order_revenue_delta_history` a
LEFT JOIN `slb-it-sp-valuecapture-prod.ear_aa_108.comcat_product_d` b
ON a.ll_product_id = b.product_id
LEFT JOIN `slb-it-sp-valuecapture-prod.ear_aa_108.sales_location_d` c
ON a.location_id = c.location_id
GROUP BY
product_type
LIMIT
1000

Delete SQL query errors

I receive an error, from the below statement; It runs perfectly as a select statement, but fails as a delete. Any idea's how to get this statement working as a delete? Seems to error out at C.*
DELETE C.*
FROM
[CRM_VNB].[dbo].[CATALOGUE] AS C
LEFT JOIN VNODAT.dbo.ARCUS AS A ON A.IDCUST = C.IDCUST
WHERE
A.IDCUST IS NULL
This looks like SQL Server syntax (because of the square brackets surrounding identifiers).
In that case, the problem is with c.*; you should be using the "raw" table alias instead:
delete c
from crm_vnb.dbo.catalogue as c
left join vnodat.dbo.arcus a on a.idcust = c.idcust
where a.custid is null
Note that you could also phrase this with not exists:
delete c
from crm_vnb.dbo.catalogue as c
where not exists (select 1 from vnodat.dbo.arcus as a where a.idcust = c.idcust)
Do not use c.*
simply
DELETE FROM [CRM_VNB].[dbo].[CATALOGUE] AS C LEFT JOIN VNODAT.dbo.ARCUS AS A ON A.IDCUST = C.IDCUST WHERE A.IDCUST IS NULL

Code Explanation - Please

Sorry - I hope you don't mind me asking this question but I am new to SQL - Could someone comments on what these lines mean in this code:
select b.recipeuuid
,b.proditemuuid
,b.proditemvalueuuid
into #rectemp
from rec_recipe as a
inner join rec_recipevalue as b
on b.recipeuuid=a.recipeuuid
and b.[value] in ('Green','Yellow')
inner join rec_proditem as c
on c.proditemuuid=b.proditemuuid
inner join rec_proditemvalue as d
on d.proditemuuid=c.proditemuuid
and d.proditemvalueuuid=b.proditemvalueuuid
and d.[name]='SetupType'
;
update a
set a.[value]='1'
from rec_recipevalue as a
inner join #rectemp as b
on b.recipeuuid=a.recipeuuid
and b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[value] in ('Green','Yellow')
;
update a
set a.[name]='Normal'
from rec_proditemvalue as a
inner join #rectemp as b
on b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[name]='SetupType'
;
drop table #rectemp;
I understand that the general idea of this code. It is about how to update (or change) the value for two different columns: Attribute & Color. These two items are located in two different tables before I joined them with the appropriate UUID.
The update should take a place only when the Attribute = ‘SetupType’ and the Color = ‘Green’ or ‘Yellow’
I would like to change these two values to:
Attribute = ‘Normal’ and the
Color = ‘1’
select b.recipeuuid
,b.proditemuuid
,b.proditemvalueuuid
into #rectemp
from rec_recipe as a
inner join rec_recipevalue as b
on b.recipeuuid=a.recipeuuid
and b.[value] in ('Green','Yellow')
inner join rec_proditem as c
on c.proditemuuid=b.proditemuuid
inner join rec_proditemvalue as d
on d.proditemuuid=c.proditemuuid
and d.proditemvalueuuid=b.proditemvalueuuid
and d.[name]='SetupType'
;
***#rectemp is a temp table. Data imported into the table***
update a
set a.[value]='1'
from rec_recipevalue as a
inner join #rectemp as b
on b.recipeuuid=a.recipeuuid
and b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[value] in ('Green','Yellow')
;
***Column Value is updated to 1 based on Value is Green or yellow***
update a
set a.[name]='Normal'
from rec_proditemvalue as a
inner join #rectemp as b
on b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[name]='SetupType'
;
*** Name column is updated to Normal ***
drop table #rectemp;
*** Temp Table is dropped***

Can i use a JOIN with DELETE clause in SQL

I wonder is the following query valid in sql:
DELETE FROM Reporters
JOIN Cases ON Reporters.CaseId = Cases.ID
WHERE Cases.Court = #Court
I get an error:
Incorrect syntax near join
mention from which table that you wanted to delete from. (the solution is specific to SQL Server)
DELETE re --if you wanted to delete the cases replace `re` with `ca`
FROM Reporters re
JOIN Cases ca ON re.CaseId = ca.ID
WHERE ca.Court = #Court
DELETE r
FROM reporters as r
INNER JOIN cases as c
ON r.CaseId = c.ID
WHERE c.Court = #Court
Yes you can, you just have to specify Alias to your table and delete using it:
DELETE R
-- SELECT *
FROM Reporters AS R
INNER JOIN Cases AS C
ON R.CaseId = C.ID
WHERE C.Court = #Court ;
It will work if you alias the tables like;
DELETE r
FROM Reporters AS r
INNER JOIN Cases AS c
ON r.CaseId = c.ID
WHERE c.Court = #Court
Or you could use
DELETE FROM Reporters
WHERE EXISTS (SELECT 1 FROM Cases WHERE Cases.ID = Reporters.CaseId AND Cases.Court = #Court)