VB6 ADO:基于第一列的值返回结果集

时间:2014-08-13 20:59:45

标签: sql vb6 ms-access-2010

任务:

我有一个包含8列的表格,其中大约有100,000行。

Year    Cycle   PHSRCode  Blanket  LastName  FirstName  Status  ExcusedStatus
2012    5       GW1-01     null    CASTILLO  LILIBETH   Yes     FALSE
2012    5       GW1-01     null    CLAVERIA  PAMELA     Yes     FALSE
2012    5       GW1-01     1       RAMOS     LAILANI    Yes     FALSE
2012    5       GW1-01     2       SIY       ZERZENDEE  Yes     FALSE
2012    5       GW1-01     null    SANTOS    MARILIN    Yes     FALSE
2012    5       GW1-01     null    BALDERAS  JULIET     No      FALSE

对于查询结果,我需要返回所有8列和100,000行,另外还有4列,其值可以从同一个表的8列和100,000行的值派生。

基本上,输出应该是这样的:

    Year    Cycle   PHSRCode  Blanket  LastName  FirstName  Status  ExcusedStatus  RawActualRate   RawTargetRate   RawActualReach
    2012    5       GW1-01     null    CASTILLO  LILIBETH   Yes     FALSE               1              1             1

列:

  • raw_actual_rate - 可以通过以下方式派生:If Status = 'Yes', 1, 0
  • raw_target_rate - 可以通过以下方式派生:If Status <> '', 1, 0

  • raw_actual_reach - 这是复杂的地方。可以通过以下方式派生:If Status = 'No', 0, 1/ [count if Status='Yes' AND (Year + Cycle + PHSRCode + Blanket + LastName + FirstName) = Row Result of the same columns

  • raw_target_reach - 这是再次合并的地方。可以通过以下方式派生:1/ [count if Status='Yes' AND (Year + Cycle + PHSRCode + Blanket + LastName + FirstName) = Row Result of the same columns

问题:

这可以在一个查询语句中完成吗?我希望不通过代码使用任何循环语句,因为以这种方式获得结果需要一些时间(10-20分钟)。

到目前为止我有什么:

这是我正在使用的SQL语句。但是,由于tb1尚未返回任何值,我仍然坚持最后一部分:

Private Function sql_string1v2() As String
    Dim sql As String

    sql = "SELECT "
    sql = sql & "tb1.ID, "
    sql = sql & "tb1.PeriodYear, "
    sql = sql & "tb1.PeriodCycle, "
    sql = sql & "tb1.PeriodZone, "
    sql = sql & "tb1.PHSRCode, "
    sql = sql & "tb1.Blanket, "
    sql = sql & "tb1.LastName, "
    sql = sql & "tb1.FirstName, "
    sql = sql & "tb1.MiddleName, "
    sql = sql & "tb1.PRC, "
    sql = sql & "tb1.Specialty, "
    sql = sql & "tb1.HCPType, "
    sql = sql & "tb1.Class, "
    sql = sql & "tb1.Room, "
    sql = sql & "tb1.Institution, "
    sql = sql & "tb1.Address, "
    sql = sql & "tb1.Region, "
    sql = sql & "tb1.Province, "
    sql = sql & "tb1.City, "
    sql = sql & "tb1.Brick, "
    sql = sql & "tb1.Type, "
    sql = sql & "tb1.Affiliation, "
    sql = sql & "tb1.Frequency, "
    sql = sql & "tb1.Status, "
    sql = sql & "tb1.MissCallReason,"
    sql = sql & "tb1.FlexiCallZone, "
    sql = sql & "tb1.Process, "
    sql = sql & "tb1.DateAdded, "
    sql = sql & "tb1.Encoder, "
    sql = sql & "tb1.ExcusedStatus, "

    sql = sql & "IIF(tb1.Status = 'Yes', 1, 0) AS raw_actual_rate, "
    sql = sql & "IIF(tb1.Status <> '', 1, 0) AS raw_target_rate, "

    sql = sql & "(SELECT (1/COUNT(ID)) AS raw_actual_reach FROM tblDCM as tb2 WHERE tb2.Status = 'Yes' AND tb2.PeriodYear = tbl1.PeriodYear AND PeriodCycle = tb1.PeriodCycle AND PHSRCode = tb1.PHSRCode AND ((tb1.Blanket IS NOT NULL) AND Blanket = tb1.Blanket) OR (LastName = tb1.LastName AND FirstName = tb1.FirstName)) AS raw_actual_reach, "

    sql = sql & "FROM tblDCM as tb1 "
    sql = sql & "WHERE LEFT(tb1.PHSRCode,2) = 'N0'"

    sql_string1v2 = sql
End Function

1 个答案:

答案 0 :(得分:1)

这应该是评论,但我还没有足够的声誉发表评论。

那就是说,我可以提供一些建议。

首先,编辑您的问题以包含目标数据库(访问,MSSQL,Oracle等),因为SQL命令的子集可能会因您要查询的平台而有很大差异。

其次,这纯粹是一个SQL问题,你应该忘记vb6方面,直到你的查询按预期工作。

我可以肯定地说,raw_actual_reach将成为您当前编写的方式的问题。对于一个并非所有字段都包含其别名前缀,并且在比较具有相同名称的字段时,您必须将别名包含为前缀,否则您将遇到错误。

此外,您在字段列表的末尾还有一个额外的逗号(当您复制/粘贴时,或者您错过了一行代码。)我已经从您的代码中删除了查询取样并在下面更正。根据您要查询的数据库平台,此查询仍可能存在问题,但现在至少语法是一致的。 HTH。

SELECT  tb1.ID, tb1.PeriodYear, tb1.PeriodCycle, tb1.PeriodZone, tb1.PHSRCode, tb1.Blanket, tb1.LastName, tb1.FirstName,
        tb1.MiddleName, tb1.PRC, tb1.Specialty, tb1.HCPType, tb1.Class, tb1.Room, tb1.Institution, tb1.Address, tb1.Region,
        tb1.Province, tb1.City, tb1.Brick, tb1.Type, tb1.Affiliation, tb1.Frequency, tb1.Status, tb1.MissCallReason, tb1.FlexiCallZone,
        tb1.Process, tb1.DateAdded, tb1.Encoder, tb1.ExcusedStatus, IIF(tb1.Status = 'Yes', 1, 0) AS raw_actual_rate, IIF(tb1.Status <> '', 1, 0) AS raw_target_rate,
        (
            SELECT  (1/COUNT(ID)) AS raw_actual_reach
            FROM    tblDCM as tb2
            WHERE   tb2.Status = 'Yes' AND tb2.PeriodYear = tbl1.PeriodYear AND tb2.PeriodCycle = tb1.PeriodCycle AND tb2.PHSRCode = tb1.PHSRCode AND 
                    ((tb1.Blanket IS NOT NULL) AND tb2.Blanket = tb1.Blanket) OR
                    (tb2.LastName = tb1.LastName AND tb2.FirstName = tb1.FirstName)
        ) AS raw_actual_reach
FROM    tblDCM as tb1
WHERE   LEFT(tb1.PHSRCode,2) = 'N0'