SQL查询不返回Null值记录

时间:2018-02-19 02:41:31

标签: sql sql-server join isnull

我在Windows 10 PC上使用SQL Server 2014。我正在将SQL查询直接发送到Swiftpage的Act中! CRM系统(通过Topline Dash)。我试图找出如何让查询给我记录,即使某些记录在Opportunity_Name字段中有某些Null值。

我在查询中使用一系列Join语句来连接4个表:History,Contacts,Opportunity和Groups。历史位于这一切的“中心”。它们彼此之间具有多对多的关系,因此每个都由位于主表之间的中间表链接,如下所示:

History – Group_History – Group
History – Contact_History – Contact
History – Opportunity_History – Opportunity

中间表仅包含每个主表中的PK。例如。 History_Group只是HistoryID和GroupID的列表。因此,任何给定的历史记录条目都可以有多个组,每个组都有许多与之关联的历史记录。

这是整个SQL语句的样子:

SELECT Group_Name, Opportunity_Name, Start_Date_Time, Contact.Contact, Contact.Company, History_Type, (SQRT(SQUARE(Duration))/60) AS Hours, Regarding, HistoryID
FROM HISTORY
   JOIN Group_History
       ON Group_History.HistoryID = History.HistoryID
   JOIN "Group"
       ON Group_History.GroupID = "Group".GroupID
   JOIN Contact_History
       ON Contact_History.HistoryID = History.HistoryID
   JOIN Contact
       ON Contact_History.ContactID = Contact.ContactID
   JOIN Opportunity_History
       ON Opportunity_History.HistoryID = History.HistoryID
   JOIN Opportunity
       ON Opportunity_History.OpportunityID = Opportunity.OpportunityID
WHERE
       (  Start_Date_Time >= ('2018/02/02')       AND
          Start_Date_Time <= ('2018/02/16')                )
ORDER BY Group_NAME, START_DATE_TIME;

问题在于,当链接商机表时,任何没有商机的记录(即空值)都不会显示。如果您删除Join语句中的商机参考,则商家信息将按照我想要的方式显示日期范围内的所有历史事件,无论他们是否有与之关联的商机。

我尝试将以下内容添加到语句的WHERE部分,但它不起作用。

  AND  (  ISNULL(Opportunity_Name, 'x') = 'x'     OR
          ISNULL(Opportunity_Name, 'x') <> 'x'              )

我还尝试将语句的SELECT部分​​中的Opportunity_Name引用更改为:ISNULL(Opportunity_Name,'x') - 这也不起作用。

任何人都可以建议一种方法来使列表包含所有记录,无论它们是否在商机名称中都有空值?非常感谢!!!

2 个答案:

答案 0 :(得分:1)

我认为这是因为默认的JOIN语句会丢弃两个表中不匹配的行。您可以使用LEFT JOIN解决此问题。

示例:

CREATE TABLE dataframe (
A int,
B int
);

insert into dataframe (A,B) values 
(1, null),
(null, 1)

select a.A from dataframe a
join dataframe b ON a.A = b.A

select a.A from dataframe a
left join dataframe b ON a.A = b.A

您可以看到第一个查询只返回1个记录,而第二个返回两个记录。

SELECT Group_Name, Opportunity_Name, Start_Date_Time, Contact.Contact, Contact.Company, History_Type, (SQRT(SQUARE(Duration))/60) AS Hours, Regarding, HistoryID
FROM HISTORY
   LEFT JOIN Group_History
       ON Group_History.HistoryID = History.HistoryID
   LEFT JOIN "Group"
       ON Group_History.GroupID = "Group".GroupID
   LEFT JOIN Contact_History
       ON Contact_History.HistoryID = History.HistoryID
   LEFT JOIN Contact
       ON Contact_History.ContactID = Contact.ContactID
   LEFT JOIN Opportunity_History
       ON Opportunity_History.HistoryID = History.HistoryID
   LEFT JOIN Opportunity
       ON Opportunity_History.OpportunityID = Opportunity.OpportunityID
WHERE
       (  Start_Date_Time >= ('2018/02/02')       AND
          Start_Date_Time <= ('2018/02/16')                )
ORDER BY Group_NAME, START_DATE_TIME;

答案 1 :(得分:1)

您需要确保使用LEFT JOIN与表机会。这将保留与商机表中的记录无关的记录。

此外,请注意,不要使用WHERE子句为LEFT JOINED的Opportunity表过滤记录。请在LEFT JOIN ... ON子句中包含与Opportunity相关的过滤条件。

相关问题