在访问或宏中安排数据

时间:2013-06-27 00:12:47

标签: sql excel vba ms-access ms-access-2007

我在excel表中有一些数据,我正在尝试将这些数据提供给Access。但数据真的很糟糕。这就是excel所拥有的。

国家,产品,ID,1qtr 2010闪存,2qtr 2010闪存,3qtr 2010闪存,4qtr 2010闪存, 1qtr 2011预算等....

现在您可以看到这是一种非常糟糕的存储数据的方式。我想在这个表上运行一些查询,但是当前模式很难,因为每年都会添加新列。

我想要一些方法(可能是一些SQL查询),这将创建一个新表,这些数据具有更好的结构,如下所示

国家/地区,产品,ID,价值,年份,Qtr,类型

希望你明白我的观点。任何人都可以在Access或Excel中向我推荐一些方法

1 个答案:

答案 0 :(得分:0)

您寻求的解决方案的一般形式是使用UNION查询,如下所示:

SELECT [country], [product], [id],
    [1qtr 2010 flash] AS [Value],
    2010 AS [Year],
    1 AS [Qtr],
    "flash" AS [Type]
FROM ImportedTable
UNION ALL
SELECT [country], [product], [id],
    [2qtr 2010 flash] AS [Value],
    2010 AS [Year],
    2 AS [Qtr],
    "flash" AS [Type]
FROM ImportedTable
UNION ALL
SELECT [country], [product], [id],
    [3qtr 2010 flash] AS [Value],
    2010 AS [Year],
    3 AS [Qtr],
    "flash" AS [Type]
FROM ImportedTable
...

您可以使用[ReformatDataFromExcel]之类的名称保存此类查询,然后在导入具有新列的更新Excel Feed时,可以根据需要复制并粘贴新的UNION ALL子句来更新查询。

- 编辑 -

或者,如果你想通过代码建立SQL查询字符串,那么像这样的一些VBA ......

Sub BuildQueries()
Dim cdb As DAO.Database, fld As DAO.Field, sSQL As String
Dim sYear As String, sQtr As String, sType As String, a() As String
Set cdb = CurrentDb
For Each fld In cdb.TableDefs("ImportedTable").Fields
    Select Case fld.Name
        Case "country", "product", "id"
            ' do nothing
        Case Else
            a = Split(fld.Name, " ", -1, vbBinaryCompare)
            sQtr = Left(a(0), 1)
            sYear = a(1)
            sType = a(2)
            sSQL = _
                    "SELECT [country], [product], [id], " & _
                    "[" & fld.Name & "] AS [Value], " & _
                    sYear & " AS [Year], " & _
                    sQtr & " AS [Qtr], " & _
                    """" & sType & """ AS [Type] " & _
                    "FROM ImportedTable"
            Debug.Print sSQL  ' or whatever you want to do with the query
    End Select
Next
Set fld = Nothing
Set cdb = Nothing
End Sub

...可以像这样构建SQL字符串:

SELECT [country], [product], [id], [1qtr 2010 flash] AS [Value], 2010 AS [Year], 1 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [2qtr 2010 flash] AS [Value], 2010 AS [Year], 2 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [3qtr 2010 flash] AS [Value], 2010 AS [Year], 3 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [4qtr 2010 flash] AS [Value], 2010 AS [Year], 4 AS [Qtr], "flash" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [1qtr 2011 budget] AS [Value], 2011 AS [Year], 1 AS [Qtr], "budget" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [2qtr 2011 budget] AS [Value], 2011 AS [Year], 2 AS [Qtr], "budget" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [3qtr 2011 budget] AS [Value], 2011 AS [Year], 3 AS [Qtr], "budget" AS [Type] FROM ImportedTable
SELECT [country], [product], [id], [4qtr 2011 budget] AS [Value], 2011 AS [Year], 4 AS [Qtr], "budget" AS [Type] FROM ImportedTable