Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用Count和GroupBy显示null而不是0的SQL Server查询_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql server 使用Count和GroupBy显示null而不是0的SQL Server查询

Sql server 使用Count和GroupBy显示null而不是0的SQL Server查询,sql-server,sql-server-2008,tsql,Sql Server,Sql Server 2008,Tsql,我有以下疑问: 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

我有以下疑问:

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     0             0
EF     1              1
我希望看到0代替空值。 有人能告诉我在查询中需要做什么更改吗

这两个表格如下:

    1 ) Product 
    ProuctId, Name 

2) Sales
TransactionId, ReqTime, ResTime, ProductId(FK), ExceptionCode
换行

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

换行

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


+1太好了!这管用!我花了这么多时间在像这样简单的事情上。+1太棒了!这管用!我花了这么多时间在这样简单的事情上。
select Pr.Name, PS.TotalSales, PS.Truesales
select Pr.Name, IsNull(PS.TotalSales, 0) AS TotalSales, IsNull(PS.Truesales, 0) AS Truesales