I neeed to do something like this in PostgreSQL:
SELECT "OFF_ID", "DUMMY" ( <--fake field )
CASE WHEN "OFF_ID" = 10661
THEN
DUMMY value is set to "john"
ELSE
DUMMY value is set to "doe"
WHERE "OFF_STATUS" = TRUE
The " CASE " block above is TOTALLY WRONG, I just can't figure out how to proceed.
In other words, I need to select a fake column (DUMMY), and set the returned value according to a condition, in my example, depending on OFF_ID value.
You need to give the alias "dummy" to the result of the CASE expression:
SELECT "OFF_ID",
CASE
WHEN "OFF_ID" = 10661 THEN 'john'
ELSE 'doe'
END AS dummy
FROM ...
WHERE "OFF_STATUS" = TRUE
You can try below -
SELECT "OFF_ID",
CASE WHEN "OFF_ID" = 10661 THEN 'john' ELSE 'doe' end as Dummy
from tablename
WHERE "OFF_STATUS" = TRUE
Related
I wanted to do a condition wherein I put values (000000) in DATE_COMPLETED if it see's the FLAG_CLOSED = Y and if its not Y then do nothing
SELECT
"JOB",
"SUFFIX",
"SUFFIX",
"DATE_COMPLETED",
"FLAG_CLOSED",
CASE "DATE_COMPLETED"
WHEN "FLAG_CLOSED"='Y'
THEN "DATE_COMPLETED"='000000'
END "DATE_COMPLETED"
FROM "JOB_OPERATIONS"
What I got
SQL Execution Error
[LNA][PSQL][SQL Engine]Syntax Error: SELECT
"JOB",
"SUFFIX",
"SUFFIX",
"DATE_COMPLETED",
"FLAG_CLOSED",
CASE "DATE_COMPLETED" WHEN "FLAG_CLOSED" << ??? >> = 'Y' THEN "DATE_COMPLETED" = '000000' END "DATE_COMPLETED"
FROM JOB_OPERATIONS
It looks like you're attempting to change the DATE_COMPLETED column in your table. You can't do that with a SELECT statement. CASE / WHEN / THEN helps construct output. UPDATE statements allow clauses like DATE_COMPLETED='000000' that change columns.
Try something like this.
SELECT "JOB", "SUFFIX", "SUFFIX", "DATE_COMPLETED", "FLAG_CLOSED",
CASE WHEN "FLAG_CLOSED"='Y' THEN '000000'
ELSE "DATE_COMPLETED" END "CLOSED_DATE_COMPLETED"
FROM "JOB_OPERATIONS"
I named your CASE-computed output column CLOSED_DATE_COMPLETED so it won't collide with the DATE_COMPLETED colum you already mentioned.
Syntax is either:
CASE a WHEN b
... or:
CASE when a=b
To return the value of DATE_COMPLETED depending on the flag, you can do this:
CASE "FLAG_CLOSED"
WHEN 'Y' THEN '000000'
ELSE "DATE_COMPLETED"
END AS "DATE_COMPLETED"
Beware that you need to produce a coherent column type. If DATE_COMPLETED is not text, you'll need to cast it.
I want to make an if statement with conditions that have 2 result
Example
if (id = "25" OR "36") then
print "The id is 25 or 36"
else
print "the id is not 25 or 36"
end if
My concern is in the if condition statement for "OR"
I try with "AND" but this only takes id = 25 as true whereas id = 36 as false
I try with "OR" "ORELSE" "XOR" this takes everything as true.
I try || sign but I got syntax error
You are missing the comparision after the or:
If (id = "25" Or id = "36") Then
You need to provide the variable to check each time
If id = "25" or id = "36" Then
Alternatively, if this is likely to expand to include other numbers, you could use
If {"25","36"}.Contains(id) Then
Alternatively, you could use a Select Case statement:
Select Case id
Case "25", "36"
Print("The id is 25 or 36")
Case Else
Print("The id is not 25 or 36")
End Select
This works the same way as an if..else statement, but allows you to easily supply different test expressions.
select fti.pa_serial_,fti.homeownerm_name,fti.ward_,fti.villagetole,fti.status,
ftrq.date_reporting, ftrq.name_of_recorder_reporting,
case
when fti.status='terminate' then ftrq.is_the_site_cleared ='1' end as is_the_site_cleared from fti join ftrq on ftrq.fulcrum_parent_id = fti.fulcrum_id
Here, is_the_site_cleared is text type of column which is converted into boolean by the when statement written and hence does not print as 1 and takes as true. I explicitly used print '1'. But this also did not work. My aim is to display '1' in the column 'is_the_site_cleared' when the value of fti.status='terminate'. Please help!!!
How about using integers rather than booleans?
select fti.pa_serial_, fti.homeownerm_name, fti.ward_,
fti.villagetole, fti.status, ftrq.date_reporting,
ftrq.name_of_recorder_reporting,
(case when fti.status = 'terminate' -- and ftrq.is_the_site_cleared = '1'
then 1 else 0
end) as is_the_site_cleared
from fti join
ftrq
on ftrq.fulcrum_parent_id = fti.fulcrum_id ;
From the description, I cannot tell if you want to include the condition ftrq.is_the_site_cleared = '1' in the when condition. But the idea is to have the then and else return numbers if that is what you want to see.
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery
FROM table
I have this. The result of this query has 2 fields: id, ?column?
The ?column? field value is true or false.
how can I get only the true results?
SELECT * FROM (
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery
FROM table
) AS search
WHERE column = 't'
You can also use WITH: http://www.postgresql.org/docs/9.1/static/queries-with.html
use WHERE clause.... (if i understand your need correctly)
WHERE condition = true
Add a WHERE clausure to fill this values. Something like this.
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery FROM table WHERE ?column? = 1 (or True)
use alias to reference your column
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery AS condition
FROM table
WHERE condition = true
I have this SQL
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
and <<either bdvalue or string value based on user selected value datatype>>
Here in the Query based on the devudp1.valueType I want to append below attribute
If the valueType is 3 then I want to append my above select clause with devudp1.bdvalue ='10', else it should be appended by devudp1.bdvalue = 'Hello'
So the above query when valueType is 3 will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.bdvalue = '10'
else it will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.stringValue = 'Hello'
Can anyone suggest me how to put this logic in place
Try this:
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND (
(<USER-SELECTED-VALUE> = 3 AND devudp1.bdvalue ='10') OR
(<USER-SELECTED-VALUE> <> 3 AND devudp1.stringvalue ='Hello')
)