Handling non existent values in sql query expression for ssrs chart

后端 未结 6 1847
野性不改
野性不改 2020-12-20 10:49

I am using the following query in an ssrs line chart. It counts how many orders are recorded each month based on each order date.

My problem is that when a month has

6条回答
  •  心在旅途
    2020-12-20 11:24

    that behavior is well defined by SQL construct "Inner join". use left join (or right join, depending on which is correct side) to retrieve null values when the join condition is not satisfied as below (not tested)

    SELECT
    MONTH(Ord.OrdDate) AS 'MONTH',
    COUNT(CASE WHEN @Worker_ID1 IS NULL OR @Worker_ID1 = Worker.ID THEN 1 END) AS 'Worker1',
    COUNT(CASE WHEN @Worker_ID2 IS NULL OR @Worker_ID2 = Worker.ID THEN 1 END) AS 'Worker2',
    COUNT(CASE WHEN @Worker_ID3 IS NULL OR @Worker_ID3 = Worker.ID THEN 1 END) AS 'Worker3',
    COUNT(CASE WHEN @Worker_ID4 IS NULL OR @Worker_ID4 = Worker.ID THEN 1 END) AS 'Worker4',
    COUNT(CASE WHEN @Worker_ID5 IS NULL OR @Worker_ID5 = Worker.ID THEN 1 END) AS 'Worker5'
    
    FROM Prod 
    LEFT JOIN ORD ON Ord.Prod_ID = Prod.ID
    JOIN ProdType ON Prod.ProdType_ID = ProdType.ID
    JOIN Grouping ON Ord.Grouping_ID = Groupord.ID
    JOIN Worker ON Grouping.Worker_ID = Worker.ID
    
    WHERE ((Ord.OrdDate is not null and @Year = YEAR(Ord.OrdDate)) or ORD.prod_id is null)
    AND (@ProdType_ID IS NULL OR @ProdType_ID = ProdType.ID)
    
    GROUP BY MONTH(Ord.OrdDate)
    

    Note - i added additional where clause conditions to check on the year function on orddate as this can be null

    google for joins SQL and am sure you will find much more quality information than this

    Hope this helps

提交回复
热议问题