There are two tables (A, B) in my database and I'm trying to join them and print the same column of table B twice in the result table. The problem is that I need to put in compliance with two different columns of table A, so to join these tables also twice. I tried to join them and put in the select expression required column two times, but in this case result table shows the same relation in every column.
Here is the result I want, where the id column of table B includes id_1 and id_2 from table A and the output names correspond to ids from table A.
Table A: (integer id_1, integer id_2)
Table B: (integer id, varchar name)
Result Table: (id_1, name, id_2, name)
You need inner join with tableB twice as follows:
select a.id_1, b1.name, a.id_2, b2.name
from tableA a join tableB b1 on a.id_1 = b1.id
join tableB b2 on a.id_2 = b2.id
for example I have 3 tables,
First Table
|PrjID|ProjectType|
1 Sample
Second Table
|PrjID|ProjectEng|
1 Joe Doen
Third Table
|PrjID|ProjectDate|
1 20112020
I want to sum values in one table with the same ID number in different tables like this :
|PrjID|ProjectType|ProjectEng|ProjectDate|
1 Sample Joe Doen 20112020
I have about 150 data tables. How can I sum values in one table with the same ID number in different tables with query?
Assuming you are having another fourth table containing PrjID and containing values in the val column and you want to sum val column.
SELECT f.PrjID, s.ProjectEng, t.ProjectDate, SUM(f.val) --Assuming you want to sum val column
FROM FirstTable as f
INNER JOIN SecondTable as s
ON s.PrjID = f.PrjID
Inner Join ThirdTable as t
ON t.PrjID = f.PrjID
Inner Join FourthTable as fo --Assuming this table has value column
ON fo.PrjID = f.PrjID
GROUP BY f.PrjID, s.ProjectEng, t.ProjectDate
Do you just want join?
select t1.*, t1.projecttype, t2.projecteng, t3.projectdate
from t1 join
t2
on t1.prjid = t2.prjid join
t3
on t1.projid = t3.projid;
I am trying to get certain records from one table that don't exist in another by multiple fields. For example, if I have table 1 like:
Account Animal
123 dog
456 cat
789 bird
And table 2 like:
Account Animal
123 cat
456 cat
What I want the final table to be is:
Account Animal
123 dog
Here is what I tried:
select
a.*
from
table1 left join table2
on a.account=b.account and a.animal=b.animal
where
b.animal is null
You seem to want where the accounts match but the animals are not the same.
This suggests an inner join and filtering:
select a.*
from table1 a join
table2 b
on a.account = b.account
where a.animal <> b.animal ;
This works for your sample data, but it does make assumptions about not having duplicates.
I am trying a left join in Hive Query, but it does not seem to work. It returns me columns only from left table:
create table mb.spt_new_var as select distinct customer_id ,target from mb.spt_201603 A
left outer join mb.temp B
on (A.customer_id=B.cust_id);
I tried selecting few records from table B based on the some random customer_id from table A and it returns some records. But if I try the left join on table A, it returns me only columns from table A. The data-type of both the IDs is same(int). what could be the possible reason behind this?
Sample Table A:
Customer_account_id target
12356 1
34245 0
12356 1
.... ..
Sample Table B:
Cust_id col1 col2 col3
12356 ..
12567 ..
24426 ..
...
Table A has some 1m records, while table B has some 30m records. There is possibility of some duplicate IDs in table A and Table B.
I'm a bit confused. Hive is returning the columns that you specify in the query:
select distinct a.customer_id, a.target
from mb.spt_201603 a left outer join
mb.temp b
on a.customer_id = b.cust_id;
If you want columns from the second table, you need to select them:
select distinct a.customer_id, a.target, b.col1, b.col2
from mb.spt_201603 a left outer join
mb.temp b
on a.customer_id = b.cust_id;
I am willing to bet that this is a really simple answer as I am a noob to SQL.
Given:
table1 has column 1 (criteria 1) column 2 (criteria 2) column 3
(metric 1)
table2 has column 1 (criteria 1) column 2 (criteria 2) column 3
(metric 2 specific to table2.criteria2)
There can be anywhere from 1 - 5 values of criteria 2 for each criteria 1 on the table.
When I use the join statement here (assuming I identify table1 as One prior to this):
SELECT WeddingTable, TableSeat, TableSeatID, Name, Two.Meal
FROM table1 as One
INNER JOIN table2 as Two
ON One.WeddingTable = Two.WeddingTable AND One.TableSeat = Two.TableSeat
I only get one of the criteria 1/criteria 2 combinations even when I know for a fact that there are 3 or 4. How do I get all combinations?
Take the situation where there is a wedding where table1 is basically a seating chart, and table2 is the meal option that each table/seat has chosen. Table1 has the convenient TableSeatID, but table2 does not have a comparable ID.
Sample Data:
The results needs to show all 4 lines, being all 3 seats at WeddingTable 001 and the one seat at WeddingTable 002.
Desired Results:
SELECT one.*, two.Meal
FROM table1 AS one
LEFT JOIN table2 AS two
ON (one.WeddingTable = two.WeddingTable AND one.TableSeat = two.TableSeat);
SELECT aa.*,
bb.meal
FROM table1 aa
INNER JOIN table2 bb
ON aa.tableseat = bb.tableseat AND
aa.weddingtable = bb.weddingtable
INNER JOIN
(
SELECT a.tableSeat
FROM table1 a
INNER JOIN table2 b
ON a.tableseat = b.tableseat AND
a.weddingtable = b.weddingtable
WHERE b.meal IN ('chicken', 'steak')
GROUP by a.tableSeat
HAVING COUNT(DISTINCT b.Meal) = 2
) c ON aa.tableseat = c.tableSeat
SEE SQLFiddle Demo
create table a1
(weddingTable INT(3),
tableSeat INT(3),
tableSeatID INT(6),
Name varchar(10));
insert into a1
(weddingTable, tableSeat, tableSeatID, Name)
values (001,001,001001,'Bob'),
(001,002,001002,'Joe'),
(001,003,001003,'Dan'),
(002,001,002001,'Mark');
create table a2
(weddingTable int(3),
tableSeat int(3),
Meal varchar(10));
insert into a2
(weddingTable, tableSeat, Meal)
values
(001,001,'Chicken'),
(001,002,'Steak'),
(001,003,'Salmon'),
(002,001,'Steak');
select x.*, y.Meal
from a1 as x
JOIN a2 as y ON (x.weddingTable = y.weddingTable) AND (x.tableSeat = y. tableSeat);
It sounds like you want to list all the metrics?
SELECT Criteria1, Criteria2, Metric1 As Metric
FROM Table1
UNION ALL
SELECT Criteria1, Criteria2, Metric2 As Metric
FROM Table2
ORDER BY 1, 2
If you only want one Criteria1+Criteria2 combination, group them:
SELECT Criteria1, Criteia2, SUM(Metric) AS Metric
FROM (
SELECT Criteria1, Criteria2, Metric1 As Metric
FROM Table1
UNION ALL
SELECT Criteria1, Criteria2, Metric2 As Metric
FROM Table2
)
ORDER BY Criteria1, Criteria2
First, I would suggest being more explicit with referencing column names. Since WeddingTable and TableSeat appear as column names in both tables and depending on your environment, there may be ambiguity.
Select One.WeddingTable, One.TableSeat, TableSeatID, Name, Two.Meal
FROM table1 as One
inner join table2 as Two
on One.WeddingTable = Two.WeddingTable and One.TableSeat = Two.TableSeat
Once I resolve this ambiguity in my environment, the INNER JOIN returns 4 records for the data listed in your tables. There are 4 records that match between the two tables.
From the sample provided, I fail to see why you are only obtaining one combination. The only other possibility I can think of is if the values in the WeddingTable and TableSeat columns of your actual data tables, in fact, do not match.
For example, assuming TableSeat is CHAR type and table1.TableSeat contains ('001', '002', '003', '001') and table2.TableSeat contains ('01', '002', '3', '01'), then this would be a situation where it would limit to one match due to the TableSeat component of the ON predicate.
Other considerations are for when there is data in one table that has no match in the other.
Anom's response uses a LEFT OUTER JOIN. This returns all records from table1 regardless of whether there is a match in table2. Where there is no match, the Meal column will contain a NULL value. Since all records match, the result is the same as the INNER JOIN.
However, adding a record to table1 for which there is no match in table2
INSERT INTO
table1 (WeddingTable, TableSeat, TableSeatID, Name)
VALUES
(003, 002, 003002, 'Arielle');
the LEFT OUTER JOIN query will now produce a different result than the INNER JOIN.