从 3 个 CSV 文件中排序和选择
Posted
技术标签:
【中文标题】从 3 个 CSV 文件中排序和选择【英文标题】:Sorting and selecting from 3 CSV files 【发布时间】:2019-08-14 23:27:25 【问题描述】:每个月我都会收到几个 CSV 文件的数据转储。一些文件超过了 Microsoft Excel 的最大行数,但我能够使用 ADODB 记录集和连接来选择和排序文件,并且只加载我需要的值。我可以让它为两个文件工作,但在一种情况下,我需要它去三个文件,这就出现了问题。
以下代码适用于两个文件:
Dim cnD As New ADODB.Connection
cnD.Provider = "Microsoft.ACE.OLEDB.12.0"
cnD.ConnectionString = "Data Source=" + fullFolder + ";Extended Properties=""text;HDR=Yes;" + _
"FMT=Delimited(,)"""
cnD.Open
Dim rsD As New ADODB.Recordset
rsD.ActiveConnection = cnD
rsD.Source = "SELECT Data.id AS HapDataId, Data.createtimestamp, " + _
"Data.lastmodified, Goals.* " + _
"FROM HAPData.csv AS Data " + _
"INNER JOIN HAPGoals.csv AS Goals ON Data.Id = Goals.hapid " + _
"WHERE Format(Data.createtimestamp, " + """" + "yyyy-MM-dd" + """" + ") >= " + _
"Format(#" + startDateStr + "#, " + """" + "yyyy-MM-dd" + """" + ") OR " + _
"Format(Data.lastmodified, " + """" + "yyyy-MM-dd" + """" + ") >= " + _
"Format(#" + startDateStr + "#, " + """" + "yyyy-MM-dd" + """" + ") " + _
"ORDER BY Data.id, Data.lastmodified, Data.createtimestamp, Goals.id "
rsD.Open
并生成以下 SQL:
SELECT Data.id AS HapDataId, Data.createtimestamp, Data.lastmodified, Goals.*
FROM HAPData.csv AS Data INNER JOIN HAPGoals.csv AS Goals ON Data.id = Goals.hapid
WHERE Format(Data.createtimestamp, "yyyy-MM-dd") >= Format(#7/1/2019#, "yyyy-MM-dd") OR
Format(Data.lastmodified, "yyyy-MM-dd") >= Format(#7/1/2019#, "yyyy-MM-dd")
ORDER BY Data.id, Data.lastmodified, Data.createtimestamp, Goals.id
如果我修改它以添加第三个文件,我会得到以下 SQL:
SELECT Data.id AS HapDataId, Data.createtimestamp, Data.lastmodified, Goals.*, Actions.*
FROM HAPData.csv AS Data INNER JOIN HAPGoals.csv AS Goals ON Data.id = Goals.hapid
INNER JOIN HAPActionSteps.csv AS Actions ON Goals.id = Actions.goalid
WHERE Format(Data.createtimestamp, "yyyy-MM-dd") >= Format(#7/1/2019#, "yyyy-MM-dd") OR
Format(Data.lastmodified, "yyyy-MM-dd") >= Format(#7/1/2019#, "yyyy-MM-dd")
ORDER BY Data.id, Data.lastmodified, Data.createtimestamp, Goals.id, Actions.id
在 rsD.Open 行上产生以下错误:
查询表达式“Data.id =Goals.hapid INNER JOIN HAPActionSteps.csv AS Actions ON Goals.id = Actions.goali”中的语法错误(缺少运算符),错误代码为 -2147217900
谁能看到我的 SQL 出了什么问题?
【问题讨论】:
【参考方案1】:我相信如果这是一个类似 Access 的语法,你需要这样的括号:
FROM (HAPData.csv AS Data INNER JOIN HAPGoals.csv ON Data.id = Goals.hapid)
INNER JOIN HapActionSteps.csv AS Actions ON Goals.id = Actions.goalid
【讨论】:
是的,就是这样。非常感谢!以上是关于从 3 个 CSV 文件中排序和选择的主要内容,如果未能解决你的问题,请参考以下文章