I wrote this snippet of code here in Oracle SQL Developer but I don't know how to use a CASE WHEN so that when k.QUARANTINED = 0, display 'No', else if k.QUARANTINED = 1 display 'Yes'. This column is always 0 or 1.
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,k.EXPIRATION
,k.DISDATE
,u.SCR_NO as "Patient No"
,k.QUARANTINED
,k.PREVIOUS_STATUS_ID
,k.SORT_KEY as "Sort Order"
from KIT k
left join SHIPMENT s on s.ID = k.SHIPMENT_ID
left join USR u on u.PAT_ID = k.PAT_ID;
I tried a couple of times but kept getting errors most likely since I don't know how write the syntax correctly or maybe I have to rewrite this completely differently? I'd like to keep the order of the columns the same. I just would like to see 'Yes' or 'No' for k.Quarantined instead of 0 or 1 returned in the result. :)
SELECT
S.name AS "shipment ID",
K.status_id AS "status",
K.expiration,
K.disdate,
U.scr_no AS "Patient No",
K.quarantined,
CASE k.quarantined
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'Missing or Null'
END AS "quarantine status case example 1",
CASE
WHEN k.quarantined = 0
THEN 'No'
WHEN k.quarantined = 1
THEN 'Yes'
ELSE 'Missing or Null'
END AS "quarantine status case example 2",
K.previous_status_id,
K.sort_key AS "sort order",
FROM Kit K
LEFT JOIN Shipment S
ON K.shipment_id = S.id
LEFT JOIN USR U
ON K.pat_id = U.pat_id
ORDER BY
K.sort_key ASC
;
Two examples of CASE above. The first example is used when you are evaluating a single column/variable.
The second example is used for testing multiple conditions.
When using the second example of CASE statements, it is important to understand that CASE will return the result for the first condition that evaluates TRUE. When using complex logic, sometimes a developer may (inadvertently) have overlapping logic where multiple conditions can be satisfied. When unexpected results from CASE occur, it is important to go back and reevaluate the statement from the top down.
If you are absolutely sure that K.quarantined cannot be NULL and it can only be 0 or one (research the table DDL for check constraints) then you can remove or comment out the ELSE clause on the CASE statements--but it is generally good practice to always have an ELSE clause for consistency. You can have it simply state "ELSE NULL" if you do not ever expect anything other than what's described in your CASE statement.
Lastly, be sure to make sure to identify whether K.quarantined is numeric or text (check table DDL). If it is actually storing text '0' or '1', then should change your literals accordingly--although I think current versions of Oracle are smart enough to do the implicit conversions for you.
Related
I am trying to create a calculated field in tableau that will count if one field OR another contains 'yes' - and then add the YES's together as a value.
I have tried CASE WHEN, IF, COUNTIF but I am having trouble as I have to have the OR in the calculated field.
Here is my last attempt that also failed:
COUNTIF([M-CROSSC (Cross Country)]) = 'YES' OR ([W-CROSSC (Cross Country)])= 'YES' THEN '1' END
Try this. Put the if statement inside the count().
COUNT(IF [M-CROSSC (Cross Country)] = 'YES' OR ([W-CROSSC (Cross Country)]= 'YES' THEN '1' END)
edited to remove extra ')' after each dimension.
Hi so I have a question regarding case statements and why certain statements dont work.
(Case WHEN LENGTH(input)<=5 THEN text LIKE '%'
ELSE text = input)
When I did this, it didnt work and im still confused as to why.
However the solution to this problem was
(Case WHEN LENGTH(input)<=5 THEN text LIKE Concat('%',input,'%')
ELSE text = input END)
However My main question is below though I would like an answer to the above question and since they are identical I will leave it as one question.
I have a student who is still being reviewed for entering the school, however this school is german so i need to translate a few words for them since the system is in English
select s.fname, s.lastname,
(Case when s.status like 'APPROVED' then s.status = 'Genehmigt'
when s.status like 'PENDING' then s.status = 'Anstehend.'
end) as Status
from Student
My results are giving me False in the status field so obviously im doing something wrong...
And I know there are two types of case statement so am I using the recommended one?
The expression you have placed after then:
s.status = 'Genehmigt'
is not an assignment, it is a boolean expression, so it yields true or false.
s.status = is just redundant.
select
fname,
lastname,
case when status = 'APPROVED' then 'Genehmigt'
when status = 'PENDING' then 'Anstehend.'
end as status
from student
Note that like with a string without wildcards on the right side should be a simple comparison.
Probably something really trivial but I haven't quite found the answer I am looking for on the internet and I get syntax errors with this. What I want/need to do is to provide a special case in my where clause where the doctype is 1. If it is, then it needs to match the claimID from a sub select of a temp table. If the doctype is not a 1 then we just need to continue on and ignore the select.
AND
CASE
WHEN #DocType = 1 THEN (c.ClaimID IN (SELECT TNE.ClaimID FROM TNE)
END
I have seen some for if statements but I didn't seem to get that to work and haven't found anything online as of yet that shows a case statement doing what I would like. Is this even possible?
You don't need a case statement, you could do:
AND (#DocType <> 1 or c.ClaimID in (SELECT TNE.ClaimID FROM TNE))
A CASE expression (not statement) returns a single value. SQL Server supports the bit data type. (Valid values are 0, 1, 'TRUE' and 'FALSE'.) There is a boolean data type (with values TRUE, FALSE and UNKNOWN), but you cannot get a firm grip on one. Your CASE expression attempts to return a boolean, give or take the unmatched parenthesis, which is not supported in this context.
You could use something like this, though Luc's answer is more applicable to the stated problem:
and
case
when #DocType = 1 and c.ClaimId in ( select TNE.ClaimId from TNE ) then 1
when #DocType = 2 and ... then 1
...
else 0
end = 1
Note that the CASE returns a value which you must then compare (= 1).
I need to further refine my stored proc resultset from this post, I need to filter my resultset to display only records where emailaddr is NULL (meaning display only records that have Invoice_DeliveryType value of 'N' ).
Among numerous queries, I have tried:
select
Invoice_ID, 'Unknown' as Invoice_Status,
case when Invoice_Printed is null then '' else 'Y' end as Invoice_Printed,
case when Invoice_DeliveryDate is null then '' else 'Y' end as Invoice_Delivered,
(case when Invoice_DeliveryType <> 'USPS' then ''
when exists (Select 1
from dbo.Client c
Where c.Client_ID = SUBSTRING(i.Invoice_ID, 1, 6) and
c.emailaddr is not null
)
then 'Y'
else 'N'
end)
Invoice_ContactLName + ', ' + Invoice_ContactFName as ContactName,
from
dbo.Invoice
left outer join
dbo.fnInvoiceCurrentStatus() on Invoice_ID = CUST_InvoiceID
where
CUST_StatusID = 7
AND Invoice_ID = dbo.Client.Client_ID
AND dbo.client.emailaddr is NULL
order by
Inv_Created
but I get an error
The conversion of the nvarchar value '20111028995999' overflowed an int column
How can I get the stored procedure to only return records with DeliveryType = 'N' ?
Trying selecting the stored proc results into a temp table, then select
* from #TempTable
We could really do with a schema definition to get this problem resolved.
It appears that there is an implicit conversion occurring within one of your case statements, but without the schema def's it's difficult to track down which one.
You can't safely mix datatypes in CASE expressions, unless you are absolutely sure that any implicit conversions will work out OK you should make the conversions explicit.
Judging by the error message seeming to include something that could be a date represented as a string(20111028) plus some kind of other data ?time?(995999) it may be something to do with Invoice_DeliveryDate, but this is a shot in the dark without more details.
I have a .rpt file , with a view datasource . I have four parameter which i use in filtering the selection. I have written my selection formula like below.
if ({?actype} <> "All") OR ({?actype} <> "All") OR ({?collectorname} <> "All") OR ({?batchno}<> "All") Then
(
if {?actype} <> "All" Then
{CollectorPerformance.accountType} = {?actype};
if {?collectorname} <> "All" Then
{CollectorPerformance.realname} = {?collectorname};
if {?batchno} <> "All" Then
{CollectorPerformance.batchno} = {?batchno}
and
{CollectorPerformance.clientid} = {?clientid}
and
Date({CollectorPerformance.paymentdate}) >= Date({?from})
and
Date({CollectorPerformance.paymentdate}) <= Date({?to})
)
My issue with the formula, above is that it does not filter by realname and actType. I understand the reason is because the key word "and" is missing . however, it filters the batchno correctly> please how do i make it filter by the remaining two if's ? any help would appreciated.
A selection formula has to be one long valid boolean statement, which is, I think, what you were already suggesting when you say the "and is missing". So in order to fix the first half, you just need to translate those statements into one simplified boolean statement instead of individual statements (those that end in a ';').
({?actype}="All" or {?actype}={CollectorPerformance.accountType})
and
({?collectorname}="All" or {?collectorname}={CollectorPerformance.realname})
and
({?batchno}="All" or {?batchno}={CollectorPerformance.batchno})
...
For each parameter, a user can either select "All" or enter a specific value to filter by. If "All" is selected, that particular portion of the statement (The part that looks like {?Parameter}="All") will evaluate to True and no filtering will be done. Otherwise, only records matching the entered parameter value will return True.