2

我正在使用 CFSpreadsheet 读取 .xlsx 文件。

该文件有大约 3000 个重复项,我可以放心地忽略它们,所以我想我会做一个select distinctQoQ,但是一旦我这样做,结果就会被排序,就像order by col_1, col_2添加到查询中一样,这是一件非常糟糕的事情。

<cfspreadsheet query = "qSheet" ...>
<cfquery dbtype="query" name = "qDistinctSheet">
    select distinct
          col_1
        , col_2
    from
        qSheet
</cfquery> 
<cfdump var = "#qDistinctSheet#">

如果我删除distinct,我会得到预期的结果,应该是:

  1. [空字符串]
  2. 姓名
  3. 约翰
  4. 约翰
  5. 亚当
  6. 史蒂夫
  7. 鲍勃
  8. 鲍勃

当我添加不同的我得到

  1. [空字符串]
  2. 亚当
  3. 鲍勃
  4. 约翰
  5. 姓名
  6. 史蒂夫

知道如何防止这种不需要的排序吗?

编辑

最终解决方案是按照 Matt 和 Dan 的建议应用行号并使用 group by

<cfset ids = []>
<cfloop query="qSheet">
    <cfset ids[qSheet.currentRow] = qSheet.currentRow>
</cfloop>
<cfset queryAddColumn(qSheet,"id",ids)>
<cfquery dbtype="query" name="qDistinct">
    SELECT  
          col_1
        , col_2
        , min(ID) AS firstID
    FROM
        qSheet
    GROUP BY    
        col_1
        , col_2
    ORDER BY
        firstID
</cfquery>
4

1 回答 1

4

您可以改用GROUP BY选项并使用电子表格查询中的 ID 行

<cfquery dbtype="query" name="qDistinct">
SELECT  
      col_1
    , col_2
    , min(ID) AS firstID
FROM
    qSheet
GROUP BY    
    col_1
    , col_2
ORDER BY
    firstID

于 2015-03-04T19:15:28.333 回答