简体   繁体   English

具有NULL值的SQL列结果

[英]SQL Column results with NULL Values

I am having the following issue with my query. 我的查询存在以下问题。

I am trying to import data from multiple tables (Fact_Contact, Quali_Seg, etc…) into one table (Fact_Forecast). 我试图将数据从多个表(Fact_Contact,Quali_Seg等)导入一个表(Fact_Forecast)。 This is to predict how many individuals are eligible for a specific offer. 这是为了预测有多少个人有资格获得特定优惠。 The problem I am having is that for some reason, the column Date_ID, which is been pulled from Fact_Contact, when importing has NULL values. 我遇到的问题是由于某种原因,导入时从Fact_Contact提取的Date_ID列具有NULL值。 I don't know where these NULL values are coming from as the table Fact_Contact don't have any NULL values in the column DATE_ID. 我不知道这些NULL值来自哪里,因为表Fact_Contact在DATE_ID列中没有任何NULL值。

This is the section of the query that has the problem, 这是查询中有问题的部分,

DECLARE @lastDateID int
 SELECT TOP 1 @lastDateID = date_id
   FROM Fact_Contact
  ORDER BY CREATE_DATE DESC

 SELECT date_id, Offers.Segmentation_id, Offers.Offer_Code, Offers.Wave_no,
        Offers.cadencevalue,
        CASE
            WHEN dailydata.activity_count IS NOT NULL THEN dailydata.activity_count
            ELSE 0
        END as "activity_count"
   FROM (
         SELECT s.Segmentation_id, s.Offer_Code, s.Wave_no, o.cadencevalue,
                o.campaign_id, o.offer_desc
           FROM Forecast_Model.dbo.Quali_Segment s
                 LEFT JOIN Forecast_Model.dbo.Dim_Offers o 
                        ON s.offer_code = o.offer_code
        ) Offers
           LEFT JOIN (
                      SELECT date_id, Offer_Code_1 Offer_Code,
                             segmentation_group_id, Count(indv_role_id) Activity_count
                        FROM Forecast_Model.dbo.Fact_Contact
                       WHERE date_id = @lastDateID
                       GROUP BY offer_code_1,segmentation_group_id,date_id
                     ) DailyData  
                  ON DailyData.offer_code = Offers.offer_code
                      AND Offers.Segmentation_id = dailydata.segmentation_group_id 
  ORDER BY Segmentation_id,Wave_no

The column Date_ID as I mentiones gets only 2 dates which is the same as the @LastDateID which is 2014-05-20 but the rest are NULL. 我提到的Date_ID列仅获得2个日期,与2014年5月20日的@LastDateID相同,但其余均为NULL。

Thank you, Omar 谢谢奥马尔

只要您在“报价”中有记录(加入Quali_Segment)但在Fact_Contact中没有匹配的记录,则date_id将为NULL

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

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