简体   繁体   中英

Show Null Values in Count Statement - SQL Server

I am developing a sales report and trying to select the count of Activities Done by the Sales Person during the week and want to show all Activities regardless he have done it or No in a table, i am using a left or full join but not showing. below is the explanation of Tables

Table 1: Activity Types - ACT_TYPE_ID ,ACT_TYPE_Desc 
Table 2: Activity Details - ACT_TYPE_ID, ACT_TYPE_Text 

I want to show it as below:

Type Count

Sales Call -             3 
Phone Call               0 
Meeting Outside          9 

Below is my select statement but not returning the required and showing only activities that is created:

Select ACT_TYPE_Desc as [Activity Type] , Count(CR031_DIARY_TEXT) as Count 
From ACT_TYPES AT  Join ACT_Details AD 
On AT.DRY_TRC_TYPE = Ad.ACT_TYPE 
Group By ACT_TYPE_Desc

This solution almost works, but it doesn't show activities that are not assigned Are there any way to include those activities, i have used Left and full join

Please let me know if you need more information.

Thanx

You can try below

Select ACT_TYPE_Desc as [Activity Type] , Count(CR031_DIARY_TEXT) as Count 
From ACT_TYPES AT left Join ACT_Details AD 
On AT.DRY_TRC_TYPE = Ad.ACT_TYPE 
Group By ACT_TYPE_Desc 

Use a left join, and count a column from the ACT_Details table, to include all records in the count, even NULL s:

SELECT
    AT.ACT_TYPE_Desc AS [Activity Type],
    COUNT(AD.ACT_TYPE) AS cnt
FROM ACT_TYPES AT
LEFT JOIN ACT_Details AD
    ON AT.DRY_TRC_TYPE = AD.ACT_TYPE AND <restriction on time>
GROUP BY
    AT.ACT_TYPE_Desc;

Note on the <restriction on time> :

If you need to add any restrictions on the ACT_Details table, then place them in the ON clause of the join. Restrictions on the ACT_TYPES table may go in a WHERE clause.

Try like this :

Select COALESCE(ACT_TYPE_Desc, 'Empty') as [Activity Type] , 
COALESCE(Count(CR031_DIARY_TEXT), 0) as Count

From ACT_TYPES AT LEFT OUTER Join 

ACT_Details AD 
On AT.DRY_TRC_TYPE = Ad.ACT_TYPE 
Group By CR774_DRY_TRC_DESC 

the Coalesce function will return 0 when the value is null. Attantion, The return value (in this case 'empty') is depends on the returning value. It can be a string or a date or any other type depending on the column type.

I think the best way is using the left outer join, in the left outer join the left table is join at the right when if the left row hasn't a corrispondent row with the right table will exist in the join with the columns of right table at null, this is different fron the inner join ( your join ) This is the documentation of left join https://www.w3schools.com/sql/sql_join_left.asp and this is the documentation of the inner join https://www.w3schools.com/sql/sql_join_inner.asp

Select ACT_TYPE_Desc as [Activity Type] , Count(CR031_DIARY_TEXT) as Count 
From ACT_TYPES AT  LEFT Join ACT_Details AD 
On AT.DRY_TRC_TYPE = Ad.ACT_TYPE 
Group By ACT_TYPE_Desc

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