build where clause to verify only once first clause is met - sql

I have following sql select:
select ...
from table1 a, table2 b
where
a.column = 'ABC' and
a.column2 = b.column2
I would like to only check if a.column2 = b.column2 when a.column = 'ABC'.
How do I do that?
Thanks

I'm not sure from your question tag if you're trying to figure out how to do this with a JOIN specifically (as opposed to how you did it with the WHERE clause), but anyway -- a couple of ways:
1) --with WHERE clause
select ...
from
table1 a
INNER JOIN table2 b
ON a.column2 = b.column2
where
a.column = 'ABC'
2) --WITHOUT WHERE CLAUSE
select ...
from
table1 a
INNER JOIN table2 b
ON a.column2 = b.column2
AND a.column = 'ABC'

Try this. It will check column2 only when column is 'ABC':
select ...
from table1 a, table2 b
where
(a.column = 'ABC' and
a.column2 = b.column2) or a.column <> 'ABC'

Related

Anti Join based on multiple keys / columns (SQL)

The setting is simple, I wanted to retrieve all rows from table A that were not present in table B. Because a unique row can be identified using 4 columns, I needed to have a way to write the WHERE statement that it works correctly.
My solution is to concatenate the 4 columns and use that as "one" column/key to do the outer join:
select *
from table_A
where filter_condition = 0
and (column1 || column2 || column3 || column4) not in (
select A.column1 || A.column2 || A.column3 || A.column4
from table_A A -- 1618727
inner join table_B B
on A.column1 = B.column1
and A.column2 = B.column2
and A.column3 = B.column3
and A.column4 = B.column4
and filter_condition = 0
)
My question is, is this a good way of doing this or am I doing something fundamentally wrong?
To be clear, the desired result is simply to get back only the rows of table_A that I "lose" due to the INNER JOIN with table_A and table_B.
You seem to be looking for not exists:
select a.*
from table_a a
where a.filter_condition = 0
and not exists (
select 1
from table_b b
where
a.column1 = b.column1
and a.column2 = b.column2
and a.column3 = b.column3
and a.column4 = b.column4
)
This will give you all records in table_a that do not have a corresponding record in table_b.
Using a LEFT JOIN between A and B and checking for a NULL row in B is probably easier:
SELECT *
FROM table_A A
LEFT JOIN table_B B ON A.column1 = B.column1
AND A.column2 = B.column2
AND A.column3 = B.column3
AND A.column4 = B.column4
WHERE B.column1 IS NULL
AND A.filter_condition = 0
You should be able to use tuples (aka row constructors) in PostgreSQL:
select *
from table_a
where filter_condition = 0
and (column1, column2, column3, column4) not in
(
select column1, column2, column3, column4
from table_b
);
If the columns can be null, then better use NOT EXISTS, as null=null results in "unknown" rather than in true or false.

Alternative for correlated update of Oracle

How can I re-write this query to sample update. Can someone decode me for this step by step. Is the whole table1 getting updated here/
UPDATE
(SELECT
A.COLUMN1 A_COLUMN1,
B.COLUMN2 B_COLUMN2
FROM TABLE1 A,TABLE2 B
WHERE A.COLUMN3=B.COLUMN3 AND A.COLUMN4=B.COLUMN4)
SET A_COLUMN1=B_COLUMN2;
Let me answer the question.
The subquery is using an inner join. Hence, the subquery will filter out rows that don't match the join conditions in the two tables. This happens before the update.
Hence, not all the rows get updated. If you want to update all rows, use a left join or:
UPDATE TABLE1
SET A_COLUMN1 = (SELECT B.B_COLUMN2
FROM TABLE2 B
WHERE A.COLUMN3 = B.COLUMN3 AND A.COLUMN4 = B.COLUMN4
);
This way it touches only the common elements
UPDATE TABLE1
SET A_COLUMN1 = (SELECT B.B_COLUMN2
FROM TABLE2 B
WHERE A.COLUMN3 = B.COLUMN3 AND A.COLUMN4 = B.COLUMN4
)
WHERE EXISTS (SELECT B.B_COLUMN2
FROM TABLE2 B
WHERE A.COLUMN3 = B.COLUMN3 AND A.COLUMN4 = B.COLUMN4);

What is the T-SQL Syntax for If This or This then That else Do Nothing?

I am trying to make a condition where for a certain ID, when either of two values from two different tables are greater than a number, then I will display a row with both values. Otherwise, I don't want to display any new row. What is the correct syntax for this?
if(select
a.Column1 > 2 or
b.Column2 > 2
from
Table1 a join Table2 b on a.ID = b.ID)
begin
select
a.Column1,
b.Column2
from
Table1 a join Table2 b on a.ID = b.ID)
end
else
begin
Don't Select
end
You just need to add it as a where condition. If your where condition fails for a given row, that row wouldn't be selected.
select
a.Column1,
b.Column2
from
Table1 a join Table2 b on a.ID = b.ID
where a.column1 > 2 or b.column2 > 2
#vkp's answer is probably what you want, but the literal translation of the query you have written -- without using control-flow statements -- is this:
select
a.Column1,
b.Column2
from
Table1 a join Table2 b on a.ID = b.ID
where exists (select 1 from Table1 c join Table2 d on c.ID = d.ID where c.Column1 > 2 or d.Column2 > 2);
This will either return nothing at all if one of records in the join doesn't have Table1.Column1 > 2 or Table2.Column2 > 2, or it will return all records.

Trouble with a conditional join

I'm trying to join depending on whether table1.column1 is null or not null.
For, example I have two tables, table1 and table2, and the query:
SELECT
A.column2
FROM table1 A , table2 B
WHERE if A.column1 is not null then (A.column1=B.column1)
else if A.column1 is null then (A.column3 = B.column1);
Try this:
SELECT
A.column2
FROM table1 A
JOIN table2 B ON
B.column1 = A.column1 OR
(A.column1 IS NULL AND B.column1 = A.column3)
Note that B.column1 = A.column1 will never be true if either B.column1 or A.column1 is NULL.
Try this...
SELECT A.column2
FROM table1 A
JOIN table2 B ON NVL(A.column1, A.column3) = b.Column1
If b.Column1 can also be null, and you want to compare two null columns, you can try this...
SELECT A.column2
FROM table1 A
JOIN table2 B ON NVL(b.Column1, 'X') = COALESCE(A.column1, A.column3, 'X')
Assuming b.Column1 can never have the value 'X'

Using a Oracle subselect to replace a CASE statement

Hy guys,
can anybody please help me with a subquery in Oracle database 10g? I need to extract the values for a column in the first table as value of another column in the second table.
I currently use this statement:
SELECT
CASE WHEN A.column1 = 'A' THEN 'aaa'
WHEN A.column1 = 'B' THEN 'bbb'
.......
WHEN A.column1 = 'X' THEN 'xxx'
ELSE 'bad' END AS COLUMN1, A.*
FROM TRANSACTION_TABLE A, CATEGORY_TABLE B
WHERE A.column1 IS NOT NULL
AND A.column1 <> ' '
This is not an elegant approach, so I'm trying to use a subselect from CATEGORY_TABLE B like the following:
SELECT A.column1, A.*
FROM TRANSACTION_TABLE A, CATEGORY_TABLE B
WHERE A.column1 IS NOT NULL
AND A.column1 = B.column_b_1
AND A.column1 <> ' '
AND A.column1 IN (SELECT B.column_b_1_descr FROM CATEGORY_TABLE B
WHERE B.FIELDNAME = 'column1' AND A.column1 = B.column_b_1)
So, I cannot get any results by using the subquery and don't want to continue using the CASE against many conditions, just want to replace the A.column1 values with the descriptive values from B.column_b_1_descr , as they're easier to read.
I would appreciate any feedback.
Thanks
Unless I'm misunderstanding your question...
CATEGORY_TABLE:
name | value
A aaa
B bbb
C ccc
...
SELECT B.value AS COLUMN1, A.\*
FROM TRANSACTION\_TABLE A, CATEGORY\_TABLE B
WHERE A.column1 = B.name
or
SELECT t2.value as COLUMN1, t1.\*
FROM TRANSACTION\_TABLE t1
INNER JOIN CATEGORY\_TABLE t2 ON t1.column1 = t2.name;
The where clause isn't needed, since an inner join automatically excludes rows with null values or no matches.