简体   繁体   English

SQL查询未返回空值记录

[英]SQL query not returning Null-value records

I am using SQL Server 2014 on a Windows 10 PC. 我在Windows 10 PC上使用SQL Server 2014。 I am sending SQL queries directly into Swiftpage's Act! 我将SQL查询直接发送到Swiftpage的行为! CRM system (via Topline Dash). CRM系统(通过Topline Dash)。 I am trying to figure out how to get the query to give me records even when some of the records have certain Null values in the Opportunity_Name field. 我试图弄清楚如何获得查询以给我记录,即使某些记录在Opportunity_Name字段中具有某些Null值也是如此。

I am using a series of Join statements in the query to connect 4 tables: History, Contacts, Opportunity, and Groups. 我在查询中使用了一系列Join语句来连接4个表:历史记录,联系人,机会和组。 History is positioned at the “center” of it all. 历史位于一切的“中心”。 They all have many-to-many relationships with each other, and are thus each linked by an intermediate table that sits “between” the main tables, like so: 它们彼此之间具有多对多的关系,因此每个都由位于主表之间的中间表链接,如下所示:

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

The intermediate tables consist only of the PKs in each of the main tables. 中间表仅包含每个主表中的PK。 Eg History_Group is only a listing of HistoryIDs and GroupIDs. 例如,History_Group仅是HistoryID和GroupID的列表。 Thus, any given History entry can have multiple Groups, and each Group has many Histories associated with it. 因此,任何给定的“历史记录”条目都可以具有多个组,并且每个“组”都有许多与之关联的历史记录。

Here's what the whole SQL statement looks like: 整个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;

The problem is that when the Opportunity table is linked in, any record that has no Opportunity (ie a Null value) won't show up. 问题是,当链接商机表时,没有商机(即Null值)的任何记录都不会显示。 If you remove the Opportunity references in the Join statement, the listing will show all history events in the Date range just fine, the way I want it, whether or not they have an Opportunity associated with them. 如果您删除Join语句中的“商机”引用,该清单将按我想要的方式很好地显示“日期”范围内的所有历史事件,无论它们是否与“商机”相关联。

I tried adding the following to the WHERE part of the statement, and it did not work. 我尝试将以下内容添加到该语句的WHERE部分中,但它不起作用。

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

I also tried changing the Opportunity_Name reference up in the SELECT part of the statement to read: ISNULL(Opportunity_Name, 'x') – this didn't work either. 我还尝试将语句的SELECT部分​​中的Opportunity_Name引用更改为以下内容:ISNULL(Opportunity_Name,'x')–这也不起作用。

Can anyone suggest a way to get the listing to contain all records regardless of whether they have a Null value in the Opportunity Name or not? 任何人都可以建议一种使列表包含所有记录的方法,而不管它们在商机名称中是否具有Null值? Many thanks!!! 非常感谢!!!

I believe this is because a default JOIN statement discards unmatched rows from both tables. 我相信这是因为默认的JOIN语句会丢弃两个表中不匹配的行。 You can fix this by using LEFT JOIN. 您可以使用LEFT JOIN修复此问题。

Example: 例:

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

You can see that the first query returns only 1 record, while the second returns both. 您可以看到第一个查询仅返回一个记录,而第二个查询均返回两个记录。

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;

You will want to make sure you are using a LEFT JOIN with the table Opportunity. 您将要确保您将“左联接”与“机会”表一起使用。 This will keep records that do not relate to records in the Opportunity table. 这将保留与“商机”表中的记录无关的记录。

Also, BE CAREFUL you do not filter records using the WHERE clause for the Opportunity table being LEFT JOINED. 另外,请注意,对于LEFT JOINED的商机表,不要使用WHERE子句来过滤记录。 Include those filter conditions relating to Opportunity instead in the LEFT JOIN ... ON clause. 在LEFT JOIN ... ON子句中包括与机会相关的那些过滤条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM