Here is what I needed:
I have:
Table A PT*,NAME,AGE
Table B PT*,COURSE,RESULT
Table C PT*,COURSE,RESULT,RANk
Wondering how could I UNION Table B and Table C and later Merge with Table A to get the output as below(Table D). PT is the PKey among all.
PT NAME AGE COURSE RESULT RANK
100 SLK 29 Test1 29 - result of merge between Table A and Table B
200 AAR 30 Test2 23 10 - result of merge between Table A and Table C
To get desirable output just perform a simple SQL SELECT query:
SELECT A.PT, A.NAME, A.AGE, B.COURSE, B.RESULT, C.RANK
FROM
A join B on A.PT=B.PT
join C ON A.PT = C.PT
Why you need COURSE, RESULT fields in table C, by the way, as they are already stored in table B?
Related
I’m using oracle. I have two table A and B
Table A has 8000 rows and 5 five columns
Table B has 5500 rows and same 5 columns
All of 5500 rows in table B are contained in Table A and they are the same
I have a query like
With t1 as (select distinct(id) from table A/B)
,T2 as (select a.id, c.value, d.value from Table A/B a
Join table c c on “conditions”
Join table d d on “conditions”
) select * from t2
So the query with Table A works excellent but with Table B it freezes for eternity.
Data types and other properties are equal in table A and table A.
Where should i look for the problem?
I tried to explain plan but differences only in row “PX PARTITION HASH JOIN-FILTER” in Table A and “PX BLOCK ITERATOR ADAPTIVE” in table B
I have two tables (table A and table B) that have a 1 to many mapping. For every record in table A, I want to check if any of its events in table B occur after 2010. For example:
Table A Table B
ID REGISTER ID DATE
A qwer A 1995-01-01
B ghlk A 1997-01-31
C thasdj A 2006-03-15
B 2001-03-15
B 2003-04-03
B 2021-08-01
B 1995-01-01
C 2001-01-01
C 2010-01-01
Therefore, the resulting Table would be
Table C
ID Register
A qwer
C thasdj
Because for ID A and C, none of their events happens after 2010.
THis is the script I tried using but I'm not sure why it's not working. Any help
SELECT *
INTO Table C
FROM Table A
where ID not in(
SELECT distinct ID from Table B
where [DATE] >= 2011-01-01
you can do it with insert into {tablename} (list column) select syntax
INSERT INTO C ( ID, Register )
SELECT A.ID, A.Register
FROM A
WHERE A.ID not in (
SELECT distinct ID from Table B
where [DATE] >= 2011-01-01
)
You can use not exists for this task. Presumably your example query is contrived however note you must properly delimit object names that contain spaces, are reserved words etc and a date value must be quoted.
select *
into TableC
from TableA a
where not exists (
select * from TableB b
where b.Id = a.Id and b.[Date] >='20110101'
);
I have created two tables as below in hive
create table test1(id string);
create table test2(id string);
test1 has values as given below
1
1
test2 has values as given below
1
1
When I am joining these two tables I am getting output with
1
1
1
1
This is the query used :
select a.id from test1 a,test2 b where a.id=b.id;
Please help I expected the output to be as
1
1
I am using cloudera distribution
Better use ANSI join syntax:
select a.id
from test1 a
inner join test2 b on a.id=b.id
The expected output cannot be the result of your join because for each a.id all matching rows from a and b are selected. For the first row from a it will be two matching rows in b. For the second row from a it will be also two matching rows from b. So it will be four rows totally.
You can apply distinct to the second table before join for example.
select a.id
from test1 a
inner join (select distinct b.id from test2 b) b on a.id=b.id
In this case for each row in table a it will be single matching row in table b.
See this lesson to understand JOINS better: https://www.coursera.org/learn/analytics-mysql/lecture/kydcf/joins-with-many-to-many-relationships-and-duplicates
I am trying to subtract sequences MN_SEQ from Table C generated based on join with other tables.
Here is the problem.
Query 1 -
Select M_Seq from Table C, Table A, Table B where C.date_sk=A.MTH_END_DT
and B.Loan_seq=A.Loan_seq
Query 2 -
Select M_Seq from Table C, Table B where C.date_sk=B.ORIG_DT
I have to get difference between 2 M_SEQ generated from the result set of query 1 and Query 2.
Below is what i tried, but I am getting error.
select mn_seq -mn_seq from
((select mn_seq from Table C, Table A, Table B where B.MTH_END_DT=C.DATE_SK and B.LOAN_SEQ=A.LOAN_SEQ)a,
(select mn_seq from Table C , Table B where B.ORIG_DT=C.DATE_SK
)b)
T
Kindly provide inputs . I am not sure if this is the right way to do it. I tried just using "-" between queries but didnt work. Thanks!
Try this..
SELECT (SELECT mn_seq
FROM TABLE c, TABLE a, TABLE b
WHERE b.mth_end_dt = c.date_sk
AND b.loan_seq = a.loan_seq) -
(SELECT mn_seq FROM TABLE c, TABLE b WHERE b.orig_dt = c.date_sk)
FROM dual
I assume both the mn_seq are NUMBER and also your WHERE clause returns only one record in each of the inner queries.
I have two tables A and B with 30 columns (same variable names, data for different year), no primary key, almost a million records each.
I want to compare A.X1 with B.X1 (nvarchar8, contains spaces, -, letter and numbers) and insert the outer join results in another table C (with same 30 columns) so I have all rows of A and where B!=A on B.X1).
Example:
Table A
X1 X2 X3 ..... X30
11 E R ..... G
12 R 4 L
13 S 5 NULL
14 D T NULL
Table B
X1 X2 X3 ..... X30
11 E R ..... G
12 R 4 L
15 R2 56 NULL
16 R1 T1 NULL
Resulting table C
X1 X2 X3 ..... X30
11 E R ..... G
12 R 4 L
13 S 5 NULL
14 D T NULL
15 R2 56 NULL
16 R1 T1 NULL
How do I do that.
I tried
INSERT INTO C
SELECT *
from A
full outer join B
on A.X1 = B.X1
Error I get
Msg 213, Level 16, State 1, Line 1
Insert Error: Column name or number of supplied values does not match table definition.
I have C created, which is currently empty.
insert C
select *
from A
union all
select *
from B
where not exists
(
select *
from A
where X1 = B.X1
)
Your query will not work becasue you are joining the tables and retunring *, with ill results in twice the number of columns that you need.
What you really want to do is select everything from table A then APPEND (rather than join) all the records from table B.
Appends are achieved by using a UNION. Here is some sample code. Note: never, ever use SELECT *. Addapt the follwing to include the specifically named fields in the correct order.
Also, I am using UNION rather than UNION ALL so that the query automaticlly excludes the records in B that are duplicates of records in A.
SELECT FIELDS...
FROM TABLEA
UNION
SELECT SAME_FIELDS...
FROM TABLEB
Insert Into TableC
(
-- List your fields explicitly
)
Select
-- List all tableA.Fields explicitly
From tableA
Left Outer Join tableB On tableB.X1 = tableA.X1
Where tablB.X1 IS NULL