How to compare two columns in SQL Redshift using a CASE statement? - sql

I'm trying to find a way to check if two values are the same or not in two different columns using sql redshift
Here is what I've tried:
CASE
WHEN table_1.column_a = table_2.column_b THEN 'Match'
ELSE 'No match'
END AS Does_it_match?
table_1 and table_2 are joined but it keeps returning the error 'No value specified for parameter 1'
Is it possible to do this kind of CASE statement with sql redshift? If not, is there another way around it?

Related

Placing a predefined value in the empty fields of an SQL query result

I need to manipulate the data that a certain SQL query outputs as a result, only by modifying the original query. Since it is s Select-Where-From query, as a novice in SQL I assume I can simply nest it inside another query of this type, resulting in a structure similar to: Select-Where-(Select-Where-From).
The data manipulation simply requires the replacement of all empty fields in a certain column (that was taken from the result of the original query) with a specific predefined value. Here are the two attempts I've made - based on findings from this website - which failed:
select NAME_OF_COLUMN, COALESCE(NULLIF(NAME_OF_COLUMN,''), 'Value_to_insert')
from
(THIS IS WHERE THE ORIGINAL SELECT QUERY GOES)
This one doesn't throw an error, but nonetheless produces empty fields instead of populating them with the value above, as if only the original query was run.
The 2nd:
Select *, NAME_OF_COLUMN=
CASE WHEN NAME_OF_COLUMN IS NULL THEN 'Value_to_insert'
WHEN NAME_OF_COLUMN='' THEN 'Value_to_insert'
ELSE NAME_OF_COLUMN
END
from
(THIS IS WHERE THE ORIGINAL SELECT QUERY GOES)
This one throws the following error (forgive me for the messy presentation, but it was not up to me):
java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
, org.eclipse.birt.report.engine.api.EngineException: Invalid bound column name: CREATOR_USER_NAME., org.eclipse.birt.report.engine.api.EngineException: Cannot get the result set metadata.
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:ORA-00923: FROM keyword not found where expected;
Can you please assist me and tell me what am I doing wrong? Perhaps I need to select a specific column and/or use the 'as' command?
Edit: I have attempted replacing the original select which was:
select table.column as NAME_OF_COLUMN
with this:
select nvl(table.column, 'Value_to_insert') as NAME_OF_COLUMN
Unfortunately, just like the first attempt, the output is identical to the output of the original query..
NAME_OF_COLUMN=CASE ... END is invalid in Oracle. You can't assign a column value in (standard) SQL like that.
If you are trying to come up with a column alias: that needs to go after the expression:
CASE
WHEN name_of_column IS NULL THEN 'Value_to_insert'
WHEN name_of_column = '' THEN 'Value_to_insert'
ELSE name_of_column
END as name_of_column
In Oracle an empty String '' is converted to NULL when you store the value. So the second condition of your CASE expression will never be true. The whole thing can be simplified to:
coalesce(name_of_column, 'Value_to_insert') as name_of_column
Note that you need to get rid of the select * part and explicitly list all other columns excluding name_of_column there, otherwise your query ends up with two columns with the same name.

CASE statement with multiple WHEN THEN in SQL

I’m working in Eloqua BI. I am trying to create one column in which I’d like to include data from couple of other columns. Those other columns contain numbers of customers.
Those columns have been created so far with a following SQL logic (in Edit Column Formula Field):
CASE
WHEN column_name = '1' THEN 'a'
WHEN column_name = '2' THEN 'b'
WHEN column_name = '3' THEN 'c'
ELSE ‘def’
What I’ve find out is the code above returns correct value for only the first WHEN. All other columns have random values.
How could I fix the code above to receive correct values for all columns? Why that statement is incorrect?

How to Compare values in SQL having dots

I'm comparing values from two different database. In one table the name is stored with dots(i.e A.B C) and in another table the same value is stored as AB C(i.e in second table none of the names contain dots) In SQL how can I compare the values? Dot can come anywhere in the name.
If you are comparing columns from two tables which are from different databases, you have to find a way to connect those two databases.
In SQL Server, you can use Linked Servers. For more details about linked server, See this link.
After that you can compare them using REPLACE:
WHERE REPLACE(T1.ColName,'.','')=T2.ColName
For example:
SELECT T1.ColName,T2.ColName2,
CASE WHEN REPLACE(T1.ColName,'.','') = T2.ColName THEN 'Equal' ELSE 'Not Equal' END AS EqualOrNot
FROM Table1 T1 JOIN
LinkedServerName.DBname.dbo.Table2 T2 ON T1.fk=T2.pk
Use replace:
column1 = replace(column2,'.','');

Query case sensitive in SQL database without setting database to case sensitive

I have a column IsSeeded with values 'N' and 'NULL' as one of the possibilities
I want to select all records that don't have a 'N' in their field.
when I query
select * from database.file where IsSeeded !='N';
it also doesn't return the files with values 'NULL'. How can I allow 'NULL' values without having to change my databasepreferences to case sensitive?
You have to use the IS operator when comparing to null values instead of the normal compare operators (!=/=/<>/...)
select * from database.file
where IsSeeded IS NULL;
Otherwise the result will be unknown for null entries and the condition is false.

Update table in run time and the column not known in compile time

I am trying to write the SQL to update multiple columns based on some code condition to the table in the code.
For example, make it the easiest one in two columns
UPDATE table set A = valueA where conditionA..
OR UPDATE table set B=valueB where conditionA..
OR UPDATE table set A=valueA, B=valueB where conditionA..
The condition is the same, but valueA/valueB may not exist depending on the code
Is there any convenient way to combine them together in one SQL such as the select one WHERE 1=1 and <condition> so that I can add the conditions regardless one or more than one conditions?
Hope it make sense.
if you use the same condition you can write the query simply like this:
UPDATE [table] SET A = valueA, B=valueB, C=valueC WHERE <condition>
But if you need different conditions for all the fields, you could you a CASE operator like this:
UPDATE [table] SET A = CASE WHEN <cond_for_A> THEN valA ELSE A END,
CASE WHEN <cond_for_B> THEN valB ELSE B END,
CASE WHEN <cond_for_C> THEN valC ELSE C END
You vary the fields being updated, presumably based on some condition in your code, which probably means you won't be able to get away with just one SQL statement - bound parameters cannot "add" or "remove" columns within SQL text.
If you have a small number of columns, you can make a separate SQL statement for each combination of them that is of interest. Otherwise, you'll likely need to construct the SQL text dynamically and fill the SET clause of your statement so it includes exactly the columns you need.
None of this should serve as an excuse to skip properly binding all parameters, though!