根据第1列中的拆分值,在“选择查询”中引入新字段

时间:2016-06-29 16:36:39

标签: sql vba field crosstab recordset

我有一个看起来像这样的选择查询:

SELECT 
LEFT([Total INNOVATIONS / MMD],INSTR(1,[Total INNOVATIONS / MMD],'-')-2) AS BRAND, MID([Total INNOVATIONS / MMD],INSTR(1,[Total INNOVATIONS / MMD],'-')+2 , INSTRREV([Total INNOVATIONS / MMD],'-')-2) AS INNOTYPE, RIGHT([Total INNOVATIONS / MMD], INSTRREV([Total INNOVATIONS / MMD],'-')+2) AS RECDATE, [Fact], 
[Week Ending 05/01/14], [Week Ending 12/01/14], [Week Ending 19/01/14], [Week Ending 26/01/14], [Week Ending 02/02/14], [Week Ending 09/02/14], [Week Ending 16/02/14], [Week Ending 23/02/14], [Week Ending 02/03/14], [Week Ending 09/03/14], [Week Ending 16/03/14], [Week Ending 23/03/14], [Week Ending 30/03/14], [Week Ending 06/04/14], [Week Ending 13/04/14], [Week Ending 20/04/14], [Week Ending 27/04/14], [Week Ending 04/05/14], [Week Ending 11/05/14], [Week Ending 18/05/14], [Week Ending 25/05/14], [Week Ending 01/06/14], [Week Ending 08/06/14], [Week Ending 15/06/14], [Week Ending 22/06/14], [Week Ending 29/06/14], [Week Ending 06/07/14], [Week Ending 13/07/14], [Week Ending 20/07/14], [Week Ending 27/07/14], [Week Ending 03/08/14], [Week Ending 10/08/14], [Week Ending 17/08/14], [Week Ending 24/08/14], [Week Ending 31/08/14], [Week Ending 07/09/14], [Week Ending 14/09/14], [Week Ending 21/09/14], [Week Ending 28/09/14], [Week Ending 05/10/14], [Week Ending 12/10/14], [Week Ending 19/10/14], [Week Ending 26/10/14], [Week Ending 02/11/14], [Week Ending 09/11/14], [Week Ending 16/11/14], [Week Ending 23/11/14], [Week Ending 30/11/14], [Week Ending 07/12/14], [Week Ending 14/12/14], [Week Ending 21/12/14], [Week Ending 28/12/14] FROM [Sheet1$] WHERE Fact="ACV Dist" 

我已经介绍了3个新的领域,

  

“品牌”,   “INNOTYPE”   “重新开始”

进入sql查询以形成记录集。通过在“ - ”分隔符上拆分第一个字段来拾取这些新列的值。 例如

    strConString = _
"Provider=Microsoft.ACE.OLEDB.12.0; " _
& "Data Source='" & varPathAndFile & "'; " _
& "Extended Properties=""Excel 12.0 XML;HDR=YES;IMEX=1"";"


strSQL = "SELECT TOP 1 * FROM [Sheet1$]"
Set objCON = CreateObject("ADODB.Connection")
objCON.Open strConString

Set objRS = CreateObject("ADODB.Recordset")
With objRS
    .locktype = ADOLockType.adLockOptimistic
    .CursorType = ADOCursorType.adOpenStatic
    .cursorlocation = ADOCursorLocation.adUseClient
    .Source = strSQL
    .activeconnection = objCON
    .Open
    .movelast
    .movefirst

  Stop
    strSQL = "SELECT "
    For Each fld In .Fields
        If InStr(1, fld.Name, "INNOVATIONS") > 0 Then

            strFieldValue = fld.Name

            --strSQL = strSQL & "[" & fld.Name & "], "
            strSQL = strSQL & " LEFT([" & strFieldValue & "],INSTR(1,[" & strFieldValue & "],'-')-2) AS BRAND,"
            strSQL = strSQL & " MID([" & strFieldValue & "],INSTR(1,[" & strFieldValue & "],'-')+2 , INSTRREV([" & strFieldValue & "],'-')-2) AS INNOTYPE,"
            strSQL = strSQL & " RIGHT([" & strFieldValue & "], INSTRREV([" & strFieldValue & "],'-')+2) AS RECDATE,"
        End If

        If fld.Name = "Fact" Then
            strSQL = strSQL & " [" & fld.Name & "], "
        End If

        If InStr(1, fld.Name, "Week Ending") > 0 And Right(fld.Name, 2) = Right(lngYear, 2) Then
            strSQL = strSQL & "[" & fld.Name & "], "
        End If
    Next fld

Stop

    If Right(Trim(strSQL), 1) = "," Then strSQL = Left(Trim(strSQL), Len(Trim(strSQL)) - 1)
    strSQL = strSQL & " FROM [Sheet1$] WHERE Fact=""ACV Dist"" "

    Debug.Print strSQL
    .Close
    .Source = strSQL
Stop
    .Open

Stop
  

如何从第1个字段中将值填充到这3个字段列中   列(拆分其值)?

1 个答案:

答案 0 :(得分:1)

正在构建动态字符串时会发生很多事情。

首先,您从SELECT开始,然后尝试测试以查看字符串的第一部分是否有","逗号,因为您在循环期间自动插入逗号。通过在测试第一个逗号之后不添加select来修复。

二。你重复3列两次。 BRAND, INNOTYPE, RECDATE如果创新和事实都在字段集中。

接下来,在识别新列时,您只需使用名称BRAND, INNOTYPE, RECDATE,但SQL认为这是一个列名,并且会给您一个错误。修改为更改为NULL AS BRAND, NULL AS INNOTYPE,等。但是你可能还想告诉它一个数据类型,所以CAST(NULL AS VARCHAR(100)) AS BRAND, CAST(NULL AS VARCHAR(100)) AS INNOTYPE,等...如果你真的只想要一个空字符串值做{{1}之类的事情}

在调试和测试时打印出您的SQL语句,错误将变得更加明显。

搜索如何从字符串中获取子字符串。并对数据进行一些测试。但它可能是一个组合 CAST('' AS VARCHAR(100)) AS BRAND将是第一个字段 LEFT(Innovations,CHARINDEX('-',Innovations) - 1)SUBSTRING(Innovations,CHARINDEX('-',Innovations),CHARINDEX('-',REVERSE(Innovations)))代表第3名。

这假设你总是有2' - '连字符,没有空格。如果有空格将结果RIGHT(Innovations,CHARINDEX('-',REVERSE(Innovations)) - 1)包裹在substring中以摆脱它们。

子串的MS Access版本

LTRIM(RTRIM())

如果你有空格,那就把它放在Left(Innovations,InStr(1,Innovations,'-') - 1) MID(Innovations,InStr(1,Innovations,'-'),InStrRev(Innovations,'-')) RIGHT(Innovations,InStrRev(Innovations,'-') - 1)

里面