【问题标题】:Help with SQL Server query帮助 SQL Server 查询
【发布时间】:2009-05-05 16:46:39
【问题描述】:

对不起*这是我应该放的

我的查询正在为具有超过 1 个实例的任何记录创建重复条目(无论日期如何)

<asp:SqlDataSource ID="EastMonthlyHealthDS" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SNA_TRTTestConnectionString %>" 
    SelectCommand="SELECT [SNA_Parent_Accounts].[Company], 
                   (SELECT [Monthly_HIP_Reports].[AccountHealth] from [Monthly_HIP_Reports] where ([Monthly_HIP_Reports].[YearMonth] = @ToDtRFC) AND ([SNA_Parent_Accounts].[CompID] = [Monthly_HIP_Reports].[CompID])) as [AccountHealth],
                   [SNA_Parent_Accounts].[CompID]
                   FROM [SNA_Parent_Accounts]
                   LEFT OUTER JOIN [Monthly_HIP_Reports] ON [Monthly_HIP_Reports].[CompID] = [SNA_Parent_Accounts].[CompID]
                   WHERE (([SNA_Parent_Accounts].[Classification] = 'Business') OR ([SNA_Parent_Accounts].[Classification] = 'Business Ihn')) AND ([SNA_Parent_Accounts].[Status] = 'active') AND ([SNA_Parent_Accounts].[Region] = 'east')
                   ORDER BY [SNA_Parent_Accounts].[Company]">
    <SelectParameters>
         <asp:ControlParameter ControlID="ddMonths" Name="ToDtRFC" PropertyName="Text" Type="String"  />
    </SelectParameters>
</asp:SqlDataSource>

使用 SELECT DISTINCT 似乎可以解决问题,但我认为这不是解决方案。数据库中没有重复的条目。所以看起来我的查询表面上是重复的。

查询应该获取满足 where 子句中条件的公司列表,还获取每个公司在特定 [YearMonth] 中的健康状态(如果存在),这就是子查询的用途。如果该 YearMonth 的条目不存在,则将 Health status 留空。

但如前所述.. 如果您有一个条目表示 CompID 2 的 2009-03 和 CompID 2 的 2009-04 条目.. 不管您选择哪个月份,它都会列出该公司 2-3次。

【问题讨论】:

    标签: asp.net sql-server join


    【解决方案1】:

    这个子查询:

    (SELECT [Monthly_HIP_Reports].[AccountHealth] from [Monthly_HIP_Reports] where ([Monthly_HIP_Reports].[YearMonth] = @ToDtRFC)) as [AccountHealth],
    

    与任何SNA_Parent_Accounts 列均不相关。

    是故意的吗?

    【讨论】:

      【解决方案2】:

      您是什么意思,它“为任何具有超过 1 个实例的记录创建重复条目”?似乎如果有多个实例,则已经存在重复条目?

      另外,我想看看这部分代码:

      SELECT count(*) FROM [SNA_Ticket_Detail] 
      WHERE ([SNA_Ticket_Detail].[CompID] = [SNA_Parent_Accounts].[CompID]) 
         AND (CAST(CAST(YEAR([SNA_Ticket_Detail].[DtRFC]) AS VARCHAR(4)) 
            + '-0' + CAST(MONTH([SNA_Ticket_Detail].[DtRFC]) AS VARCHAR(2))
            AS VARCHAR(7)) = @ToDtRFC) 
         AND ([SNA_Ticket_Detail].[Reviewed] = '1') 
      ) as [TicketCount] 
      

      我看到一个 VARCHAR(4) + '-0' + VARCHAR(2),在我看来最多有 8 个字符,填充到 VARCHAR(7) 中。您是否正在丢失一个角色并像那样复制?

      【讨论】:

        【解决方案3】:

        我认为您应该在 [Monthly_HIP_Reports] 表和 [SNA_Parent_Accounts] 表之间添加连接。我认为这就是它被重复的地方,因为我不知道这些表的用途......如果你提供更多的表描述,我肯定可以告诉你问题出在哪里......

        【讨论】:

          【解决方案4】:

          看来我在 LEFT OUTER JOIN 中发现了问题

          我有 左外连接 [Monthly_HIP_Reports] ON [Monthly_HIP_Reports].[CompID] = [SNA_Parent_Accounts].[CompID]

          它会抓取 ID 匹配的所有月度报告。我需要的是

          LEFT OUTER JOIN [Monthly_HIP_Reports] ON [Monthly_HIP_Reports].[CompID] = [SNA_Parent_Accounts].[CompID] AND ([Monthly_HIP_Reports].[YearMonth] = @ToDtRFC)

          因此它也将其限制为 YearMonth。

          谢谢大家!

          【讨论】: