Not exists operator error on multiple tables selection - sql

This selection works great with NOT IN, but with NOT EXISTS it returns an error:
SQL Error: ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
Does NOT EXISTS works other way?
select COMPANY.TITLE_COMPANY
from COMPANY
outer join LOCATION on (LOCATION.NAME_LOC = COMPANY.NAME_LOC)
where COMPANY.NUM_COMPANY not exists (select NUM_COMPANY from COMPANY_SUC)
;

Your syntax is wrong and it's easy to spot why. Mind that a where not exists clause is used to subtract one set of data from another set:
select
ename
from
emp
where NOT EXISTS
(select
null
from
dependents
where
emp.empno = dependents.empno
and ...
);
But you're trying to relate it to a specific field in the base query, which is wrong, it is not exactly as NOT IN, which compares data in a column to a subquery result.
Don't try to replace it with one another.

Related

Hive "with" clause syntax

The with syntax is absolutely not cooperating, can not get it to work. Here is a stripped down version of it
set hive.strict.checks.cartesian.product=false;
with
newd as (select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table1),
oldd as ( select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table2
where ds='2022-09-08')
select
(newd.risk_score_hi-oldd.risk_score_hi)/newd.risk_score_hi diff_risk_score_hi,
(newd.risk_score_cur-oldd.risk_score_cur)/newd.risk_score_cur diff_risk_score_cur,
from newd cross join oldd
order by 1 desc
Apache Hive Error
[Statement 2 out of 2] hive error: Error while compiling statement:
FAILED: SemanticException [Error 10004]: Invalid table alias or
column reference 'newd': (possible column names are:
diff_risk_score_hi, diff_risk_score_cur)
I had been following the general form shown here: https://stackoverflow.com/a/47351815/1056563
WITH v_text
AS
(SELECT 1 AS key, 'One' AS value),
v_roman
AS
(SELECT 1 AS key, 'I' AS value)
INSERT OVERWRITE TABLE ramesh_test
SELECT v_text.key, v_text.value, v_roman.value
FROM v_text JOIN v_roman
ON (v_text.key = v_roman.key);
I can not understand what I am missing to get the inline with views to work.
Update My query (the first one on top) works in Presto (but obviously with the set hive.strict.checks.cartesian.product=false; line removed). So hive is really hard to get happy for with clauses apparently. I tried like a dozen different ways of using the aliases.

Use table in order by case when does not work

I am running this query but I get an error
select *
from dog
order by case
when exists(
select 1 from dogfood where dog.dogid = dogfood.dogid)
then '1'
else '0' end;
So 2 tables, dog and dogfood which both have a dogid column. I get this error:
[42703][-206] "DOG.DOGID" is not valid in the context where
it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.26.14
[56098][-727] An error occurred during implicit system action type
"2". Information returned for the error includes SQLCODE "-206",
> SQLSTATE "42703" and message tokens "DOG.DOGID"..
> SQLCODE=-727, SQLSTATE=56098, DRIVER=4.26.14
I just want to order the dog by if it has a row in dogfood. A solution would be querying the result in the select clause and refer to it in the order by clause, but I want it in the order by clause for my application. I am curious why this query isn't working, I double checked for syntax errors but I could not find any. Am I missing something obvious? I would expect I could refer to a table in the order by which I queried in the select/from clauses.
See documentation
sort-key-expression
An expression that is not simply a column name or an unsigned integer constant. The query to which ordering is applied must be a
subselect to use this form of sort-key. The sort-key-expression cannot
include a correlated scalar fullselect (SQLSTATE 42703) or a function
with an external action (SQLSTATE 42845).
But since it is not correlated you can use IN with a fullselect
select *
from dog
order by
case when dog.dogid in (select dogid from dogfood)
then '1' else '0' end;

REPLACE with JOIN - SQL

I need help to understand what I did wrong ... I'm a beginner so excuse me the simple question!
I have two tables in which I want to do a JOIN where, in one of the columns I had to use REPLACE to remove the text 'RIxRE' that does not interest me.
In table 1, this is the original text of the column id_notification: RIxRE-1787216-BSB and this is the text that returns when using REPLACE: 1787216-BSB
In column 2, this is the text that exists: 1787216-BSB
However, I get the following error:
# 1054 - Unknown column 'a.id_not' in 'on clause'
SELECT *, REPLACE(a.id_notificacao,'RIxRE','') AS id_not
FROM robo_qualinet_cadastro_remedy a
JOIN (SELECT * FROM painel_monitoracao) b ON a.id_not = b.id_notificacao
You cannot use a column alias again in the FROM clause or the WHERE clause after the SELECT (and possibly not other clauses as well, depending on the database).
So, repeat the expression:
SELECT *, REPLACE(a.id_notificacao, 'RIxRE', '') AS id_not
FROM robo_qualinet_cadastro_remedy rqcr JOIN
painel_monitoracao pm
ON REPLACE(rqcr.id_notificacao, 'RIxRE', '') = pm.id_notificacao;
Notes:
Use table aliases the mean something, such as abbreviations for the able names.
The subquery is not necessary in the FROM clause.
I suspect that you have a problem with your data model if you need a REPLACE() for the JOIN condition, but that is a different issue from this question.

Oracle Pivot Multi Table

I am trying to create a Pivot in Oracle. I keep getting the error message
ORA-00904: "VALUEZ": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 18 Column: 6
Any thoughts?
SELECT * FROM
(
SELECT ACC.NBR,CTA.NAMEZ
FROM ACCS ACC
JOIN CARS CAR ON CAR.CAR_AAD_ID = ACC.ACC_AAD_ID
JOIN CTAS CTA ON CAR_CUS_ID = CTA_CUS_ID
)
PIVOT
(
MAX(VALUEZ) --comes from table CTAS (ERROR LINE)
FOR NAMEZ IN ('1','2','3') --from table CTAS
)
ORDER BY ACC.NBR;
As a side note, I would love it if it was possible to turn the ('1','2','3') into a subquery, but it looks like that is not possible from other post i have read. If it was easy it would be (select distinct namez from CTAS)
The columns referenced in your PIVOT clause must exist in the row source that is being pivoted. You select ACC.NBR,CTA.NAMEZ from the table; it looks like you need to expand that to ACC.NBR,CTA.NAMEZ,CTA.VALUEZ.
You cannot use a subquery to replace the list of pivot values. I believe the underlying reason for this is that the parser must be able to figure out the columns that the query will produce prior to executing it; so the pivot values must be hardcoded.
What you might be able to do, if it is appropriate to wrap this query up in a procedure or function, is to first execute a query to get the list of pivot values, then build the pivot query string using that information and execute it via dynamic SQL.
ora-01748: only simple column names allowed here pivot
select* from (SELECT TRUNC(I.POST_DATE) DATES FROM INVOICE I
INNER JOIN INVC_TENDER_V T ON T.INVC_SID=I.INVC_SID)
PIVOT
(COUNT(*)
FOR T.TENDER_TYPE IN(0,1) )

SQL statement for a join in dB2

The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.