ORA-00936 Missing expression on ROW NUMBER - sql

I've checked linked questions and still haven't found an answer.
SELECT *
FROM
( SELECT contract_number,
ROW NUMBER() OVER (PARTITION BY contract_number
ORDER BY ID) RowNumber
FROM contracts ) a
WHERE a.RowNumber = 1
This code throws ORA-00936 error. It underlines ROW NUMBER(), what's missing here?

Have you got the syntax right for row number? It should be ROW_NUMBER()

in the 4th line ROW NUMBER() i used and in the last line you are using WHERE a.RowNumber = 1
the function name differ from each other
this is the only error

Related

Hive - joining two tables to find string that like string in reference table

I stumbled in a case where requires to mask data using keyword from other reference table, illustrated below:
1:
Table A contains thousands of keyword and Table B contains 15 millions ++ row for each day processing..
How can I replace data in table B using keyword in table A in new column?
I tried to use join but join can only match when the string exactly the same
Here is my code
select
sourcetype, hourx,minutex,t1.adn,hostname,t1.appsid,t1.taskid,
product_id,
location,
smsIncoming,
case
when smsIncoming regexp keyword = true then keyword
else 'undef' end smsIncoming_replaced
from(
select ... from ...
)t1
left join
(select adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount,max(datex)
from ( select adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount,datex ,last_update,
max(last_update) over (partition by keyword) as last_modified
from sqm_stg.reflex_service_map ) as sub
where last_update = last_modified
group by adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount)t2
on t1.adn=t2.adn and t1.appsid=t2.appsid and t1.taskid=t2.taskid
Need advice :)
Thanks
Use instr(string str, string substr) function: Returns the position of the first occurrence of substr in str. Returns null if either of the arguments are null and returns 0 if substr could not be found in str. Be aware that this is not zero based. The first character in str has index 1.
case
when instr(smsIncoming,keyword) >0 then keyword
else 'undef'
end smsIncoming_replaced

How to output 'null' in SQL?

I'm curious about how to output 'null' in SQL. In many cases, we need to output 'null' if the targeted value doesn't exist. For example, for this leetcode problem: https://leetcode.com/articles/biggest-single-number/
The correct answer is
select ifnull((select num
from number
group by num
having count(num) = 1
order by num desc
limit 1), null) as num
The other way to write this code is
select ifnull(num, null) as num
from number
group by num
having count(num) = 1
order by num desc
limit 1
However, this code does not output 'null' if the targeted num doesn't exist. I don't understand why and am quite confused.I wonder if anyone can give a detailed explanation over this problem? Thanks a lot!
The IFNULL() function returns the alt_value, if the expression is a NULL
The IFNULL() function returns the expression, if the expression is NOT NULL
https://www.w3schools.com/sql/func_mysql_ifnull.asp
the IFNULL works outside the main query because it is evaluating the result as a whole and not per record.

RowNumber Case Statement shows Invalid column

Issue: I have a query in T-SQL which I have ROW_NUMBER as RowNumber set and Partitioned on a column called UnitNumber in my SELECT. I want to create a CASE statement that says:
CASE
WHEN RowNumber = 1 then un.SqFt Else ''
End as GrpSqft
However, I am receiving an error that my RowNumber column is invalid. Am I placing this in the wrong location? I am a rookie to Sql and am having trouble understanding how to define the column as a "built-in" column. I get:
[ Invalid column name 'RowNumber'. ]
Desired Result is to have a column "GrpSqft" with all SqFt values that fall into RowNumber=1
Thank you,
Needs to be
CASE
WHEN ROW_NUMBER() OVER (PARTITION BY UnitNumber ORDER BY BLAH DESC) = 1 THEN un.sqft ELSE ''
END AS GrpSqft

Complex SELECT IN doesn't work in Oracle 11

I have this query:
SELECT d.CREATION_DATE, r.RN5, s.rn1, s.PI1, r.KTR,
VERSION_NR, getTitle(r.RN5, VERSION_NR) AS TITLE
FROM DOI_SNAPSHOT d, RELATION r, SOURCE_ADDRESS s
WHERE r.RN1 NOT IN (8010,777)
AND d.RN5 = r.RN5
AND r.RN1 = s.RN1
AND r.RN5 IN (91010008,91010015)
AND r.RN5 not in (
SELECT RN5
FROM RELATION
WHERE DOI5 IS NOT NULL
AND DOI_DATE IS NULL
)
AND VERSION_NR = (
SELECT max(VERSION_NR)
FROM DOI_SNAPSHOT dmax
WHERE d.RN5 = dmax.RN5
);
and this query:
SELECT substr(w.message, 5, instr(w.message, 'KTR') - 5) AS RN5
FROM WEB_STATISTICS w
WHERE w.ACTION = 'DOI Display'
GROUP BY substr(w.message, 5, instr(w.message, 'KTR') - 5)
ORDER BY count(*) DESC;
both of them are working correctly.
Now if I replace the first query's line
AND r.RN5 IN (91010008, 91010015)
and put in the parenthesis the second query, I get an error
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
I don't know what is happening, since both queries are working oracle should at least accept the syntax?
The problem is that you have an order by clause in your second query, and you've left that in when you turned it into a subquery. The clause isn't valid in that context and makes no sense anyway - you're looking for members of a set, the ordering of the elements within that set is irrelevant - so you should remove the order by count(*) desc part.
To demonstrate with a simple but very contrived example; with an order by clause:
select * from dual
where dummy in (select 'X' from dual order by 1 desc);
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
And without:
select * from dual
where dummy in (select 'X' from dual);
DUMMY
-----
X
As a simplistic explanation of the error message, when the parser see the order by it thinks that applies to the main query, and therefore the subquery should have already been terminated a closing right parenthesis - which is missing.
This is a message that often doesn't mean what you might think it implies - it isn't that the query overall has unbalanced parentheses, just that it's found a syntax error that may have been caused by a missing or misplaced one.
Generally error ORA-00907 usually means "there is a syntax error somewhere before this point".
Let's try
SQL> select * from dual
2 where dummy in (select dummy from dual);
DUMMY
-----
X
SQL> select * from dual
2 where dummy in (select dummy from dual order by 1);
ORA-00907: missing right parenthesis
So you should remove ORDER BY clause from inner query.

SQL Server 2012: Incorrect syntax near the keyword 'order'

I get this error when running this query...
select *, stuff(PartNumber,1,3,'')
from products.products
where isnumeric(stuff(partNumber,1,3,'')
order by cast(Stuff(partNumber,1,3,'')As Float)
Any ideas?
Thanks!
The where statement is incomplete:
where isnumeric(stuff(partNumber,1,3,'')) = 1
-----------------------------------------^
It looks like you are missing a parentheses:
This:
where isnumeric(stuff(partNumber,1,3,'')
Should be:
where isnumeric(stuff(partNumber,1,3,''))
First is you are missing the closed bracket ")" and another is you have to assign in where clause the isnumeric returns 1 when the input expression evaluates to a valid numeric data type.
` select *, stuff(PartNumber,1,3,'')
from products.products
where isnumeric(stuff(partNumber,1,3,'')) =1
order by cast(Stuff(partNumber,1,3,'') As Float)
`