【发布时间】: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