Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于对另一列的位置引用选择列的SQL_Sql_Excel_Select - Fatal编程技术网

基于对另一列的位置引用选择列的SQL

基于对另一列的位置引用选择列的SQL,sql,excel,select,Sql,Excel,Select,我有一个excel文档,其中一些列没有命名。excel的组织方式是,每个类别都有一个DateTime和一个值,但只有DateTime列有一个标题(这是类别的名称,而不是“日期”) 比如说 |Category 1| | |Category 2| | |May 1 |13246| |May 4 |84563 | |May 3 |65842| |May 7 |84535 | |May 8 |86453| |May 8 |876541|

我有一个excel文档,其中一些列没有命名。excel的组织方式是,每个类别都有一个
DateTime
和一个值,但只有
DateTime
列有一个标题(这是类别的名称,而不是“日期”)

比如说

|Category 1|     | |Category 2|      |
|May 1     |13246| |May 4     |84563 |
|May 3     |65842| |May 7     |84535 |
|May 8     |86453| |May 8     |876541|
等等,以获取文档的其余部分。有没有办法将这两列与SQL关联起来?可能是

 Select Category1, [Category1 + 1]
 from Table

谢谢你

我已经探讨过这个问题了。我认为最简单的解决方案是简单地添加标题

但这是VBA内部的一种解决方法。当我们创建连接时,我们设置HDR=no。这将自动为我们生成标题F1到Fn。不幸的是,我们现在将有一行读取category 1,null,category 2,null。我们无法运行删除查询,因为它们不受支持。我试图将整行更新为null,但遇到了问题

Sub main()
Dim cn                      As ADODB.Connection
Dim rs                      As ADODB.Recordset
Dim strFile                 As String
Dim strCon                  As String
Dim strSql                  As String

    cn.Open _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source='" & ThisWorkbook.FullName & "';" & _
        "Extended Properties=""Excel 12.0 Macro;HDR=NO;IMEX=1;"";"

    Set cn = New ADODB.Connection

    cn.Open strCon

    strSql = "SELECT * From [DataTable]"

    Set rs = New ADODB.Recordset

    rs.Open strSql, cn

    Debug.Print rs.GetString

    rs.Close

    strSql = "SELECT CDATE([F1]),CDATE([F3]),[F4] FROM [DATATABLE] WHERE [F2] = 13246 ORDER BY [F1];"

    rs.Open strSql, cn

    Debug.Print rs.GetString

    Set cn = Nothing
    Set rs = Nothing
End Sub
我的输出。第一个debug.print将获取整个记录集。然后我运行一个测试查询,确信它看起来没有问题


谢谢你的帮助!我会尽力让它像这样工作。
category 1      category 2  
43221   13246   43224   84563
43223   65842   43227   84535
43228   86453   43228   876541

5/1/2018    5/4/2018    84563