How to use Case statement in Postgresql? - sql

This is my SQL Query
CASE WHEN (1>2) THEN(
select * from rate limit 10
)
ELSE
(
select * from rate limit 1
)
When I use Case statement like above , I can get error like below.
ERROR: syntax error at or near "CASE"
LINE 2: CASE WHEN (1>2) THEN(
^
SQL state: 42601
Character: 2
Can anyone help me to solve this

Put a SELECT in front of the CASE statement. Also, you need an END after the last statement of the CASE. You need a place for the result of the CASE expression to be stored. The examples in the documentation are not executing statements that return a value; just variable assignment. So you don't need a SELECT there. If you don't care about the return from the overall query, you can use PERFORM instead of SELECT.
SELECT CASE
WHEN 1 > 2 -- always false
THEN (SELECT * FROM rate LIMIT 10)
ELSE (SELECT * FROM rate LIMIT 1)
END
;
You can also assign the result of the subqueries to a variable if this CASE is defined inside of a function.
DO
$$
DECLARE
rec RECORD;
BEGIN
CASE WHEN 1 > 2
THEN (SELECT * INTO rec FROM rate LIMIT 10)
ELSE (SELECT * INTO rec FROM rate LIMIT 1)
END
;
END;
$$ LANGUAGE PLPGSQL;

Related

single query that defines 2 tables has same row

query 1
(select count(*) from CALENDAR)
it returns 15
query 2
(select value from PARAMETER where name = 'PLAN_HORIZON')
it returns 15 too only when my programs runs without error. if error occurs,
it returns 10 or other values.
this↓ is wrong sql, but i want a single query which returns True or False.
select if (query1 == query2)
How can I define 2 sql has same result in a query?
The following SQL statement returns 0 or 1. It runs with SQL Server
SELECT CASE WHEN (select count(*) from CALENDAR) = (select value from PARAMETER where name = 'PLAN_HORIZON') THEN 1 ELSE 0 END
Something like this:
select count(*) = 0
from (
select count(*)
from calendar
except
select value
from parameter
where name = 'PLAN_HORIZON'
) t
You didn't specify your DBMS, but the above is standard SQL.
Try this query !
SELECT
CASE
WHEN (select count(*) from CALENDAR) = (select value from PARAMETER
where name = 'PLAN_HORIZON')
THEN true
ELSE false
END ;

Missing keyword ora -00905

I am getting the error indicated in the question title when I execute the following query:
select mantas_stg.NY_EGIFTS_TRANS_STG.*,
CASE WHEN MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_CURR != 'USD' THEN
CASE WHEN MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_CURR != 'USD' AND MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_LOCAL_CURR != 'USD' THEN
MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT = MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT * 100
END
END
from mantas_stg.NY_EGIFTS_TRANS_STG;
Can anyone tell me why the above query is failing at
MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT = MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT * 100
One doesn't assign values in a case expression. I think you want something like this:
select s.*,
(CASE WHEN s.FUNDS_ORIG_CURR <> 'USD' AND s.FUNDS_LOCAL_CURR <> 'USD'
THEN s.FUNDS_ORIG_AMT * 100
ELSE s.FUNDS_ORIG_AMT
END) as new_funds_orig_amt
from mantas_stg.NY_EGIFTS_TRANS_STG s;
Indentation and table aliases sure make the query easier to read.
The line below in your nested case is probably the issue:
THEN MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT = MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT * 100
You're trying to assign a value to MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT in a select statement which isn't allowed
If your goal is to update the value in that column of a particular record you should be using an update statement.

SQL Server IF EXISTS THEN 1 ELSE 2

Using Sql Server 2012. I have a stored procedure and part of it checks if a username is in a table. If it is, return a 1, if not, return a 2. This is my code:
IF EXISTS (SELECT * FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx') 1 else 2
However, I keep receiving the below error:
Incorrect syntax near '1'.
Is this even possible with an IF EXIST?
Regards,
Michael
If you want to do it this way then this is the syntax you're after;
IF EXISTS (SELECT * FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx')
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 2
END
You don't strictly need the BEGIN..END statements but it's probably best to get into that habit from the beginning.
How about using IIF?
SELECT IIF (EXISTS (SELECT 1 FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx'), 1, 2)
Also, if using EXISTS to check the the existence of rows, don't use *, just use 1. I believe it has the least cost.
Its best practice to have TOP 1 1 always.
What if I use SELECT 1 -> If condition matches more than one record then your query will fetch all the columns records and returns 1.
What if I use SELECT TOP 1 1 -> If condition matches more than one record also, it will just fetch the existence of any row (with a self 1-valued column) and returns 1.
IF EXISTS (SELECT TOP 1 1 FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx')
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 2
END
In SQL without SELECT you cannot result anything. Instead of IF-ELSE block I prefer to use CASE statement for this
SELECT CASE
WHEN EXISTS (SELECT 1
FROM tblGLUserAccess
WHERE GLUserName = 'xxxxxxxx') THEN 1
ELSE 2
END
You can define a variable #Result to fill your data in it
DECLARE #Result AS INT
IF EXISTS (SELECT * FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx')
SET #Result = 1
else
SET #Result = 2
What the output that you need, select or print or .. so on.
so use the following code:
IF EXISTS (SELECT * FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx') select 1 else select 2

SQL - SELECT with inner IF or CASE

Trying to have a row that will display 'YES' or 'NO' depending on values found (if a tree has been treated before the date given in argument say YES else NO).
Here's my function:
CREATE OR REPLACE FUNCTION tree_care(care_date DATE)
RETURNS TABLE(name VARCHAR(32), type VARCHAR(32), treated TEXT) AS
$$
BEGIN
RETURN QUERY
SELECT tree.name,
tree.type,
IF EXISTS (SELECT * FROM treatment
JOIN tree ON tree.name = treatment.tree_name
WHERE treatment.date < care_date) THEN
'YES'::text
ELSE
'NO'::text
END IF
FROM tree;
END;
$$
And I get the following error:
ERROR: syntax error at or near "EXISTS"
LINE 8: IF EXISTS (SELECT * FROM treatment
How does one implement an IF statement inside a SELECT?
PS: Using postgresql 9.4
IF is control flow. Use CASE because this is within a SELECT statement:
SELECT tree.name,
tree.type,
(CASE WHEN EXISTS (SELECT 1
FROM treatment
WHERE tree.name = treatment.tree_name AND
treatment.date < care_date
) THEN
THEN 'YES'::text
ELSE 'NO'::text
END) as Flag
FROM tree;
I am also guessing that you intend a correlated subquery, rather than an independent subquery.

Select in the IF condition statement [duplicate]

This question already has an answer here:
Oracle Select Statement in IF condition
(1 answer)
Closed 9 years ago.
I want to put in the if condition select query:
IF ( 10 in (select id from ids where ..) ) then ...
/* */
END IF;
How can implement that in the correct syntax ?
If you're using SQL Server then the obvious way is to use EXISTS, e.g.
if exists(select id from ids where id = 10)
begin
print 'it exists'
end
An alternative is to use the equivalent keywords SOME or ANY
if 10 = some(select id from ids)
begin
print 'it exists'
end
This may help - all examples are equiv to IF in Oracle SQL.
CASE Expressions:
SELECT deptno, empno, ename
, (CASE WHEN deptno = 10 THEN 1 ELSE 0 END) check_dept
FROM scott.emp
ORDER BY 1
/
DECODE() function:
SELECT deptno, empno, ename, DECODE(deptno, 10, 1, 0) check_dept
FROM scott.emp
ORDER BY 1
/
Your example - CASE..:
select id, (CASE WHEN id = 10 THEN 1 ELSE 0 END) AS check_id from ids where...
/
for ( --
-- The following select statement returns
-- records only, if there are any whose
-- ID = 10.
-- The rownum = 1 condition makes sure that
-- at most one record is returned.
--
select null
from ids
where id = 10 and
rownum = 1) loop
/*
Do something if at least on record is selected
by the select statement.
*/
end loop;
While this "solution" should do what you want, I don't necessarily recommend to do it like so because it might not be intrinsically clear for a later maintainer of the code what the purpose if this for ... loop exactly was. You might want to consider using a variable into which you select the count of the records with id=10 and do your thing based upon cnt>0.