How do I join my two tables? - sql

I have two Tables and I am trying to do a join or union to bring over one column from the second table.
SELECT Services,Metric_Type,services_be,Services_Be_L2,Services_Be
FROM table1
JOIN table2
ON table1.SERVICES_BE = table2.SERVICES_BE
Metric_Type is all I need from table2
Potential joins
Table1
Services_Be,
SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR,
Table2
Services_Be,
Sales_L1 = SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR

I guess it's a column ambiguously defined error.
In the select statement, you list the field SERVICES_BE which belongs to both tables, and the sql parser don't know which table you meant.
try using table alias.
SELECT t1.Services,t2.Metric_Type,t1.services_be,t1.Services_Be_L2,t1.Services_Be
FROM table1 t1
Inner JOIN table2 t2 ON t1.SERVICES_BE = t2.SERVICES_BE

SELECT Services,Metric_Type,services_be,Services_Be_L2,Services_Be
FROM table1
JOIN table2
ON table1.SERVICES_BE = table2.SERVICES_BE
Looks like a valid join and that would allow you to use columns from both tables side by side. The one thing that you may want to use are table aliases to make it easier to reference the table and then specifically identify the table your column is from in the Select list something like:
SELECT t1.Services,t1.Metric_Type,t1.services_be,t2.Services_Be_L2,t2.Services_Be
FROM table1 t1
JOIN table2 t2
ON t1.SERVICES_BE = t2.SERVICES_BE
If you want to APPEND the records from one table to another you will want to use UNION or UNION ALL
SELECT
Services_Be,
SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR
FROM
Table11
UNION ALL
SELECT
Services_Be,
SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR
FROM
Table12
When appending, the column definitions and data types need to be the same between the top table and bottom. UNION ALL will append every record from Table2, while UNION will only append records that are not already present in Table1 (Sort of like DISTINCT).

Related

Coalesce gives repeat columns that prevents doing a union

I have two tables (Table 1 and Table 2) that I join to then do a coalesce. So...
SELECT t1.*,
coalesce(t1.A, t2.A_T) as A
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.key = t2.key
Doing what I said above, I will get a repeated A column given I want to replace missing values in A from Table 1 with Table 2's A column. I want to do a union but a repeated A column is an issue (as seen in Table 3). You cannot do a union with different numbers of columns.
How can I fix this issue?
What I want:
Note: the actual tables I am working with have over 40 columns each. But this example is to get to the point of my issue.
Snowflake supports exclude so you could do
select coalesce(t1.A, t2.A_T) as A, t1.* exclude(t1.A)
from ...
Note that your problem stems from the fact that you are using * to select columns instead of being explicit about which columns to select.

Comparing base table value with second table's sum of value with group by

I have two tables:
One is base table and second is transaction table. I want to compare base table value with second table's sum of value with group by.
Table1(T1Id,Amount1,...)
Tabe2(T2Id,T1ID,Amount2)
I want those rows from table 1 WHere SUM of Table2's SUM( Amount2) is greater or equal table1's Amount1.
*T1ID is in relation with both tables
* The SQL query have many joins with other table for data retriving.
One approach uses a join:
SELECT t1.T1Id, t1.Amount1
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.T1Id = t2.T1ID
GROUP BY
t1.T1Id, t1.Amount1
HAVING
SUM(t2.Amount2) >= t1.Amount1;
We can also try doing this via a correlated subquery:
SELECT t1.T1Id, t1.Amount1
FROM Table1 t1
WHERE t1.Amount1 <= (SELECT SUM(t2.Amount2) FROM Table2 t2
WHERE t1.T1Id = t2.T1ID);
I would use something similar to the query below:
SELECT
a.T1Id, a.Amount1, SUM(b.Amount2)
FROM Table1 a
INNER JOIN Table2 b on b.T1Id = a.T1Id
GROUP BY a.T1Id, a.Amount1
HAVING SUM(b.Amount2) >= a.Amount1;
Basically what the query above does is give you the ID, Amount from table 1 and the summed amount from table 2. The HAVING clause at the end of query filters out those records where the summed amount from the second table is smaller than the amount from the first one.
If you want to add further table joins to the query, you can do so by adding as many joins as you wish. I would recommend having a referenced ID for each table you are joining in the Table1 table.

How to map each distinct value of a column in one table with each distinct value of a column in another table in Hive

I have two tables in Hive, Table1 and Table2. I want to get each distinct customerID in Table1 and map it to each distinct value in a column called category of Table2. However I am a bit lost on how to do this in hive. A better example of what I am trying to do is the following: Let's say Table1 contains 5 distinct customerID's and Table2 contains 3 distinct categories. I want my query result to look something like the following:
However Table1 and Table2 do not have any columns in common so I am a bit lost on how to perform a join on this two tables in hive. Is this task possible in hive? Any insights on this would be greatly appreciated!
You can do that with a cross join of distinct values from both tables.
select t1.customerid,t2.categories
from (select distinct customerid from tbl1) t1
cross join (select distinct categories from tbl2) t2

Not able to Get data from multiple independent tables that have a common column and yet do not depend on each other

I have 8 tables all with equal number of columns and with a common column. I want to fetch data from all tables in a single query.
My table structure is TABLE1, TABLE2, TABLE3, ..... TABLE 8.
that have columns COLUMNA, COLUMNB... COLUMNE and a COMMON_COLUMN
I need to get data with a where clause where COMMON_COLUMN='X'
I will need all columns from all tables.
I used a query that goes like this..
SELECT TABLE1.*, TABLE2.*, TABLE3.*
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.COMMON_COLUMN = T2.COMMON_COLUMN,
LEFT JOIN TABLE3 T3 ON T1.COMMON_COLUMN = T3.COMMON_COLUMN
WHERE T1.COMMON_COLUMN='X' AND T2.COMMON_COLUMN='X' AND T3.COMMON_COLUMN='X'
The above query is not giving any results even if one of the tables do not have any rows. I do not want to use inner join because although the tables have a common column they do not depend on each other and I need data from all tables with a certain common column.
Also, the tables have unequal number of rows.
What am I doing wrong?
correct me if i am wrong - as you do not attach any sample data and desired result
but i assume that you simply need union all tables. You write in the title that tables are independent
SELECT T1.*
FROM TABLE1 T1
WHERE T1.COMMON_COLUMN='X'
UNION ALL
SELECT T2.*
FROM TABLE2 T2
WHERE T2.COMMON_COLUMN='X'
UNION ALL
SELECT T3.*
FROM TABLE3 T3
WHERE T3.COMMON_COLUMN='X'
...

Comparing two datasets SQL SSRS 2005

I have two datasets on two seperate servers. They both pull one column of information each.
I would like to build a report showing the values of the rows that only appear in one of the datasets.
From what I have read, it seems I would like to do this on the SQL side, not the reporting side; I am not sure how to do that.
If someone could shed some light on how that is possible, I would really appreciate it.
You can use the NOT EXISTS clause to get the differences between the two tables.
SELECT
Column
FROM
DatabaseName.SchemaName.Table1
WHERE
NOT EXISTS
(
SELECT
Column
FROM
LinkedServerName.DatabaseName.SchemaName.Table2
WHERE
Table1.Column = Table2.Column --looks at equalities, and doesn't
--include them because of the
--NOT EXISTS clause
)
This will show the rows in Table1 that don't appear in Table2. You can reverse the table names to find the rows in Table2 that don't appear in Table1.
Edit: Made an edit to show what the case would be in the event of linked servers. Also, if you wanted to see all of the rows that are not shared in both tables at the same time, you can try something as in the below.
SELECT
Column, 'Table1' TableName
FROM
DatabaseName.SchemaName.Table1
WHERE
NOT EXISTS
(
SELECT
Column
FROM
LinkedServerName.DatabaseName.SchemaName.Table2
WHERE
Table1.Column = Table2.Column --looks at equalities, and doesn't
--include them because of the
--NOT EXISTS clause
)
UNION
SELECT
Column, 'Table2' TableName
FROM
LinkedServerName.DatabaseName.SchemaName.Table2
WHERE
NOT EXISTS
(
SELECT
Column
FROM
DatabaseName.SchemaName.Table1
WHERE
Table1.Column = Table2.Column
)
You can also use a left join:
select a.* from tableA a
left join tableB b
on a.PrimaryKey = b.ForeignKey
where b.ForeignKey is null
This query will return all records from tableA that do not have corresponding records in tableB.
If you want rows that appear in exactly one data set and you have a matching key on each table, then you can use a full outer join:
select *
from table1 t1 full outer join
table2 t2
on t1.key = t2.key
where t1.key is null and t2.key is not null or
t1.key is not null and t2.key is null
The where condition chooses the rows where exactly one match.
The problem with this query, though, is that you get lots of columns with nulls. One way to fix this is by going through the columns one by one in the SELECT clause.
select coalesce(t1.key, t2.key) as key, . . .
Another way to solve this problem is to use a union with a window function. This version brings together all the rows and counts the number of times that key appears:
select t.*
from (select t.*, count(*) over (partition by key) as keycnt
from ((select 'Table1' as which, t.*
from table1 t
) union all
(select 'Table2' as which, t.*
from table2 t
)
) t
) t
where keycnt = 1
This has the additional column specifying which table the value comes from. It also has an extra column, keycnt, with the value 1. If you have a composite key, you would just replace with the list of columns specifying a match between the two tables.