sam2929
asked on
Unpivot data
Hi,
I have table a
Id class1
123. 001
123. 002
Then table B
Id. Class1 class2 class3
123. 002. 001 002
Table b has like 50 class1 till class 50
I don’t want to do or query is it easy way to join two tables and see if table a column1 data is in table b column 1 to 49.
I have table a
Id class1
123. 001
123. 002
Then table B
Id. Class1 class2 class3
123. 002. 001 002
Table b has like 50 class1 till class 50
I don’t want to do or query is it easy way to join two tables and see if table a column1 data is in table b column 1 to 49.
Are all class columns in tableB the same type as the Class1 column in tableA ? Will this be a one time effort or an ongoing need?
ASKER
Yes they will be same and need will be ongoing
ASKER
Pawan I need to compare two tables if data in table a then try to check all columns in tableb to find matching data
What version of Oracle are you using?
Also, is the data type number or varchar2?
ASKER
Oracle 11 datatype varchar2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what are the results you are expecting?
You may need to provide more sample data in order to post a meaningful result set.
if you're just looking for the table A data that also exists in table B, the "exists" clause should work nicely
WITH
tablea
AS
(SELECT 123 id, '001' class1 FROM DUAL
UNION ALL
SELECT 123, '002' FROM DUAL),
tableb AS (SELECT 123 id, '002' class1, '001' class2, '002' class3 FROM DUAL)
SELECT *
FROM tablea a
WHERE EXISTS
(SELECT NULL
FROM tableb b
WHERE a.id = b.id AND a.class1 IN (b.class1, b.class2, b.class3))
If that's not what you're looking for, please illustrate with a representative sample set and expected results.
You may need to provide more sample data in order to post a meaningful result set.
if you're just looking for the table A data that also exists in table B, the "exists" clause should work nicely
WITH
tablea
AS
(SELECT 123 id, '001' class1 FROM DUAL
UNION ALL
SELECT 123, '002' FROM DUAL),
tableb AS (SELECT 123 id, '002' class1, '001' class2, '002' class3 FROM DUAL)
SELECT *
FROM tablea a
WHERE EXISTS
(SELECT NULL
FROM tableb b
WHERE a.id = b.id AND a.class1 IN (b.class1, b.class2, b.class3))
If that's not what you're looking for, please illustrate with a representative sample set and expected results.
@Author - We can use UNPIVOT and JOIN method to get the required data.
Table Creation and data generation.
Note - Tested on http://rextester.com/l/oracle_online_compiler
Table Creation and data generation.
CREATE TABLE MyClass
(
Id INTEGER
,Class1 DECIMAL(10,5)
,class2 DECIMAL(10,5)
,class3 DECIMAL(10,5)
)
\\
INSERT INTO MyClass VALUES(123,0.002,0.001,0.002)
\\
CREATE TABLE classes
(
Id INT
,class1 DECIMAL(10,3)
)
\\
INSERT INTO classes VALUES (123,.001)\\
INSERT INTO classes VALUES (123,.002)\\
SOLUTIONWITH CTE
AS
(
SELECT Id,Class1,Class2,Class3,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Id) rnk FROM MyClass
)
,CTE1 AS
(
SELECT Id,Class FROM CTE
UNPIVOT
(
class
FOR r
IN (Class1, Class2, Class3)
)
)
SELECT * FROM CTE1 c INNER JOIN classes c1
ON c.Class = c1.class1
\\
OUTPUT ID CLASS ID CLASS1
1 123 0,002 123 0,002
2 123 0,001 123 0,001
3 123 0,002 123 0,002
Note - Tested on http://rextester.com/l/oracle_online_compiler
Table creation and data generation
Open in new window
SOLUTION
Open in new window
OUTPUT
Open in new window