【问题标题】:Is there a way to perform a cross join or Cartesian product in excel?有没有办法在 excel 中执行交叉连接或笛卡尔积?
【发布时间】:2015-01-15 22:53:08
【问题描述】:

目前,我不能使用典型的数据库,所以暂时使用 excel。有什么想法吗?

【问题讨论】:

标签: sql database excel join cross-join


【解决方案1】:

这是一种使用 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)))

【讨论】:

    【解决方案2】:

    这是一种使用数据透视表生成任意数量列表的笛卡尔积的非常简单的方法:

    https://chandoo.org/wp/generate-all-combinations-from-two-lists-excel/

    该示例适用于两个列表,但它适用于任意数量的表和/或列。

    在创建数据透视表之前,您需要convert your value lists to tables

    【讨论】:

      【解决方案3】:

      这里有 3 个维度:dim1 (ABC)、dim2 (123)、dim3 (XYZ)。

      以下是使用标准 Excel 而非 VBA 制作二维笛卡尔积的方法:

      1) 垂直绘制 dim1,水平绘制 dim2。在交叉点上连接维度成员:

      2) 反透视数据。使用 ALT-D-P 启动数据透视表向导(不要按住 ALT,按一次)。选择“多个合并范围” --> 创建一个页面.. --> 选择所有单元格(包括标题!)并将其添加到列表中,然后按下一步。

      3) 垂直绘制生成的并分解连接的字符串

      瞧,你有交叉连接。如果您需要添加另一个维度,请再次重复此算法。

      干杯,

      康斯坦丁。

      【讨论】:

        【解决方案4】:

        使用 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
        

        【讨论】:

          【解决方案5】:

          这篇文章帮助我在 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

          【讨论】:

          • Power Query 成为标准 Excel 2016 的一部分,不再是加载项。
          【解决方案6】:

          尝试使用CROSS JOIN。阅读更多MSDN

          您可以使用表达式CROSSJOIN(table1, table2) 创建笛卡尔积。

          【讨论】:

          • 问题是关于excel的。
          猜你喜欢
          • 2015-10-16
          • 2023-03-23
          • 2014-12-06
          • 2021-10-12
          • 2018-11-30
          • 2017-08-05
          • 1970-01-01
          • 1970-01-01
          • 2016-06-26
          相关资源
          最近更新 更多