Unable to execute nested SQL queries in Spark SQL - 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.

Related

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

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;

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

Writing a single UPDATE statement that prevents duplicates

I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234

Mysql, NOT EXISTS, SELECT

Well, I was trying to select rows from one table if there are no rows in another table.
My original query was:
SELECT * FROM `jos_datsogallery` as a WHERE a.published = 1
and a.approved=1 NOT EXISTS (SELECT * FROM `jos_datsogallery_votes`
As v WHERE v.vip=62 AND v.vpic=a.id) ORDER BY a.imgdate DESC
but it keeps failing.
I made some testing and shortened my query to:
SELECT * FROM `jos_datsogallery` WHERE EXISTS (SELECT 1)
Which is supposed to select everything from jos_datsogallery as 'EXISTS (SELECT 1)' is always true.
I tried phpMyAdmin:
1064 - You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server
version for the right syntax to use
near 'EXISTS (SELECT 1) LIMIT 0, 30'
at line 1
What's wrong?
MySQL version: 4.0.27
MySQL doc: http://dev.mysql.com/doc/refman/4.1/en/exists-and-not-exists-subqueries.html
EXISTS is only supported in 4.1 and above - the documentation you linked is a combined documentation for both 4.0/4.1 so it may be misleading as to what versions actually support the keyword.
Since you updated your question to state that you're using 4.0.x that would be why it's not working for you.
Here is another way you can achieve the same, without using NOT EXISTS.
SELECT * FROM `jos_datsogallery` AS a
LEFT JOIN `jos_datsogallery_votes` AS v ON v.vip=62 AND v.vpic=a.id
WHERE a.published = 1 AND
a.approved=1 AND
v.vip IS NULL
ORDER BY a.imgdate DESC
Using a left join means the right-hand of the join (the jos_datsogallery_votes part) is allowed to not find any rows while still returning a result. When the right hand side of the join is not found, its columns will all have a value of NULL, which you can check on in the WHERE part of the query.
HTH
A little late, but I was searching for something similar and it might be of use to someone else. Another way to do this, is to use a count in the where clause. So, your query would be:
SELECT * FROM `jos_datsogallery` AS a
WHERE a.published = 1
AND a.approved=1
AND (
SELECT COUNT(*) FROM `jos_datsogallery_votes` AS v
WHERE v.vip=62
AND v.vpic=a.id
) = 0
ORDER BY a.imgdate DESC