SQL - Null value should match with not null value with another table - sql

Table 1
Column1 Column2 Column 3
A Null 12
B Null 15
C 0 15
Table 2
Column2 Column3
0 15
0 12
I have table 1 and Table 2 , Here I'm passing table 2 parameters to table 1 which should return the column1 but it should match with Null values like the below scenario
If I pass (0, 15) to table 1 then it should return 'C' not 'B'.
If I Pass (0,12) to table 1 then it should return 'A'
Anytime it should return one value not multiple vales.
Could you please help me with this logic ?

You could do this as a union:
select Column1 from
(
select Table1.Column1 as Column1,Table1.Column2 as Column2
from Table1
join Table2 on
Table1.Column2=Table2.Column2 and Table1.Column3=Table2.Column3
UNION
select Table1.Column1 as Column1,Table1.Column2 as Column2
from Table1
join Table2 on
Table1.Column2 is NULL and Table1.Column3=Table2.Column3
) as Unioned
ORDER BY Column2 NULLS LAST
LIMIT 1;
The UNION discards duplicates, but you could always wrap the entire statement in another SELECT if you explicitly need to tell it to return at most one value.

select *
from table2 t
left outer join table1 t1 on t.column2=t1.column2 and t.column3=t1.column3
left outer join table1 t2 on t2.column2 is null and t.column3=t2.column3
where t1.column2 is not null or t2.column3 is not null

SQL Server
WITH T1
as
(SELECT COLUMN1,COLUMN2,
case when COLUMN2 is null then 0 else COLUMN2 end AS TRANSFORMED_COLUMN2,
row_number() OVER (partition by isnull(column3,2) ORDER BY isnull(COLUMN2,2) asc) AS rnk,
COLUMN3
FROM TABLE1)
SELECT
T1.COLUMN1,
T2.COLUMN2,
T2.COLUMN3
FROM
T1
INNER JOIN
(SELECT COLUMN2,COLUMN3 FROM TABLE2) T2
ON T1.TRANSFORMED_COLUMN2 = T2.COLUMN2
AND T1.COLUMN3=T2.COLUMN3
where T1.rnk=1
By using CTE, you will running through the tables just once.

SELECT t1.* FROM table1 t1
INNER JOIN table2 t2
ON NVL(t1.column2,0) = t2.column2 and t1.column3 = t2.column3

Related

How to create table with binary values based on existing some values in two other tables in Teradata SQL?

I have two tables in Teradata SQL like below:
Table1:
ID
10
11
12
Table2:
ID
10
13
14
15
Based on two tables above I need to create one table like below.
So:
col: tab1 --> If ID is in table 1 give them 1 and 0 otherwise.
col: tab2 --> If ID is in table 2 give them 1 and 0 otherwise.
Desired result:
ID
tab1
tab2
10
1
1
11
1
0
12
1
0
13
0
1
14
0
1
15
0
1
How can I do that in Teradata SQL ?
Teradata seems to support enough of ISO SQL-2003 natively, so no Teradata-specific SQL extensions or proprietary features is needed (i.e. the exact same query will work in MSSQL Server, Oracle, MariaDB, etc).
You'll want a UNION of table1 and table2's values and then JOINed back, which is straightforward:
WITH distinctIdValues AS (
SELECT id FROM table1
UNION
SELECT id FROM table2
)
SELECT
dv.id,
CASE WHEN t1.id IS NOT NULL THEN 1 ELSE 0 END AS tab1,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS tab2
FROM
distinctIdValues AS dv
LEFT OUTER JOIN table1 AS t1 ON dv.id = t1.id
LEFT OUTER JOIN table2 AS t2 ON dv.id = t2.id
You can then use this query either as a VIEW or materialize it into a new TABLE:
CREATE VIEW foobar AS /* same SQL as above */;
SELECT * FROM foobar;
Teradata's documentation is unclear about how/if a CTE can be used with an INSERT statement, so I'll use an inner-query instead:
CREATE TABLE foobar (
id int NOT NULL PRIMARY KEY,
tab1 byteint NOT NULL,
tab2 byteint NOT NULL
);
INSERT INTO foobar ( id, tab1, tab2 )
SELECT
dv.id,
CASE WHEN t1.id IS NOT NULL THEN 1 ELSE 0 END AS tab1,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS tab2
FROM
(
SELECT id FROM table1
UNION
SELECT id FROM table2
)
AS dv
LEFT OUTER JOIN table1 AS t1 ON dv.id = t1.id
LEFT OUTER JOIN table2 AS t2 ON dv.id = t2.id
ORDER BY
dv.id
;
Or just this:
Props to #dnoeth for reminding me that it can be reduced to this:
SELECT
COALESCE( t1.id, t2.id ) AS id,
CASE WHEN t1.id IS NULL THEN 0 ELSE 1 END AS tab1,
CASE WHEN t2.id IS NULL THEN 0 ELSE 1 END AS tab2
FROM
table1 AS t1
FULL OUTER JOIN table2 AS t2 ON t1.id = t2.id
ORDER BY
COALESCE( t1.id, t2.id )
You just need a Full Outer Join:
SELECT
Coalesce(t1.id, t2.id) AS id
,CASE WHEN t1.id IS NULL THEN 0 ELSE 1 END AS tab1
,CASE WHEN t2.id IS NULL THEN 0 ELSE 1 END AS tab2
FROM table1 AS t1
FULL JOIN table2 AS t2
ON t1.id = t1.id

Oracle Sql - I have two tables and need to filter table1 with results from table2. I need to return all of table1 if table2 is empty

I'm currently turning a comma-separated string into a number table with the field name of ID. I'm then trying to do an nvl to select all if the generated table is null.
table1.ID = NVL(table2.ID, table1.ID)
I have two tables and need to filter table1 with results from table2. I need to return all of table1 if table2 is empty.
Scenario I
Table1
ID
1
2
3
4
Table2 (Empty)
ID
Return rows 1, 2, 3, 4
Scenario II
Table1
ID
1
2
3
4
Table2
ID
2
3
Return rows 2, 3
You can use filtering in the where clause:
select t1.id
from table1 t1
where not exists (select 1 from table2) or
exists (select 1 from table2 t2 where t2.id = t1.id);
I don't think join is the right way to express this logic.
You can also use UNION
select t1.id
from table1 t1
where not exists (select 1 from table2 where id = t1.id) union all
select t2.id
from table2 t2
where exist (select 1 from table1 where id = t2.id);

SQL to get the common rows from two tables

I have two tables T1 and T2.
Can any one please help with a SQL query which will fetch the common rows from these two tables? (Assume T1 and T2 has 100 columns each)
P.S : I guess INNER JOIN on each of the columns will not be a good idea.
Thanks
If you are using SQL Server 2005, then you can use Intersect Key word, which gives you common records.
SELECT column1
FROM table1
INTERSECT
SELECT column1
FROM table2
If you want in the output both column1 and column2 from table1 which has common columns1 in both tables.
SELECT column1, column2
FROM table1
WHERE column1 IN
(
SELECT column1
FROM table1
INTERSECT
SELECT column1
FROM table2
)
Use INTERSECT
SELECT * FROM T1
INTERSECT
SELECT * FROM T2
Yes, INNER JOIN will work.
eg. SELECT (column_1, column_2, ... column_n) FROM T1 JOIN T2 ON (condition) WHERE (condition)
This query will fetch the common records (intersection of) in both the tables according to ON condition.
select
t1.*
from
t1, t2
where
(
(t1.col1 is null and t2.col1 is null) or (
(t1.col1 = t2.col1 )
) and
(
(t1.col2 is null and t2.col2 is null) or (
(t1.col2 = t2.col2 )
) and
(
(t1.col3 is null and t2.col3 is null) or (
(t1.col3 = t2.col3 )
) and
....
(
(t1.col100 is null and t2.col100 is null) or (
(t1.col100 = t2.col100 )
)
SELECT NAME FROM Sample1
UNION
SELECT NAME FROM Sample2;
EX: Table Sample1
ID NAME
-------
1 A
-------
2 B
-------
Table Sample 2
ID NAME
--------
1 C
--------
2 B
------
Output
NAME
----
A
---
B
---
C
---

Oracle function to distinct NULL values based on other column

I am looking for the simplest solution that will resolve following problem.
I have a table:
Column1 Column2
------- -------
A 11
A NULL
B 12
B 14
B NULL
C NULL
I would like to query(Select) this table to achive only NULL value when this is the only value for distinct value of column1. When there is at least one non null value with column1 it is important to bypass then null values for them. Desired outcome:
Column1 Column2
------- -------
A 11
B 12
B 14
C NULL
I was trying with COALESCE, NULLIF.. etc and had no results. I would like to achieve this with simplest solution. I am joining then Column2 with column in other table but for NULL I hope left join could be appropriate .
I am very grateful for any help
Try this:
SELECT column1, column2
FROM dummy t1
WHERE column2 IS NOT NULL
OR NOT EXISTS (SELECT ''
FROM dummy t2
WHERE t1.column1 = t2.column1
AND column2 IS NOT NULL)
Keeps all rows for which column2 either not NULL or are NULL and are the only within in a group.
Should work like this ( there might be a more perfomant way though )
select column1, column2
from table where column2 is not null
union
( select column1, column2
from table
where column2 is null
and column1 not in ( select column1
from table
where column2 is not null
group by column1)
)
The first select above the union gets all rows without null values, then the second one simply adds all rows with null values in column2, but not the ones you had allready an the first one. group by not necessary for the result, but might clearify the logic.
You can use a left join, first get the values you need from the table and then join it to itself.
Shown here as a CTE for clarity.
WITH Col1Values AS
(
SELECT DISTINCT Column1
FROM Table
)
SELECT Col1Values.Column1, T2.Column2
FROM Col1Values
LEFT JOIN Table T2 ON Col1Values.Column1 = T2.Column1
or with a sub-query
SELECT T1.Column1, T2.Column2
FROM (
SELECT DISTINCT Column1
FROM Table
) T1
LEFT JOIN Table T2 ON T1.Column1 = T2.Column1
Inner query: Use analytic function to compute how many non-null C2 values each C1 has
Outer query: filter for rows with non-null C2 or count-distinct-C2=0
SELECT C1, C2
FROM (
SELECT C1, C2, COUNT(C2) OVER (PARTITION BY C1) C2_COUNT
FROM TABLE
)
WHERE C2 IS NOT NULL OR C2_COUNT = 0;
Join version of same solution (no analytic functions)
SELECT C1, C2
FROM TABLE
NATURAL INNER JOIN
(SELECT C1, COUNT(C2) C2_COUNT
FROM TABLE GROUP BY C1)
WHERE C2 IS NOT NULL OR C2_COUNT = 0;
Try this
select c1, c2 from t where c2 in (select distinct c2 from t where c2 is not null);

Find where a row of data in one column doesn't exist in another column

so I'm trying to figure out a way to compare each and every row of a column against all the rows in another column and filter out those that doesn't exist in the column that it is comparing to. I understand how to compare the two columns on a row-by-row basic like this:
select table1.column1
from table1 inner join
table2 on table1.id = table2.id
where column1 <> column2
But I want to compare the rows from table1 in column1 against ALL the rows of column2 in table2 and find rows in column1 that doesn't exist in column2 at all. So it would be something like columns with these values:
Column1 Column2
1 2
2 1
4 3
5 5
7 6
And after the SQL it would become something like this:
Column1
4
7
Try with NOT IN:
select table1.column1
from table1
where table1.column1 not in (select table2.column2 from table2)
select column1 from table1 as t1 where not exists
(select column2 from table2 as t2 where t1.id=t2.id)
If you compare column between 2 tables
SELECT table1.column1 FROM table1
WHERE table1.column1 NOT IN
(SELECT table2.column2 as column1 FROM table2)
If you compare column1 with column2 against same table
SELECT table1.column1 FROM table1
WHERE table1.column1 NOT IN
(SELECT table1.column2 as column1 FROM table1)
select t1.id
from ... t1
left join ... t2 on t1.id = t2.id
where t2.id is null