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

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

Related

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.

use 'case when' after in function

I need to use case when to generate the value range for in function (in DB2).
for example, in below code, I want the columnB in (5,6)
select columnA from tableName where columnB in (
(case
when #variable=1 then '4' // specific number
when #variable=2 then '5' //specific number
when #variable=3 then '7,10' // a value range
end)
)
but tried several times and other similar solutions, never got the expected result
how to do this?
Firstly, In function does not read multiple values inside Case statement. The comma must be after every single value in the range.
Second, you can mention a valid condition in your Question, rather than just 1=1. It's always true so, doesn't make sense.
Example:
1) output of below query gives in (5, 6)
select columnA from tableName where columnB in ((case when #variable=1 then 5 end), 6);
2) this gives only records of columnB = 5, let say the second condition is false.
select columnA from tableName where columnB in ((case when #variable=1 then 5 end), (case when #variable=2 then 6 end));
try Something like this
select columnA from tableName
where columnB in (
select * from table(values 4) tmp(NewCol)
where #variable=1
union all
select * from table(values 5) tmp(NewCol)
where #variable=2
union all
select * from table(values 7, 10) tmp(NewCol)
where #variable=3
)
You cannot have string as value range unless you convert it into rowset. I'm not sure how to do this in DB2, but I have something that should work, since according to documentation, DB2 does have unnest(). There are of course other ways to create rowsets.
SELECT columnA
FROM unnest(array[2,6,8,10], array[7,5,6,28]) --create "temp table" for example purposes
WITH ORDINALITY AS a(columnA, columnB) --alias columns from temp table
WHERE
CASE WHEN true THEN --switch true to some other condition
columnB IN(SELECT * FROM unnest(array[5,6])) --unnest(array[]) will create rowset with 2 rows, each having one column holding integer value
END;
You might need to drop alias from AS a(columnA, columnB) since I'm not sure if it works in DB2 and I have not found live DB2 tester (it is required in PostgreSQL where I tested query).

Query to write extra rows in Excel output

I'm trying to accomplish something that seems like it should be straightforward in MS Excel. I want to use a single SQL query - so I can pass it on to others to copy and paste - though I know the following could be achieved with other methods as well. Sheet 1 looks like this:
ID value value_type
1 minneapolis city_name
2 cincinnati city_name
I want an SQL query to return an "exploded" version of those two rows:
ID attr_name attr_value
1 value minneapolis
1 value_type city_name
2 value cincinnati
2 value_type city_name
There's much more I need to do, but this concept gets at the heart of the issue. I've tried a single SELECT statement, but can't seem to make it create two rows from one, and when I tried using UNION ALL I got a syntax error.
In Microsoft Query, how can I construct an SQL statement to create two rows from the existing values in one row?
UPDATE
thanks for the help so far. First, for reference, here is the default statement that recreates the table in Microsoft Query:
SELECT
`Sheet3$`.ID,
`Sheet3$`.name,
`Sheet3$`.name_type
FROM `path\testconvert.xlsx`.`Sheet3$` `Sheet3$`
So, following #lad2025's lead, I have:
SELECT
ID = `Sheet3$`.ID
,attr_name = 'value'
,attr_value = `Sheet3$`.value
FROM `path\testconvert.xlsx`.`Sheet3$` `Sheet3$`
UNION ALL
SELECT
ID = `Sheet3$`.ID
,attr_name = 'value_type'
,attr_value = `Sheet3$`.value_type
FROM `path\testconvert.xlsx`.`Sheet3$` `Sheet3$`
And the result is this error Too few parameters. Expected 4.
LiveDemo
CREATE TABLE #mytable(
ID INTEGER NOT NULL PRIMARY KEY
,value VARCHAR(11) NOT NULL
,value_type VARCHAR(9) NOT NULL
);
INSERT INTO #mytable(ID,value,value_type) VALUES (1,'minneapolis','city_name');
INSERT INTO #mytable(ID,value,value_type) VALUES (2,'cincinnati','city_name');
SELECT
ID
,[attr_name] = 'value'
,[attr_value] = value
FROM #mytable
UNION ALL
SELECT
ID
,[attr_name] = 'value_type'
,[attr_value] = value_type
FROM #mytable
ORDER BY id;
Ok, after going back to the original statement and working up from there as per the suggestions from #lad2025, I've come up with this statement which achieves what I was looking for in my original question:
SELECT
ID,
'name' AS [attr_name],
name AS [attr_value]
FROM `path\testconvert.xlsx`.`Sheet3$` `Sheet3$`
UNION ALL
SELECT
ID,
'name_type',
name_type
FROM `path\testconvert.xlsx`.`Sheet3$` `Sheet3$`
ORDER BY ID;
One of the main problems is that the new column names are only defined in the first SELECT statement. Also, brackets are ok, just not how #lad2025 was using them originally.
Microsoft Query is pretty finicky.

Display value from column B if column A is NULL

I am wondering how i can display data from column B if Column A is null. The reason is if we get a product from one of our manufactures it is put in a different column. However, when i go to build the report the two columns essentially the same thing and it is throwing the graph way off. Any help would be appreciated.
Something like this maybe?
Case When column A isnull then column B?
Use ISNULL() or COALESCE(), or CASE
SELECT ISNULL(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT COALESCE(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT CASE WHEN ColumnA IS NULL THEN
ColumnB
ELSE
ColumnA
END AS [YourColumn]
FROM FOO

Select rows where column is null

How do you write a SELECT statement that only returns rows where the value for a certain column is null?
Do you mean something like:
SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL
?
I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:
SELECT * FROM customers WHERE first_name IS NULL
On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:
SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
Other DBMSes have similar functionality available.
If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.
Use Is Null
select * from tblName where clmnName is null
You want to know if the column is null
select * from foo where bar is null
If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it
does not work:
select * from foo where bar <> 'value'
does work:
select * from foo where bar <> 'value' or bar is null
in Oracle (don't know on other DBMS) some people use this
select * from foo where NVL(bar,'n/a') <> 'value'
if I read the answer from tdammers correctly then in MS SQL Server this is like that
select * from foo where ISNULL(bar,'n/a') <> 'value'
in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.
select Column from Table where Column is null;
select * from tableName where columnName is null
For some reasons IS NULL may not work with some column data type. I was in need to get all the employees that their English full name is missing, I've used:
SELECT emp_id, Full_Name_Ar, Full_Name_En
FROM employees
WHERE Full_Name_En = '' or Full_Name_En is null