简体   繁体   中英

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). 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. 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.

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.

Thank you, Omar

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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