this is my first time posting. I am trying to display "N/A" when the the date I selected to let the query run is after a specific date, otherwise I do want a calculated number, but I keep receiving error messages. Can someone help me with this?
CASE When a.run_date > a.street_day
THEN convert(varchar(10),pre_order_forecast) as pre_order_forecast
pre_order_forecast = 'N/A'
ELSE (CASE
WHEN datediff(DAY, fcst.forecast_creation_day, NVL(a.street_day)) BETWEEN 0 AND 7
THEN fcst_qty_wk0
END)
END AS pre_order_forecast
Typically, a CASE expression is along the lines of
> CASE
> WHEN condition test 1 THEN value/calculation
> WHEN condition test 2 THEN value/calculation
> ...
> ELSE value/calculation END
It goes through the WHEN condition tests in sequence, until one is true; then that value/calculation is used (it's like a series of IF THEN ELSE expressions).
Writing these quickly becomes second nature imo, but to get started, you can
Start by writing CASE
On the next line, write WHEN, some spaces, and then THEN
Fill in the thing to check whether true after the WHEN, and what the result would be if it was true after the THEN
Repeat with the next line etc
Add an 'ELSE' for when none of the checks are true.
In your case, I'm guessing you want something like
CASE WHEN a.run_date > a.street_day
THEN convert(varchar(10),pre_order_forecast)
WHEN datediff(DAY, fcst.forecast_creation_day, NVL(a.street_day)) BETWEEN 0 AND 7
THEN fcst_qty_wk0
ELSE 'N/A'
END AS pre_order_forecast
The exact syntax and fields depend on your database.
Note that in the second WHEN, you may need to also convert fcst_qty_wk0 to varchar to match the other outputs.
Related
I have searched all around and cannot seem to find a solution to this problem I'm having. I have a fairly large case statement (over 100 lines) that works and returns the result I am looking for. An example of the line is below:
case
When (Description like '%job%'
or description like '%job%fail%') then 'Job'
Else 'Not Classified'
End as ATC
I have a case statement that returns the result 'Job' as expected. I would also like to create a separate case statement that returns the criteria that returns the condition that the record met, allowing me to evaluate which criteria are returning the match ( a 'job' vs. 'job failed' comparison). I'm aware that I can duplicate my case statement to output the criteria met, but I would like to repeat this analysis and am looking for a more easily replicable solution (something along the lines of reading the conditions from the above case statement). Any thoughts?
If you're just trying to avoid repeating the logic you can wrap it up in a table expression.
with matches as (
select *,
case when Description like '%job%fail%' then 1 -- most specific first
when Description like '%job%' then 2 -- least specific last
else 0
end as MatchCode
from ...
)
select *,
case when MatchCode > 0 then 'Job' Else 'Not Classified' End as ATC
from matches
I tried to use CASE/WHEN inside Postgresql to check two colums, but the results are odd.
As it's shown in the image below, all the lines where "gain_value" is 8 AND "patrimony_value" have a higher value return a wrong result.
This is my statement:
select stop_value, gain_value, patrimony_value,
case
when patrimony_value >= gain_value then 1
else 2
end
from copy.copy_stop_gain csg
Since it's a pretty straightforwad "if/else", i'm really not sure what i could be doing wrong.
Can anyone show me where is my mistake?
Try casting string values to numbers (or perhaps change column type in schema)...
select stop_value, gain_value, patrimony_value,
case
when patrimony_value::INTEGER >= gain_value::INTEGER then 1
else 2
end
from copy.copy_stop_gain csg
I am trying to create a formula for the following criteria.
WHEN usernotes.notetitle CONTAINS ‘collection’ AND usernotes.notedate is within the last thirty days
Here is what I have right now. It lets me write a case then set the value.
CASE WHEN ({usernotes.notetitle} CONTAINS 'collection') AND (TRUNC({today}-{usernotes.notedate}) BETWEEN 0 AND 30) THEN 1 ELSE 0 END
I don’t know if CONTAINS is the right syntax and I’m not sure if I can combine the two formulas or if I did it right.
I believe you are combining your conditions correctly with AND.
To do string comparisons, use LIKE and the % wildcard:
CASE WHEN ({usernotes.notetitle} LIKE '%collection%') AND (TRUNC({today}-{usernotes.notedate}) BETWEEN 0 AND 30) THEN 1 ELSE 0 END
If you only need to look for notes that start with collection, then just remove the first %.
See the NetSuite Help article titled SQL Expressions for details on all the SQL functions you can use.
See this page on LIKE for more details about pattern matching.
i have an sql2008 server in which i need to calculate a discount.
the fields at play are SuggestedRetailPrice, BasePrice, and AdPrice
The logic is like this:
if AdPrice is null
return baseprice/suggestedretailprice
else
if AdEndDate < GETDATE() // Ad is still valid
return adprice/SuggestedRetailPrice
endif
endif
if the above is confusing, here's what I need to do:
Determine whether to use AdPrice or BasePrice depending on if the Ad has expired by comparing AdEndDate to GETDATE(). Then use that field and divide by SuggestedRetailPrice and compare with a value. i.e. (AdPrice/SuggestedRetailPrice) >= 0.6
is something like this even possible to do with one query? i know there are case statements but i am not sure if they are applicable to this situation.
You can also embed a CASE statement within ISNULL() to simplify it still further:
SELECT
ISNULL
(
CASE
WHEN AdEndDate < GETDATE()
THEN AdPrice
END,
BasePrice
) / SuggestedRetailPrice
Yes indeed - CASE allows you to project a derived value, even through multiple branches or switch statements. The general format is as follows:
SELECT ...
CASE WHEN AdPrice is null THEN baseprice/suggestedretailprice
WHEN AdEndDate < GETDATE() -- Ad is still valid
THEN adprice/SuggestedRetailPrice
ELSE 42 -- Project a value for the column if the other branches aren't matched
END AS MyValue
I have been assigned the task of updating the EEO survey and reporting for a mid sized company. I am working on a stored procedure to populate a report from. All is good but for a syntax problem. One of the requirements is to dynamically allow the user to filter the results by the EEO Job Group Number. When the report page loads, it populates the table with all Job Groups Combined. I have placed a DropDownList on the page that allows the user to choose one of the 10 EEO Job Groups or by default, All Job Groups Combined (no filtering). The DDL executes postback and populates a parameter; #intEeoJobGroupID. There is not actually a 0 ID value in the table, just in the DDL. I want the (usp) query to use one set of WHERE statements if the passed parameter #intEeoJobGroupID = 0, and another if #intEeoJobGroupID <> 0. (Effectively adding another AND statement if the parameter <> 0)
I want to return the count of how many EEO records meet the requirements of the query. I have tried IF/THEN, and CASE, in many different formats, and can not seem get the syntax right. In the example below I get the message "Incorrect Syntax near the first = in the THEN statement, as well as the keyword ELSE.
Any hints?
DECLARE #intEeoJobGroupID INT
SELECT
COUNT (E.intEeoID)
FROM
dbo.NewEEO AS E
WHERE
CASE WHEN #intEeoJobGroupID = 0
THEN
E.intGenderID = 1
AND E.intRaceID = 2
ELSE
E.intGenderID = 1
AND E.intRaceID = 2
AND E.intEeoJobGroupID = #intEeoJobGroupID
You're making it way too complicated:
WHERE E.intGenderID = 1
AND E.intRaceID = 2
AND (E.intEeoJobGroupID = #intEeoJobGroupID OR #intEeoJobGroupID = 0)
As someone else already mentioned, your existing syntax was missing an "END", but it still won't work with that added. To get this right in the future, one thing you can try to do is remember that CASE expressions in SQL are just that: expressions. They are not statements, as you might be used to with if statements in c# code. You don't use CASE for flow control, to define blocks as you were trying to do.
Don't try to return a boolean from a CASE statement. Instead return some value that is then checked outside the CASE statement (and so then resulting in a boolean).
CASE WHEN #mode = 1 THEN CASE WHEN <Condition1> THEN 1 ELSE 0 END
WHEN #mode = 2 THEN CASE WHEN <Condition2> THEN 1 ELSE 0 END
END
=
1
Note: This will create Awful execution/explain plans and totally nerf performance. You are better using real IF blocks and real queries, or possibly unions...
IF #mode = 1
SELECT foo FROM bar WHERE <Condition1>
ELSE IF #mode = 2
SELECT foo FROM bar WHERE <Condition2>
Or...
SELECT foo FROM bar WHERE <condition1> AND #mode = 1
UNION ALL
SELECT foo FROM bar WHERE <condition2> AND #mode = 2
In order to prevent massive duplication of code, you may find that encapsulating the bulk of the query in a VIEW is helpful.
You can't make a comparison the result of a case condition. If you're using case in a where clause, it needs to be on one side of the operator:
CASE #case_value
WHEN 0 THEN
some_column
ELSE
some_other_column
END = #some_value
However, if you try to make your actual condition fit this rule, you'll end up not using the case statement at all, as #Joel point out.
You have to add
end
in the end of case.