SQL Conditional selection - Can I use Case statement - sql

I have a select query with multiple joins. Based on a value I need to set the value of one of the select values to 'N/A' if it's not it should be queried from the table. Can I use Case from this, pls let me know.
Assume the below query:
select distinct name,id,branch,Related
from table1;
And the requirement is
Need to set the "Related" to "N/A" if branch is not "computer science" else it should get the value from table1.

You may use case..when structure as :
select distinct name,id,branch,
( case when Related = 'computer science' then
related
else
'N/A'
end
) as related
from table1

Related

SQL select statement to change two other column values based on a column that contains null

I would like to use a SQL select statement that has the condition 'where column A is NULL change column B values to be equal to column C values'. How would I be able to incorporate this logic into a SELECT statement (Not an UPDATE statement as I cant change the tables on the server but want to query them from the server).
SELECT final.*
FROM final
The actual table is in the image below, here I want to change column Old to match column DirectUse if the Change column is null.
Try Case statement:
SELECT
Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
CASE: https://www.w3schools.com/sql/sql_case.asp
Can also be incorporated by UNION ALL:
SELECT Old
FROM final where Change is not null
UNION ALL
SELECT DirectUse
FROM final where Change is null
Use a CASE expression:
SELECT Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
I think you basically you want:
SELECT
ColumnA
, CASE WHEN ColumnA IS NULL THEN ColumnC ELSE ColumnB END AS ColumnB
, ColumnC
, <any other columns>
FROM Final

SQL - Where clause with case statement and wildcards

I'm working in Oracle's APEX environment, trying to return all values in one case and specific values in another case. If you aren't familiar with APEX, it uses low-code to produce a front-end page that is data driven; this case uses a dropdown to select from a list, and I'm comparing that list selection to values pulled from my database.
This creates the dropdown list (requires 2 columns - a display column [name], and a return column [id]):
select distinct 'All users' as name,
'99999' as id
from real_table
union
select distinct name,
id
from real_table
That input is stored in a variable we'll call :LIST_INPUT. I want to select all values from another_table when 'All users' is selected, and only those associated with the particular user when their name/id is selected. Below is the code I have to try and achieve that, but no dice.
select name,
id,
other_col1,
other_col2
from another_table
where case
when :LIST_INPUT like '99999' then '%'
else :LIST_INPUT
end like id
This works fine when a real user id is selected, but returns nothing when the 'All users' value is selected. My logic here is that I'm asking it to compare a wildcard to the ID field, so it should return everything, but instead it returns nothing.
Thanks!
Probably case statement is not necesasary, look:
select name,
id,
other_col1,
other_col2
from another_table
where id = :LIST_INPUT
OR :LIST_INPUT like '99999' -- You can use any other condition here
I think your LIKE operator has the operands in the wrong order. Shouldn't it be?
select name,
id,
other_col1,
other_col2
from another_table
where id like case
when :LIST_INPUT like '99999' then '%'
else :LIST_INPUT
end

Is there a CASE statement to apply to several columns to have a like output value changed to another (but different) like value?

I have several columns that read "Not Specified". I would like them to be blank instead.
Is there a better way to apply a case statement to my ENTIRE query rather than each line, if I'm looking to change the same value in different columns? Currently my query looks like this (plus several columns):
SELECT
[user_name]
,[employee_number]
,CASE [veteran_status] WHEN 'not specified' THEN ''
ELSE veteran_status
END AS veteran_status
,CASE [ethnic_origin] WHEN 'not specified' THEN ''
ELSE ethnic_origin
END AS ethnic_origin
,CASE [gender] WHEN 'not specified' THEN ''
ELSE gender
END AS gender
FROM table.HR
I am getting the correct output with the case statements, but looking to see if there's a more efficient way to apply CASE to a mass amount.
Well you can combine the UNPIVOT and PIVOT operators like so:
with t1 as (
select user_name
, employee_number
, col
, nullif(value,'not specified') value
from HR unpivot(value for col in (veteran_status, ethnic_origin, gender)) as x
)
select *
from t1
pivot (max(value) for col in (veteran_status, ethnic_origin, gender)) x;
If your columns aren't of the same data type, you might need to precondition them by casting them all to the same data type:
with t0 as (
select user_name
, employee_number
, cast(veteran_status as varchar(30)) veteran_status
, cast(ethnic_origin as varchar(30)) ethnic_origin
, cast(gender as varchar(30)) gender
from HR
), t1 as (... from t1 ...)
...
However, this may be a LOT of work for something easily achievable by more conventional means.
There is no way using which you can apply one principle (logic) to multiple / all columns of a select statement without putting them for each column.
What you can do though is write alternatives that are more concise than CASE statement. From your query style, I guess you are using MS SQL server, here is one alternative for MS SQL Server
SELECT
[user_name]
,[employee_number]
,iif([veteran_status] = 'not specified', '', [veteran_status]) veteran_status
,iif([ethnic_origin] = 'not specified', '', [ethnic_origin]) ethnic_origin
,iif([gender] = 'not specified', '', [gender]) gender
FROM table.HR
For Oracle there is a similar function called decode, Mysql equivalent is called if
That said, CASE is standard ANSII SQL, available in all databases. Choice is yours.
The following solution will work when you also want to resolve NULLs with the same value. You will still need to add this to each column that is referenced, but it uses less code & reduces redundancy overall.
ISNULL(NULLIF([veteran_status], ‘not specified’), ‘’) AS [veteran_status]
You could join the table to itself, excluding the 'not specified' columns. There isnt a huge gain, however, its an alternative perspective.
SELECT
user_name
,employee_number
,veteran_status
,ethnic_origin
,gender
FROM table.HR hr
LEFT JOIN table.HR hrvs ON hrvs.employee_number = hr.employee_number AND hrvs.veteran_status <> 'not specified'
LEFT JOIN table.HR hreo ON hreo.employee_number = hr.employee_number AND hreo.ethnic_origin <> 'not specified'
LEFT JOIN table.HR hrge ON hrge.employee_number = hr.employee_number AND hrge.gender <> 'not specified'

SQL Select Case Statement With Where Condition In Nested Table

I need to insert a SELECT CASE statement to add a column "acct_profile_ext.acct_pay_terms", to a nested condition table. So the only table that can link with this table is "acct_profile", which is "acct_profile.acct_id = acct_profile_ext.acct_id". Problem is this table "acct_profile" is heavily linked to other table as well, so I need to add this column without effect the result of other table.
So now I've insert the statement at the column part, but I'm not sure how to add the column with WHERE condition inside the SELECT CASE statement.
(SELECT CASE acct_profile_ext.acct_pay_terms
WHEN 'CASH' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
WHEN 'CHEQUE' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
ELSE NULL
END
FROM acct_profile_ext)
Please advise and help. Thanks.

sql query to get the column value again in case statement

select *,
case when number = '12' and status = 'y' then cost
end as [price]
from tblx
i got the results from the above query, i want to use the value of price column, again in case statement of the same query as like below
select *,
case when number = '12' and status = 'y' then cost-500
end as [price],
case when price = 24 then trasdate end as [trasdate]
from tblx
If my question is not clear, please suggest me
I am working in a stored procedure with more than two joins connecting 8 tables, i just want to get the column value again to use it on case statement on the same query, I shortened the question as the query is bigger.
Thanks
In the same query you need to just duplicate the same case statement where you've attempted to reference it. Btw you should probably add an else 0 to it because currently it'll return null if your conditions don't evaluate to true which could affect its usage in the other case statement.
There are other options if you don't want to duplicate the case statement e.g.:
use the price statement in a sub query and reference it by name in an outer query
use a temp table with price added as a calculated column
as above but use a table variable
Just repeating the statement is obviously simplest and may well perform best too, downside is duplication. In terms of performance, test for your likely use case.
select *,
case when number = '12' and status = 'y' then cost end price,
case when number = '12' and status = 'y'
and cost = 24 then trasdate end [trasdate]
from tblx