Equivalent to Joining tables (SQL) in Excel?

In summary: Basically, I am trying to find a sum of expressions f1, f2 in Sheet1, Sheet 2 respectively. Sheet1, Sheet2 share a fieldf12. Problem is values f12 in Sheet1 and Sheet2 overlap ( i.e., their intersection is not empty ), but are not equal, so I need to match f12-entries which are equal in Sheet1,...
  • #1
WWGD
Science Advisor
Gold Member
7,009
10,469
Hi All,
Sorry if this seems pretty lost.
I want to compute an expression of the type A/B with data given in different sheets, i.e., A is given in sheet i and b is given in sheet j. Now, I know that in
SQL Server ( SQL in general), one can join tables. Is there a similar way of
getting data from different sheets, i.e., something similar to joins, in Excel?
 
Computer science news on Phys.org
  • #5
Greg Bernhardt said:
beat you to it @jedishrfu ! :)
Thanks, Greg ( Jedishfru, in intent :) ). There are a lot of ways of making things work this way within Excel.
Still, it seems it would be easier to export the Excel files into SQL Server using the Import/Export Wizard
Just wondering: Do you think it helps to learn these commands in Excel when it could all be done relatively easily by exporting data to SQL Server? I mean, is there any advantage to doing it with Excel over using SQL Server?
 
  • #6
Generally best practice to store data in sql and use excel as a reporting / analysis tool
 
  • Like
Likes WWGD
  • #7
BWV said:
Generally best practice to store data in sql and use excel as a reporting / analysis tool
Problem is when the connection between Excel and Microsoft Query is not working well . I need to do a query on my Excel data ( as I stated, with data from different sheets ), and the connection has been hanging for a while now. I am trying all sorts of hacks to get it done.
 
  • #8
A pivot table won’t work?
 
  • #9
BWV said:
A pivot table won’t work?

Will it allow me to do a ratio of fields in different sheets ( EDIT In same document)?
 
  • #13
In case anyone is interested , this is how I did it -- the actual formula to figure out a ratio A/B , where A,B are fields in different sheets in same document.
1) Align ordering of common fields in two sheets:
2) Then , say A,B are in columns A,B respectively in Sheets A,B, with values starting at A1,B1 resp.

3) Select empty cell, say , F, then use formula: =A_table!A1/B_table!B1

Then extend to other cells.
 
  • #17
That is excel, but if you don’t want to calculate within the pivot table you can simply enter the formula in a column of cells referencing the two columns in the pivot table
 
  • #18
BWV said:
That is excel, but if you don’t want to calculate within the pivot table you can simply enter the formula in a column of cells referencing the two columns in the pivot table
Sorry, I misread your post above.
 
  • #19
One last question, please. With Pivot, is it possible to filter along an equality of rows? I mean, I have data from Sheet 1, Sheet 2, with a common field F ( in both sheets), say F1, F2 in sheets 1,2 respectively. Problem is that, while F1, F2 do overlap, not all values of F1 equal those of F2 , i.e., there is ##f_{1j} \in F1## which is not in F2 , and there are values ##f_{2k} \in F2 ## which are not in F1. How can I then use Pivot to calculate a new field that uses fields in Sheet1, Sheet2? Can I do an equivalent to an SQL join through filtering or otherwise?
EDIT: Basically, I am trying to find a sum of expressions f1, f2 in Sheet1, Sheet 2 respectively. Sheet1, Sheet2 share a fieldf12. Problem is values f12 in Sheet1 and Sheet2 overlap ( i.e., their intersection is not empty ), but are not equal, so I need to match f12-entries which are equal in Sheet1, Sheet2.
 
Last edited:
  • #20
Backing up, if the pivot table does not allow the correct join (I have not tried this so don’t know the answer) go back to the connection. I don’t understand why you have to do this in excel rather than in a query in sql server. Perhaps the problem is that you are using Microsoft Query which is an outdated crap add-in. If the raw data comes into populate the two worksheets, why can’t the output of a query with the proper join come in instead? Rather than using MS query, you can place a SQL server query directly in the connection and the results will populate a worksheet. You can write and test the query in sql server management studio and then copy and paste it into the connection. The connection has to be updated manually and the data from the last update will remain in the worksheet so if the connection is not working, it will retain the last successful query result.
 
  • Like
Likes WWGD
  • #21
I have no problem with using SQL Server per se, it is just that doing aggregates becomes complicated, and, worse, I am not getting the right result.
 
  • #22
Is it a one-to-one or one-to-many relationship on the two sheets?
 
  • #23
BWV said:
Is it a one-to-one or one-to-many relationship on the two sheets?
It is 1-1, but not all pairs are related -- and that is the big problem. In sheet 1, I have the common field f1 with, vakues, say, 1,2,3,4,7,9 , and the same field in sheet 2 has values, say 2,3,6,8,9 . So some field values are common, but some are not. That is what makes it complicated.
 
  • #24
If it’s one-to-one then you can use the vlookup function
 
  • #25
BWV said:
If it’s one-to-one then you can use the vlookup function
Yes, thanks, that is what I was thinking. Will check it out and report back here for any third parties that may be interested.
 
  • #26
But if your query is not aggregating properly and there are a number of tables, the likely culprit is a one-to-many join
 
  • #27
BWV said:
But if your query is not aggregating properly and there are a number of tables, the likely culprit is a one-to-many join
I think the problem may be related to the variable defined as a ratio of aggregates. Aggregates are messy in SQL Server, more so if we use joins as well. I am double-checking. This is the reason i tried Excel, because the ration I did : Sum(A)/Count (B) , all done over a common field in two databases, was not outputting the right results.
 
  • #28
You can nest queries in SQL server so if you can do two separate queries with the correct results they can be combined
 
  • #29
BWV said:
You can nest queries in SQL server so if you can do two separate queries with the correct results they can be combined
You're right, those do take some extra work. Will try it , tho. Thanks for all your comments.
 
  • #30
Ah, never mind. In case anyone is interested for the SQL format:
We want to compute the ratio SUM(f_1)/COUNT(f_2) , f1 in table 1 (T1), f2 in table 2 (T2);
f_12 a common field , over values in f_12 common to T1, T2.
This runs: EDIT

SELECT A/B FROM
( SELECT SUM(f_1) AS A
FROM T1 JOIN T2 on T1.f_12=T2.f_12 GROUP BY f_12) AS Table1
JOIN
(SELECT COUNT(f_2) AS B FROM T1 JOIN T2 ON T1.f_12=T2.f_12
GROUP BY f_12 ) AS Table2
ON Table1.f_12 =Table2.f_12
GROUP BY f_12

Sorry to all for dragging this along so far.
 
Last edited:
  • Like
Likes BWV
  • #31
WWGD said:
Ah, never mind. In case anyone is interested for the SQL format:
We want to compute the ratio SUM(f_1)/COUNT(f_2) , f1 in table 1 (T1), f2 in table 2 (T2);
f_12 a common field , over values in f_12 common to T1, T2.
This runs: EDIT

SELECT A/B FROM
( SELECT SUM(f_1) AS A
FROM T1 JOIN T2 on T1.f_12=T2.f_12 GROUP BY f_12) AS Table1
JOIN
(SELECT COUNT(f_2) AS B FROM T1 JOIN T2 ON T1.f_12=T2.f_12
GROUP BY f_12 ) AS Table2
ON Table1.f_12 =Table2.f_12
GROUP BY f_12

Sorry to all for dragging this along so far.

Have you found a solution yet?
I do sql in Excel now and then (ODBC) using some time sheets in the same file as tables, but also sometime sheets in differents files.
I know you can do simple join operations. See this link with some .xlsx examples and a ReadMe.txt file:
https://www.dropbox.com/sh/awoes8lgpj76igb/AABZOAEq7pF36kIXKykn3PKga?dl=0

In file T_Queries, I put A and B in different columns and did A/B the old school Excel way in a formula but you can play around with more complex join queries and see how Excel handles it.. (Ribbon > Data > Connections > DoubleClick on Connection > Definition).
Those two queries use tables/sheets being in the same file (T_1_and_2.xlsx).

Another query, just for fun, uses tables/sheets being in 2 different files (T_1.xlsx and T_2.xlsx).
It's cool that one can use a virtual database of excel files being all over the place and query it using our dear friends SQL and ODBC!

Let us know it this helps a little in what you were trying to achieve.
 
Last edited:
  • Like
Likes WWGD
  • #32
Tokinabo said:
Have you found a solution yet?
I do sql in Excel now and then (ODBC) using some time sheets in the same file as tables, but also sometime sheets in differents files.
I know you can do simple join operations. See this link with some .xlsx examples and a ReadMe.txt file:
https://www.dropbox.com/sh/awoes8lgpj76igb/AABZOAEq7pF36kIXKykn3PKga?dl=0

In file T_Queries, I put A and B in different columns and did A/B the old school Excel way in a formula but you can play around with more complex join queries and see how Excel handles it.. (Ribbon > Data > Connections > DoubleClick on Connection > Definition)

Those two queries use tables/sheets being in the same file (T_1_and_2.xlsx).
Another query, just for fun, uses tables/sheets being in 2 different files (T_1.xlsx and T_2.xlsx).

Let us know it this helps a little in what you were trying to achieve.
Thanks. I was able to do it in SQL , but I had trouble using Microsoft query within Excel; somehow the connection was not working well ( despite the fact that the Wizard had no trouble sending data back-and-forth). Thanks for your refs., will check them out.
 

1. What is the purpose of joining tables in SQL?

Joining tables in SQL allows you to combine data from two or more tables into a single result set. This is useful when you need to analyze data that is spread across multiple tables, or when you want to retrieve data from related tables in a database.

2. How do I join tables in SQL?

To join tables in SQL, you need to use the JOIN keyword in your query. There are different types of joins, such as INNER JOIN, LEFT JOIN, and RIGHT JOIN, which determine how the data from the two tables will be combined. You also need to specify the columns that you want to join on using the ON clause.

3. Can I join tables in Excel?

Yes, you can join tables in Excel using the VLOOKUP function or the INDEX and MATCH functions. However, these methods are not as efficient as using SQL to join tables, especially when dealing with large datasets.

4. What is the difference between a join and a union in SQL?

A join combines data from two or more tables into a single result set, while a union combines data from two or more queries into a single result set. Joins are used to retrieve data from related tables, while unions are used to combine data from unrelated tables or queries.

5. Are there any limitations to joining tables in SQL?

There are some limitations to joining tables in SQL, such as the number of tables that can be joined in a single query, the size of the result set, and the availability of matching data in the joined columns. It is important to carefully plan and optimize your joins to avoid these limitations and ensure efficient data retrieval.

Similar threads

Replies
11
Views
1K
  • Computing and Technology
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Computing and Technology
Replies
7
Views
870
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
51
Views
4K
  • Computing and Technology
Replies
1
Views
2K
  • Computing and Technology
Replies
9
Views
3K
  • Computing and Technology
Replies
1
Views
958
Replies
5
Views
2K
Back
Top