简体   繁体   中英

Return all rows even if record does not exist or value is zero

I have few tables in SQL Server 2014.

In the below view (dbo.new) and I used left outer join to combine the tables.

 SELECT        
dbo.Division.DivNo, 
dbo.Division.DivName, 
dbo.Categaory.CatNo, 
dbo.Categaory.CatName, 
dbo.Department.DeptNo, 
dbo.Department.DeptName, 
dbo.Dissection.DissNo, 
dbo.Dissection.DissName, 
dbo.WSALES.BRN, 
dbo.WSALES.SELLINC, 
dbo.WSALES.DATE, 
dbo.BRANCH.NAME AS BranchName

FROM            

dbo.Department left outer join
dbo.Categaory left outer join
dbo.Division left outer join
dbo.Dissection ON dbo.Division.DivNo = dbo.Dissection.DivNo ON        dbo.Categaory.CatNo = dbo.Dissection.CatNo ON dbo.Department.DeptNo = dbo.Dissection.DeptNo left outer join
dbo.WSALES ON dbo.Dissection.DissNo = dbo.WSALES.SSECTION left outer join
dbo.BRANCH ON dbo.WSALES.BRN = dbo.BRANCH.NUM

I am using below T-SQL code to extract results,

 SELECT 
  s.[DivNo]as Division,
  s.[DivName] as DivisionName,
  s.[CatNo] Category,
  RTRIM(CAST(s.[CatName] AS VARCHAR(50)) ) AS CategoryName,

    sum(case when s.Date between '2016-08-14' and '2016-08-20' 
           then s.SELLINC else 0 end) ActualSales,
     sum(case when s.Date between '2015-08-16' and '2015-08-22'
           then s.SELLINC else 0 end) LastYrVariance,

     (IsNull(sum(case when s.Date between '2016-08-14' and '2016-08-20' 
           then s.SELLINC else 0 end)-sum(case when s.Date between '2015-08-16' and '2015-08-22'
           then s.SELLINC else 0 end),0)/NullIf(sum(case when s.Date between '2015-08-16' and '2015-08-22'
           then s.SELLINC else 0 end),0))*100 LastYrVariancePercentage


FROM [dbo].[new] s 
WHERE s.BRN = 3 
GROUP BY s.[DivNo],s.[DivName],s.[CatNo], s.[CatName]
ORDER BY     case s.[DivNo]
        when '1' then 1
        when '2' then 2
        when '3' then 3
        when '4' then 4       
        else 5 
    end,

    case s.[CatNo]
        when '1' then 1
        when '2' then 2
        when '3' then 3
        else 4 
    end

If I run the above code few rows/records in a table which has no values are missing.I am getting below results,

Div DivName Cat CatName Sales   Variance   YrVariance
1   Fashion 1   Women's  5      10         5
1   Fashion 2   WomAcc  10      20         10
2   Home    3   Floor   5       10         5
2   Home    4   Food    5       10         5
4   Rest    6   Rests   10      20         10

I am expecting below results,

Div DivName Cat CatName Sales   Variance   YrVariance
1   Fashion 1   Women's  5      10         5
1   Fashion 2   WomAcc  10      20         10
2   Home    3   Floor   5       10         5
2   Home    4   Food    5       10         5
**3 Pets    5   Pet     0       0          0**
4   Rest    6   Rests   10      20         10

I wanted to display all the records even if it has no value.

What changes needed please?

Solution,

In where statement I used,

WHERE s.BRN = 3 or s.SELLINC is null or s.SELLINC = '0'

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