I am new to SQL plus . Could anyone help me figure out the syntax error for my code?
CREATE OR REPLACE VIEW BR_STATUS AS
SELECT CARTS_PER_CUSTOMER.loginName,CARTS_PER_CUSTOMER.number_of_carts,
CASE WHEN (number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS 'BR-status'
FROM CARTS_PER_CUSTOMER;
Whenever I try to run this part of the code, I get this error message
ORA-00923: FROM keyword not found where expected.
I followed several oracle documentation for CASE, but can't figure out what I am writing wrong. Any suggestions would be appreciated.
There is a unnecessary comma before FROM clause nothing wrong with CASE statement. Also use double quotes for Alias name instead of single quotes (thanks to Jarlh)
SELECT CARTS_PER_CUSTOMER.loginName,
CARTS_PER_CUSTOMER.number_of_carts,
CASE
WHEN ( number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS "BR-status" --Remove the comma here
FROM CARTS_PER_CUSTOMER;
Related
when i execute the following select statement i am getting the below error.
I am trying to execute the below statement in Oracle server 9.1.
I believe due to this i am getting this error.
I know we can excute the query for single quotes by '%Blondeau D''Uva%'. But i am looking for query which will pass the special character value as parameter.
Kindluy let me know how to escape single quotes in old oracle server
ERROR :
ORA-00933: SQL Command not properly ended
Error at line : 1 Column : 50
Query:
Select * FROM TABLEA where UI_FNAME like q'[%Michael%]' and UI_LNAME like q'[ %Blondeau D'Uva%]';
On oracle the following should work
Select *
FROM TABLEA
where UI_FNAME like '[%Michael%]'
and UI_LNAME like '[ %Blondeau D''Uva%]';
So no duplicate where
And you have to quote the ' between D und Uva
And probably the q before the ' is wrong ... so I have removed it as well.
Ok tried it out with the q operator:
Select *
FROM employees
where first_name like q'[%Michael%]'
and last_name like q'[ %Blondeau D'Uva%]';
No errors no row ...
Two things about your query:
Two times usage of where where there should be just one.
About the second condition. You have used q'[ %Blondeau D'Uva%]' in like clause. I think that won't give you the result you might be looking for. This has nothing to do with your error, but still, it would not hurt to re-check the query.
Try this, this shouldn't run you into any error :
Select * FROM TABLEA
where UI_FNAME like q'[%Michael%]'
and UI_LNAME like q'[%Blondeau D'Uva%]';
Cheers!
As others have tried to give you an answer, but I think you probably need to keep % outside brackets -
Select *
FROM TABLEA
where UI_FNAME like '%[Michael]%'
and UI_LNAME like '%[ Blondeau D''Uva]%';
I am trying to write a query in Hive with a Case statement in which the condition depends on one of the values in the current row (whether or not it is equal to its predecessor). I want to evaluate it on the fly, this way, therefore requiring a nested query, not by making it another column first and comparing 2 columns. (I was able to do the latter, but that's really second-best). Does anyone know how to make this work?
Thanks.
My query:
SELECT * ,
CASE
WHEN
(SELECT lag(field_with_duplicates,1) over (order by field_with_duplicates) FROM my_table b
WHERE b.id=a.id) = a.field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
FROM my_table a
Error:
java.sql.SQLException: org.apache.spark.sql.AnalysisException: cannot recognize input near 'SELECT' 'lag' '(' in expression specification; line 4 pos 9
Notes:
The reason I needed the complicated 'lag' function is that the unique Id's in the table are not consecutive, but I don't think that's where it's at: I tested by substituting another simpler inner query and got the same error message.
Speaking of 'duplicates', I did search on this issue before posting, but the only SELECT's inside CASE's I found were in the THEN statement, and if that works the same, it suggests mine should work too.
You do not need the subquery inside CASE:
SELECT a.* ,
CASE
WHEN prev_field_with_duplicates = field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
FROM (select a.*,
lag(field_with_duplicates,1) over (order by field_with_duplicates) as prev_field_with_duplicates
from my_table a
)a
or even you can use lag() inside CASE instead without subquery at all (I'm not sure if it will work in all Hive versions ):
CASE
WHEN lag(field_with_duplicates,1) over (order by field_with_duplicates) = field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
Thanks to #MatBailie for the answer in his comment. Don't I feel silly...
Resolved
I try to do an aggregation with two fields but only if the value of one field is not 0.
SELECT
CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION / CC90_StatisticQueueData.SUM_WAITING_DURATION)
FROM
CC90_StatisticQueueData
I always receive **ERROR**:
Incorrect syntax near the keyword 'FROM'.`
and don't know how to fix it.
Thanks in advance.
Use the END Keyword in CASE Statement :
SELECT CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION /
CC90_StatisticQueueData.SUM_WAITING_DURATION) END
FROM CC90_StatisticQueueData
Problem while placing oracle query inside of the oracle if else statements
select workgroupid, maxchats,
case
when a.maxchats ='-1' then
(select propvalue from ofproperty where name = 'xmpp.live.defaults.maxchats')
else
null (1)
end as aaaa
from fpworkgroup a;
when im placing maxchats at 1 position im getting error,,
how can i resolve this....?
help me..
thanks in advance
It's hard to understand your question but it sounds like you're trying to put maxchats in place of null (1) and it gives you an error?
If that is correct, I'm guessing that the error is that fpworkgroup.maxchats and ofproperty.propvalue have different column types that do not line up. All branches of a CASE must return the same column type. You cannot, for example, return an int from one branch and return a varchar from another.
You'll have to cast one or the other so that they return the same type.
I'm having a little trouble with this sql view.
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE
WHEN (tb.themed_party_size % 2) = 0 THEN
(tb.themed_party_size-2)/2
ELSE ((tb.themed_party_size-2)/2) + 0.5
END themed_tables
FROM themed_booking tb;
Can anyone help me here? I'm trying to add a column to the end of the view that the natural number result of (S-2)/2 where S is the themed_party_size.
When i say natural number result i mean like round up the answers that end in .5 so if S=7 the answer would be 3 and not 2.5.
The error I get when I try and run the above code is
Error starting at line 1 in command:
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE WHEN (tb.themed_party_size % 2) = 0
THEN (tb.themed_party_size-2)/2
ELSE ((tb.themed_party_size-2)/2) + 0.5
END themed_tables
FROM themed_booking tb
Error at Command Line:3 Column:34
Error report:
SQL Error: ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
If it makes a difference I am using sqldeveloper connected to an oracle server so I can use PL/SQL.
The error message is telling you what the problem is.
Look at Line:3 Column:34
It is an invalid character
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE WHEN (tb.themed_party_size % 2) = 0
^
My suspicion is that you are trying to use the modulo operator.
Since you are using oracle PL/SQL, you should use mod
Here is a reference Oracle/PLSQL: Mod Function
I think you can simplify with the CEIL() or ROUND() function:
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
ROUND((tb.themed_party_size-2)/2) AS themed_tables
FROM themed_booking tb;
Not sure why you get that error. Perhaps it's the % operator that is not available in Oracle. This links suggests so: Fundamentals of PL/SQL. There seems to be a MOD() function though.