用 0 和 1 填充几列(创建虚拟变量)

发布于 2024-10-12 06:06:14 字数 580 浏览 6 评论 0原文

我有一个带有 exp 和 imp 列的 ms-access 2003 表。在这些 exp 和 imp 列中,我有 75 个国家/地区。我想在同一个表中创建虚拟变量 exp1-exp75、imp1-imp75,显示哪个国家/地区是 exp,哪个国家/地区是 imp。因此,例如,如果 exp 是澳大利亚(澳大利亚是第 1 个国家),则 exp1 必须为 1,所有其他 exp2-exp75 应为 0。如果 imp 是英国(英国是第 5 个国家),则 imp5 应为 1,所有其他imp 应该为 0。所以表格应该如下所示(如果美国是第三个国家,意大利是第 17 个国家)

exp           imp    exp1 exp2 ...exp17 ... exp75 imp1 imp2 imp3 ... imp5 ... imp75


Australia     UK      1    0        0         0     0    0    0        1        0


Italy         USA     0    0        1         0     0    0    1        0        0

谢谢。

I have a ms-access 2003 table with exp and imp columns. In these exp and imp columns I have 75 countries. I want to create dummy variables exp1-exp75, imp1-imp75, in the same table, showing which country is exp and which country is imp. So for example if exp is Australia (Australia is the 1st country) then exp1 must be 1 and all other exp2-exp75 should be 0. And if imp is UK (UK is the 5th country), imp5 should be 1 and all the other imp's should be 0. So the table should look like this (if USA is the 3rd and Italy is the 17th country)

exp           imp    exp1 exp2 ...exp17 ... exp75 imp1 imp2 imp3 ... imp5 ... imp75


Australia     UK      1    0        0         0     0    0    0        1        0


Italy         USA     0    0        1         0     0    0    1        0        0

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-10-19 06:06:14

我是在 Access 2007 编辑器中编写的,但 VBA 应该是相同的。我认为你不能通过查询来做到这一点。

Private Sub FillTable()

Const firstCountry  As Integer = 1
Const lastCountry   As Integer = 75
Const maxRows       As Integer = 234 'or whatever you need...


Dim impCountry  As Integer
Dim expCountry  As Integer

Dim db  As DAO.Database
Dim rs  As DAO.Recordset
Dim i   As Integer

Dim j   As Integer

    'function Random1to75 is left as an exercise for the reader
    impCountry = Random1to75
    expCountry = Random1to75

    Do Until expCountry <> impCountry
        expCountry = Random1to75
    Loop

    Set db = CurrentDb()
    Set rs = db.OpenRecordset("select * from YourTable", dbOpenDynaset)

    For j = 1 To maxRows
        rs.AddNew
            For i = firstCountry To lastCountry
                If i <> impCountry Then
                    rs("imp" & i) = 0
                Else
                    rs("imp" & i) = 1
                End If

                If i <> expCountry Then
                    rs("exp" & i) = 0
                Else
                    rs("exp" & i) = 1
                End If
            Next
        rs.Update
    Next

    rs.Close
    Set rs = Nothing
    Set db = Nothing

End Sub

I wrote this in the Access 2007 editor, but the VBA should be the same. I don't think you can do it with queries.

Private Sub FillTable()

Const firstCountry  As Integer = 1
Const lastCountry   As Integer = 75
Const maxRows       As Integer = 234 'or whatever you need...


Dim impCountry  As Integer
Dim expCountry  As Integer

Dim db  As DAO.Database
Dim rs  As DAO.Recordset
Dim i   As Integer

Dim j   As Integer

    'function Random1to75 is left as an exercise for the reader
    impCountry = Random1to75
    expCountry = Random1to75

    Do Until expCountry <> impCountry
        expCountry = Random1to75
    Loop

    Set db = CurrentDb()
    Set rs = db.OpenRecordset("select * from YourTable", dbOpenDynaset)

    For j = 1 To maxRows
        rs.AddNew
            For i = firstCountry To lastCountry
                If i <> impCountry Then
                    rs("imp" & i) = 0
                Else
                    rs("imp" & i) = 1
                End If

                If i <> expCountry Then
                    rs("exp" & i) = 0
                Else
                    rs("exp" & i) = 1
                End If
            Next
        rs.Update
    Next

    rs.Close
    Set rs = Nothing
    Set db = Nothing

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文