【发布时间】:2015-01-15 22:53:08
【问题描述】:
目前,我不能使用典型的数据库,所以暂时使用 excel。有什么想法吗?
【问题讨论】:
-
一秒钟,这篇文章的格式就被屠宰了。
标签: sql database excel join cross-join
目前,我不能使用典型的数据库,所以暂时使用 excel。有什么想法吗?
【问题讨论】:
标签: sql database excel join cross-join
这是一种使用 Excel 公式的方法:
| | A | B | C |
| -- | -------------- | -------------- | -------------- |
| 1 | | | |
| -- | -------------- | -------------- | -------------- |
| 2 | Table1_Column1 | Table2_Column1 | Table2_Column2 |
| -- | -------------- | -------------- | -------------- |
| 3 | A | 1 | X |
| -- | -------------- | -------------- | -------------- |
| 4 | B | 2 | Y |
| -- | -------------- | -------------- | -------------- |
| 5 | C | 3 | Z |
| -- | -------------- | -------------- | -------------- |
| 6 | | | |
| -- | -------------- | -------------- | -------------- |
| 7 | Col1 | Col2 | Col3 |
| -- | -------------- | -------------- | -------------- |
| 8 | = Formula1 | = Formula2 | = Formula3 |
| -- | -------------- | -------------- | -------------- |
| 9 | = Formula1 | = Formula2 | = Formula3 |
| -- | -------------- | -------------- | -------------- |
| 10 | = Formula1 | = Formula2 | = Formula3 |
| -- | -------------- | -------------- | -------------- |
| 11 | ... | ... | ... |
| -- | -------------- | -------------- | -------------- |
Formula1: IF(ROW() >= 8 + (3*3*3), "", INDIRECT(ADDRESS(3 + MOD(FLOOR(ROW() - 8)/(3*3), 3), 1)))
Formula2: IF(ROW() >= 8 + (3*3*3), "", INDIRECT(ADDRESS(3 + MOD(FLOOR(ROW() - 8)/(3) , 3), 2)))
Formula3: IF(ROW() >= 8 + (3*3*3), "", INDIRECT(ADDRESS(3 + MOD(FLOOR(ROW() - 8)/(1) , 3), 3)))
【讨论】:
这是一种使用数据透视表生成任意数量列表的笛卡尔积的非常简单的方法:
https://chandoo.org/wp/generate-all-combinations-from-two-lists-excel/
该示例适用于两个列表,但它适用于任意数量的表和/或列。
在创建数据透视表之前,您需要convert your value lists to tables。
【讨论】:
使用 VBA,您可以。这是一个小例子:
Sub SqlSelectExample()
'list elements in col C not present in col B
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
con.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & ThisWorkbook.FullName & ";" & _
"DefaultDir=" & ThisWorkbook.FullName & ";ReadOnly=False;"
Set rs = New ADODB.Recordset
rs.Open "select ccc.test3 from [Sheet1$] ccc left join [Sheet1$] bbb on ccc.test3 = bbb.test2 where bbb.test2 is null ", _
con, adOpenStatic, adLockOptimistic
Range("g10").CopyFromRecordset rs '-> returns values without match
rs.MoveLast
Debug.Print rs.RecordCount 'get the # records
rs.Close
Set rs = Nothing
Set con = Nothing
End Sub
【讨论】:
这篇文章帮助我在 Excel 中执行交叉联接:
http://www.excelguru.ca/blog/2016/05/11/cartesian-product-joins-for-the-excel-person/
它需要 Microsoft 插件 适用于 Excel 的 Microsoft Power Query https://www.microsoft.com/en-us/download/details.aspx?id=39379
【讨论】:
尝试使用CROSS JOIN。阅读更多MSDN
您可以使用表达式CROSSJOIN(table1, table2) 创建笛卡尔积。
【讨论】: