Oracle SQL: The equivalent way to execute the following expression from PostgreSQL? - sql

The following thread successfully showed how to use a an UPDATE SET and FROM clause together, to update an entire row of a specific column with a value derived from a different table.
When executing following expression in Oracle SQL:
UPDATE territory2_t
SET total_sales_person = t.total_count
FROM (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
WHERE territoryid = t.salesterritoryid;
Oracle states: SQL Error: ORA-00933: SQL command not properly ended

In Oracle you can use merge to do the job:
merge into territory2_t
using (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
on (territoryid = t.salesterritoryid)
when matched then
update SET total_sales_person = t.total_count

This can be done with MERGE (in Oracle and most other DB systems - those that implement the MERGE statement from the SQL Standard). MERGE is the preferred solution in most cases.
There is a misconception that this cannot be done with an UPDATE statement in Oracle. I show below how it can be done - not to encourage its use (MERGE is better), but to show that UPDATE can be used as well. This is the transformation of the Postgre SQL the OP solicited in the original post.
update ( select t2.total_sales_person, t.total_count
from territory2_t t2 inner join
( select salesterritoryid, count(*) as total_count
from salesperson_t
group by salesterritoryid
) t
on t2.territoryid = t.salesterritoryid
)
set total_sales_person = total_count;

Related

Unable to execute nested SQL queries in Spark SQL

I am trying to execute this query but it doesn't work:
SELECT COLUMN
FROM TABLE A
WHERE A.COLUM_1 = '9999-12-31' AND NOT EXISTS (SELECT 1 FROM TABLE2 ET WHERE ET.COl1 = A.COL2 LIMIT 1)
It results in an error which says the following:
"mismatched input FROM expecting"
Went through this post as it states its supported by Spark with 2.0+ version.
I'm not sure that SparkSQL supports TOP. But it is not needed. Does this work?
SELECT t.COLUMN
FROM TABLE t
WHERE t.COLUM_1 = '9999-12-31' AND
NOT EXISTS (SELECT 1 FROM TABLE2 ET WHERE ET.COl1 = t.COL2);
This fixes a few other syntax issues with the query (such as no alias A).
LIMIT in the subquery is also not needed. NOT EXISTS should stop at the first match.

SQL v Access table selection alias

I'm trying to convert some SSMS SQL to Access SQL and am finding the whole process rather frustrating! I have SQL that works perfectly well in SSMS but cannot get it to work in Access. The SQL is relatively simple. All it does is update one field in a table based on a count of items in a second table.
update Summary_Complaint_Table set period1_count = sql.mycount from
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between #1/9/2015# and #23/12/2016#
group by category
) as sql
where Summary_Complaint_Table.category = sql.category
The inner Select works perfectly well as does the outer update when I substitute sql.count and sql_category with values.
The error I'm getting is
Syntax error (missing operator) in query expression 'sql.mycount from
(select t2.category,count(t2.category)as mycount from complaints t2
where t2.date_received between #1/9/2015# and #23/12/2016#
group by category) as sql'
The original SSMS (SQL server 2005) syntax that works is
update #temp set period1_count = sql.mycount
from
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between #period1_from and #period1_to
group by category
) as sql
where
#temp.category = sql.category
Access cannot update data in one SQL if it contains aggregation/group by functions in any part of SQL. As workaround you can use DCount function instead of Count()..Group By.
I believe you need a space and an "as":
'sql.mycount from
(select t2.category, count(*) as mycount from complaints as t2
where t2.date_received between #2015/09/01# and #2016/12/23#
group by category) as sql'
Also, the dd/mm/yyyy date sequence will not work where dd is 12 or less.

Merge Query in DB2

I need to update few columns in one table with the very convoluted calculation.
I'm not good enough in SQL so I tried to use "with" clause in combination with update, but It threw error.
Then I found a post online which suggested to use MERGE so I came up with Merge query. But that one was also throwing an error.
So I removed all other column and updating only one column to remove complexity, but no avail still errors
Below is my query, select query inside working perfectly fine.
Please suggest.
MERGE INTO TABLE_1 AS O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE
FROM TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID= TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
and TABLE_3.Primary_ID=TABLE_1.Primary_ID
and TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE =CORRECT.CORRECT_FLOOR_PRICE
Error is
An error occurred when executing the SQL command:
MERGE INTO ........
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=SELECT;VALUES, DRIVER=3.61.75 [SQL State=42601, DB Errorcode=-199]
Try this instead, I think you forgot your identifier in your select statement because after the "ON" statement "CORRECT.Primary_ID" doesn't associate to anything.
MERGE INTO TABLE_1 as O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER
(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)
-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER
(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE,
TABLE_1.Primary_ID AS Primary_ID
FROM
TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID = TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
AND TABLE_3.Primary_ID=TABLE_1.Primary_ID
AND TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE = CORRECT.CORRECT_FLOOR_PRICE

Regarding joins and subquery

I have below query that I am using ..
select * from app_subsys_param where assp_name like '%param_name%'
where param_name is the name of the parameter. From this query we will get the assp_id corresponding to the parameter. With this id we look up into app_subsys_parmval table to get the value of the parameter.
update app_subsys_parmval set aspv_value = 'true' where assp_id = id_val
Now instead of separately launching the two sql statements , I want to combime both of them as one is there any sub query or join mechanism that can combine both of them in one statement , please advise
You need to use UPDATE .. FROM syntax:
UPDATE app_subsys_paramval
SET aspv_value = 'true'
FROM app_subsys_param
WHERE app_subsys_param.id = app_subsys_paramval.id
AND app_subsys_param.value LIKE '%param_name%';
Use a subselect in your update statement:
UPDATE app_subsys_parmval
SET aspv_value = 'true'
WHERE id_val = (SELECT assp_id
FROM app_subsys_param
WHERE assp_name LIKE '%param_name%')
Note, I am assuming a bit about what's in the * of your select *.
Look at the MERGE statement. This is the ANSI SQL:2003 standard for UPDATE … FROM.
Documentation:
MERGE for DB2 for Linux/UNIX/Windows
MERGE for DB2 z/OS 9.1

Update multiple columns in a TABLE from another TABLE (Oracle)

I would like to update multiple columns in one table based on values in another.
I think I know how to write an update statement in T-SQL that does what I want (haven't tested the below). Problem is I'm trying to translate this for an Oracle database. Does anyone know how to do the following in Oracle:
UPDATE oldauth SET
AUTHUNIQUENAME=newauth.AUTHUNIQUENAME
DESCRIPTION=newauth.DESCRIPTION
MAPPINGAUTHNAME=newauth.MAPPINGAUTHNAME
FROM
(SELECT * FROM USERS1 WHERE AUTHSOURCEID=100) oldauth
LEFT JOIN
(SELECT * FROM USERS2 WHERE AUTHSOURCEID=200) newauth
ON
oldauth.AUTHUSERNAME=newauth.AUTHUSERNAME;
MERGE
INTO (
SELECT *
FROM users1
WHERE AUTHSOURCEID=100
) oldauth
USING (
SELECT *
FROM users2
WHERE AUTHSOURCEID=200
) newauth
ON oldauth.AUTHUSERNAME=newauth.AUTHUSERNAME
WHEN MATCHED THEN
UPDATE
SET AUTHUNIQUENAME=newauth.AUTHUNIQUENAME,
DESCRIPTION=newauth.DESCRIPTION,
MAPPINGAUTHNAME=newauth.MAPPINGAUTHNAME