I am having Table1 with Column A and Table2 with Column B.
When I use join, (Table1.A = Table2.B) I need to create a temp column 'Flag' and set Flag value as '1' for matching records and for remaining records(Table1.A != Table2.B) should have flag value '0'.
Result set should have column from both the table and flag value.
Thanks in advance.
You can use LEFT JOIN with ISNULL as below:
SELECT *,CASE WHEN t2.B IS NULL THEN 0 ELSE 1 END Flag
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.A=t2.B
For displaying unmatched values also from right table
SELECT *,CASE WHEN t1.A IS NULL OR t2.B IS NULL THEN 0 ELSE 1 END Flag
FROM Table1 t1
FULL OUTER JOIN Table2 t2 ON t1.A=t2.B
Try with FULL OUTER JOIN .
SELECT *,CASE WHEN t1.A = t2.B THEN 1 ELSE 0 END Flag
FROM Table1 t1
FULL OUTER JOIN Table2 t2 ON t1.A=t2.B
Check below example:
DECLARE #tblA AS TABLE
(
ID INT,
A VARCHAR(50)
)
DECLARE #tblB AS TABLE
(
ID INT,
B VARCHAR(50)
)
INSERT INTO #tblA VALUES(1,'AA'),
(2,'CCC'),
(3,'DDD'),
(4,'FFF')
INSERT INTO #tblB VALUES(1,'AA'),
(2,'BBB'),
(3,'DDD'),
(4,'KKK')
SELECT
A.A,
B.B,
CASE ISNULL(B.B,'') WHEN '' THEN 0 ELSE 1 END AS Match
FROM #tblA A
LEFT JOIN #tblB B ON A.A=B.B
Related
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
I have this scenario question which needs to be solved using joins only. can't use except,intersect or union.
sample code:
--demo setup
create table t1 (id int)
insert into t1 values (1),(2),(3)
create table t2 (id int)
insert into t2 values (4),(5),(6)
--join
select t1.id,t2.id
from t1 full outer join
t2 on t1.id=t2.id
--after join i am getting
id id
----------- -----------
1 NULL
2 NULL
3 NULL
NULL 4
NULL 5
NULL 6
--But i need is
id
-----------
1
2
3
4
5
6
Can someone help me with this ? i know this can be easily done using union but this challenge needs to be solved using joins only.
any help is appreciated ....
--solved the challenge after adding case to the code.
select case when t1.id is null then t2.id else t1.id end as id
from t1 full outer join
t2 on t1.id=t2.id
you can use this code:
SELECT case when t1.id IS NULL THEN t2.id ELSE t1.id END AS id
FROM t1 FULL OUTER JOIN t2
ON t1.id=t2.id
As you said the Use From Union
select * from t1
union
select * from t2
Although UNION ALL is the better option, you can use this following script-
SELECT
CASE WHEN C.ID1 IS NULL THEN C.ID2 ELSE C.ID1 END V
FROM
(
SELECT * FROM
(
SELECT 'T1' TabName1,id ID1 FROM #t1
)A
FULL JOIN (
SELECT 'T2' TabName2,id ID2 FROM #t2
)B
ON A.TabName1 = B.TabName2
)C
ORDER BY 1
I have two tables like the following:
Table1
Id Table1_Col
1 A
2 B
3 C
4 D
5 E
Table2
Id Table1_Col Table2_Col
1 A Test
I want the count of (Table1_Col) in Table2 and I need query for the following output:
Expected Output
Table1_Col Count_Table2_Col
A 1
B 0
C 0
D 0
E 0
What I have tried so far:
select Table1_Col,Count(Table2_Col) from table1 t1
Left outer join table2 t2 on t1.Table1_Col = t2.Table1_Col
Please provide me a proper solution for this.
You need GROUP BY, when using aggregate methods. Also Table1_Col existing in both tables, so please use with the proper table alias for the columns.
The query below will return your expected result. Please find the demo too.
select T1.Table1_Col, Count(T2.Table2_Col) AS Table2_Col
from table1 t1
Left outer join table2 t2 on t1.Table1_Col = t2.Table1_Col
GROUP BY T1.Table1_Col
Demo on db<>fiddle
UPDATE: As per the comment in the post, based on your fiddle, the condition t3.visitno=1 should be in the LEFT OUTER JOIN and not in the WHERE clause, so the following query will work:
select t3.pvisitno, t1.DocName, count(t2.vdocid) as [count]
from Document_type t1
left outer join visitdocs t2 on t2.DocId = t1.DocId
left outer join visittbl t3 on t3.visitno = t2.visitno and t3.visitno=1
group by t3.pvisitno,t1.DocName
order by count(t2.vdocid) desc
db<>fiddle demo for the revised fiddle
Try this query:
select t1.Table1_Col,
sum(case when Table2_Col is null then 0 else 1 end) Count_Table2_Col
from Table1_Col t1
left join Table2 t2 on t1.Table1_Col = t2.Table1_Col
group by t1.Table1_Col
You can try this:
Declare #t table ( id int ,col varchar(50))
insert into #t values (1,'A')
insert into #t values (2,'B')
insert into #t values (3,'C')
Declare #t1 table ( id int ,col varchar(50),col2 varchar(50))
insert into #t1 values (1,'A','TEST')
select t.col,count(t1.id) countT2 from #t t left join #t1 t1
on t.id=t1.id
group by t.col
Here's another option:
select t1.Table1_Col, coalesce(x.cnt, 0) cnt
from table1 t1
left outer join (select Table2_Col, count(*) cnt from table2 group by Table2_Col) x
on x.Table2_Col = t1.Table1_Col;
The idea here is to create an inline view of table2 with its counts and then left join that with the original table.
The "coalesce" is necessary because the inline view will only have records for the rows in table2, so any gaps would be "null" in the query, while you specified you want "0".
I have a table with two columns. I want to compare each row of the first column with all rows in the second column. If matched I need to insert yes in the 3rd column, if not I need to insert no.
First second
A B
C D
E A
X c
Output should be
First second yesOrno
A B yes
C D yes
E A NO
X c NO
Use an EXISTS with the same table.
UPDATE T1 SET
ThirdColumn = CASE WHEN EXISTS (
SELECT
'a SecondColumn match exists'
FROM
YourTable AS T2
WHERE
T1.FirstColumn = T2.SecondColumn) THEN 'Yes' ELSE 'No' END
FROM
YourTable AS T1
SELECT
First, Second,
CASE WHEN EXISTS (
SELECT * FROM Table t2
WHERE t2.Second = t1.First)
THEN 'Yes' ELSE 'No' END AS YesOrNo
FROM
Table t1
you can get it by self join as below
select T1.First,T1.second,(case when T2.second is null then 'no' else 'yes' end) as yesOrno from T as T1 left join T as T2
on T1.First=t2.second
check sql fiddle : SQL Fiddle
You can do it like this:
declare #t table (
C1 char(1),
C2 char(1)
)
insert #t
values ('A','B'),
('C','D'),
('E','A'),
('X','C')
select t1.C1,t1.C2, case when t2.C2 is null then 'No' else 'Yes' end YesOrNo
from #t t1 left join #t t2
on t1.C1 = t2.C2
There are many different ways of writing this basic query.
I work with student data and have two tables with the same structure but different data sets. I would like to add a new column with a record of '0' or '1' to determine whether or not the student appears in the other table. Example:
Table 1:
s_id s_year s_term s_crs NewColumn(was student enrolled in 2016?)
123456 2017 Fall Math 1010 1
654321 2017 Fall Eng 1010 0
Table 2:
s_id s_year s_term s_crs
123456 2016 Fall Math 2010
432516 2016 Fall Eng 2010
How would you go about doing this?
SELECT s_id, s_year, s_term, s_crs
(CASE
WHEN S_ID IN (select s_id from table2)
THEN '1'
or something of that nature? I am unsure how to write this with a join
You could left join with the second table, and see if it the resulting column is not null:
SELECT t1.s_id, t1.s_year, t1.s_term, t1.s_crs,
CASE WHEN t2.s_id IS NOT NULL THEN 1 ELSE 0 END AS newcolum
FROM table1 t1
LEFT JOIN table2 t2 ON t1.s_id = t2.s_id
assuming table 1 is the result ... using subquery solely based on the s_id and s_year... if there is another requirement please update OP.
SELECT
s_id,
s_year,
s_term,
s_crs,
ISNULL((SELECT
1
FROM table2 t2
WHERE t2.s_id = t1.s_id
AND t2.s_year = 2016), 0) [NewCol 2016]
FROM table1 t1
Assuming the s_id is a common identificator
SELECT t1.s_id, t1.s_year, t1.s_term, t1.s_crs, 1 as NewColumn
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.s_id = t2.s_id)
UNION
SELECT t1.s_id, t1.s_year, t1.s_term, t1.s_crs, 0 as NewColumn
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.s_id = t2.s_id)
You can do this
ALTER TABLE Table1 ADD NewColumn BIT NOT NULL DEFAULT (0)
At this point all your items in the Table1 will be 0, now you just update the needed items with 1
UPDATE Table1
SET NewColumn = 1
WHERE ID IN (SELECT ID FROM Table2)
You can use Left join
SELECT t1.s_id, t1.s_year, t1.s_term, t1.s_crs,
CASE WHEN t2.s_Year = '2016' THEN 1 ELSE 0 END AS [NewColumn(was student enrolled in 2016?)]
FROM table1 t1
LEFT JOIN table2 t2 ON t1.s_id = t2.s_id
create table #tbl1
(
s_id int,
s_year varchar(4),
s_term varchar(4),
s_crs varchar (10)
)
insert into #tbl1 values(123456,'2017','Fall','Math 1010')
insert into #tbl1 values(654321,'2017','Fall','Eng 1010')
create table #tbl2
(
s_id int,
s_year varchar(4),
s_term varchar(4),
s_crs varchar (10)
)
insert into #tbl2 values(123456,'2016','Fall','Math 2010');
insert into #tbl2 values(432516,'2016','Fall','Eng 2010')
select a.s_id,a.s_year,a.s_term,a.s_crs,case when b.s_id is null then '0' else '1' end as NewColumn
from #tbl1 a left outer join #tbl2 b on a.s_id = b.s_id
drop table #tbl1
drop table #tbl2