0

我有以下查询:

select 
     Pr.Name, PS.TotalSales, PS.Truesales
From 
     Product Pr
Left Join 
     (select ProdudtId, TotalSales = COUNT(Transaction.Id),
            Truesales = COUNT(DISTINCT case when .Transaction.ExceptionCode  = ' ' then Transaction.Id END)
     from [Transaction] 
     WHERE   
        (ResponseTime < '2013/05/16 01:53:52' and  RequestTime > '2013/05/11 01:53:52')
     Group By 
          ProductId) PS on PS.ProductId = Pr.ProductId

我得到这样的结果:

Name TotalSales  TrueSales
AB     3             2
CD     NULL          NULL
EF     1              1

我想得到这样的结果

Name TotalSales  TrueSales
AB     3             2
CD     0             0
EF     1              1

我希望看到 0 代替 nulls 。谁能告诉我需要在查询中进行哪些更改?

两张表如下:

    1 ) Product 
    ProuctId, Name 

2) Sales
TransactionId, ReqTime, ResTime, ProductId(FK), ExceptionCode
4

2 回答 2

2

更改此行

select Pr.Name, PS.TotalSales, PS.Truesales

select Pr.Name, IsNull(PS.TotalSales, 0) AS TotalSales, IsNull(PS.Truesales, 0) AS Truesales
于 2013-05-20T17:29:19.650 回答
2
select Pr.Name, isnull(PS.TotalSales, 0) TotalSales, isnull(PS.Truesales, 0) Truesales
From Product Pr
Left Join (
select ProdudtId, TotalSales = COUNT(Transaction.Id),
Truesales = COUNT(DISTINCT case when .Transaction.ExceptionCode  = ' ' then Transaction.Id END)
from [Transaction] 
WHERE   
(ResponseTime < '2013/05/16 01:53:52' and  RequestTime > '2013/05/11 01:53:52')
Group By ProductId) PS on PS.ProviderId = Pr.ProviderId
于 2013-05-20T17:28:46.257 回答