Cross join in excel --- Copy from Internet
Set up the Workbook
In this example, there are two tables -- Raw Materials and Packaging -- and each table is on a separate worksheet.
The Raw Materials table is on the sheet named Materials, and the Packaging table is on the sheet named Packaging.
The third sheet in the workbook is named Combined, and this is where the query results will be stored.
With Microsoft Query, you can create a list that combines each item in one table, with all the items in the other table -- a Cartesian join, also called a cross join. You can read more about the different join types on the Microsoft website: Description of the usage of joins in Microsoft Query
Open MS Query
To create the Cartesian (cross) join, you'll use MS Query.
- On the Excel Ribbon, click the Data tab
- In the Get External Data group, click From Other Sources, then click From Microsoft Query
- In the Choose Data Source window, click on Excel Files*, and click OK
- In the Select Workbook window, locate and select the current workbook, and click OK.
- In the Query Wizard, if you don't see the sheet names listed, click Options, and add a check mark to System Tables
- Click Materials$, and click the arrow to put the Raw Materials column in the query.
- Click Packaging$, and click the arrow to put the Packaging column in the query
- Click Cancel, to close the Query Wizard, and click Yes when prompted.
- In Microsoft Query, double-click on Raw Materials in the Materials$ table, to add it to the query grid.
- Then, double-click on Packaging, to add it to the query grid.
- Click the Return Data button, to send the data to Excel.
Create a Worksheet Table
It might take a few seconds, but then the Import Data window will open.
- In the Import Data window, select Table
- Select the cell on the worksheet where you want to place the query results, and click OK.
- A table is created, and shows all the items from each table, in all possible combinations.
Add Formulas to the Table
You can add formulas to the table, in a new column. The formulas will automatically adjust if the source tables are changed.
- Type a new heading in cell C1 -- MatPack -- and press Enter
- A new column is automatically included in the table.
- In cell C2, type a formula to combine the text in columns A and B, with a space character between them:
=[@[Raw Materials]] & " " & [@Packaging]
- Then, copy the formula down to the last row of data in the table.
Update the Table
You can update the source tables, and then update the query results table, to show the revised data.
- Add a new item to each of the source tables. In this example, Boxes was added to the Packaging list, and Cream was added to the Raw Materials list.
- On the Excel Ribbon, click the Data tab, and click Refresh All.
- The new items are shown in the updated query results table.
Manually Update the Workbook Name
If you change the workbook name, the query will need to be updated, before it will run. To manually update the query:
- Right-click a cell in the results table, and click Refresh
- When the Login Failed message appears, click OK
- In the Select Workbook window, locate and select the new workbook, and click OK.
Update the Query Connection with VBA
If the file name or location will change frequently, you can use programming to automatically change the file location in the connection.
Paste the following code -- FixQueryConnection -- into a regular module in the workbook, and then run the code when the workbook opens, by adding a Workbook_Open event.
This code was tested in Excel 2010 (32-bit), and might need to be adjusted for other versions of Excel.
Sub FixQueryConnection()
'
Dim strFile As String
Dim strPath As String
Dim strQry As String
Dim strCmd As String
Dim strConn As String strPath = ActiveWorkbook.Path & "\"
strFile = ActiveWorkbook.Name
strQry = "Query from Excel Files"
strCmd = "SELECT `Materials$`.`Raw Materials`, `Packaging$`.Packaging "
strCmd = strCmd & "FROM `Materials$` `Materials$`, `Packaging$` `Packaging$`"
strConn = "ODBC;DSN=Excel Files;DBQ=" & strPath & strFile
strConn = strConn & ";DefaultDir=" & strPath
strConn = strConn & ";DriverId=790;MaxBufferSize=2048;PageTimeout=5;"
'
With ActiveWorkbook.Connections(strQry).ODBCConnection
.BackgroundQuery = True
.CommandText = strCmd
.CommandType = xlCmdSql
.Connection = strConn
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections(strQry)
.Name = strQry
.Description = ""
End With
ActiveWorkbook.Connections(strQry).Refresh ActiveWorkbook.RefreshAll
End Sub
'====================================
Put this code into the ThisWorkbook module:
Private Sub Workbook_Open()
FixQueryConnection
End Sub
'====================================

Get Excel News
Copyright © Contextures Inc. 2016
Cross join in excel --- Copy from Internet的更多相关文章
- SQL中inner join、outer join和cross join的区别
对于SQL中inner join.outer join和cross join的区别简介:现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同 ...
- CROSS JOIN连接用于生成两张表的笛卡尔集
将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘 CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...
- left join ,right join ,inner join ,cross join 区别
left join ,right join ,inner join ,cross join 区别(参考:http://zhidao.baidu.com/link?url=gpOl9HXZR0OrQuy ...
- MySQL的几种连接 join/inner join/cross join/逗号/left join/right join/natural join
转载请注明出处!! 之前数据表连接操作多使用逗号或者join,对几种连接的概念一直浑浑噩噩,最近研究了一波,把这些连接的区别搞明白了. 连接:A xjoin B(主表 操作 关联表) selec ...
- 用实例展示left Join,right join,inner join,join,cross join,union 的区别
1.向TI,T2插入数据: T1 7条 ID Field2 Field3 Field41 1 3 542 1 3 543 1 3 544 2 3 545 3 3 546 4 3 547 5 3 54 ...
- Linq表连接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)
我们知道在SQL中一共有五种JOIN操作:INNER JOIN.LEFT OUTER JOIN.RIGHT OUTER JOIN.FULL OUTER JOIN.CROSS JOIN 1>先创建 ...
- FULL JOIN 与 CROSS JOIN
FULL JOIN 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行.(返回JOIN 两端表的所有数据,无论其与另一张表有没有匹配.显示左连接.右连接和内连接的并集) FULL JOIN ...
- SQL表连接查询(inner join(join)、full join、left join、right join、cross join)
下面列出了您可以使用的 JOIN 类型,以及它们之间的差异. JOIN: 如果表中有至少一个匹配,则返回行(join=inner join) LEFT JOIN: 即使右表中没有匹配,也从左表返回所有 ...
- CROSS JOIN,NATURAL JOIN
CROSS JOIN:笛卡尔积 NATURAL JOIN:
随机推荐
- SystemErrorCodes
有人把SystemErrorCodes整理成了类,并定义了方法,用于返回消息,他大概不知道FormatMessage的用法,放在这里做参考吧 C# code snipppet class System ...
- 二、CoreAnimation之寄宿图详解
在之前的图层树中我们知道,可以使用CALayer对象创建一些有背景颜色的图层,其实使用CALayer,不仅可以利用其展示背景颜色,还可以展示图片.而这些展示内容,其实就是CALayer的寄宿图.这一节 ...
- 修改iphone联系人头像
如何通过代码来修改iphone联系人头像 使用如下代码: UIImage *image=[UIImage imageNamed:@"star_full.png"]; NSData ...
- Sublime3学习笔记
学习笔记: 学习内容:sublime 3 学习时间:2015-10-20 预计学习时长:1 hour/3 day 学习工具&资料: 官网:http://www.sublimetext.com/ ...
- windows消息钩子注册底层机制浅析
标 题: [原创]消息钩子注册浅析 作 者: RootSuLe 时 间: 2011-06-18,23:10:34 链 接: http://bbs.pediy.com/showthread.php?t= ...
- 数据库错误:check the manual that corresponds to your MySQL server version for the right sy
检查对应到您的MySQL服务器版本附近使用正确的语法手册 数据库插入的时候出现上述问题,总结了两方面原因: 1.语法错误,这是百度之得到的大部分结果,但是没有解决我的问题 2.仔细观察我的sql语句, ...
- SourceTree安装教程和GitLab配置详解
一.安装Git 链接: http://pan.baidu.com/s/1mh7rICK 密码: 48dj 二.安装SourceTree 链接: http://pan.baidu.com/s/1skWk ...
- Go语言 数组
介绍 Array 是值类型,Slice 和 Map 是引用类型.他们是有很大区别的,尤其是在参数传递的时候. 另外,Slice 和 Map 的变量 仅仅声明是不行的,必须还要分配空间(也就是初始化,i ...
- Node.js-Socket.IO【1】-身份验证
Websocket身份验证失败的时候,希望向前台传输错误信息,但是Socket.IO目前最新版本1.4.6在后台使用 next(new Error('unauthorization')); 前端的代码 ...
- Hibernate和IBatis对比
[转自]http://blog.csdn.net/ya2dan/article/details/7396598 项目也做过几个, 使用IBatis就做一个项目, 基本上都是使用Hibernate, 也 ...