合并不返回0

时间:2017-11-19 20:02:02

标签: sql sql-server

我正在创建一个存储过程来返回经理和向他们报告的任何人的总销售额。如果没有销售,我希望它返回总共零,但如果在给定时间段内没有销售,则它当前没有返回任何内容。

 static void Main(string[] args)
        {             
            Baltile balti = new Baltile();
            while (true)
            {
              "data input block"
              for (int i = 0; i < unitCount; i++)
              {
               "more data input"    
               if (i>2)
               {
                Balta balta = new Balta(x, y, radius, extra, unitId, 0);
                Baltile.populate(balta);//the CS0120 error is here
               }
             }
           }
         }

2 个答案:

答案 0 :(得分:0)

问题是group by。 。 。如果没有符合条件的行,则不返回任何行。

一种方法是:

with g as (
      <your query here>
     )
select g.*
from g
union all
select name, totalsales
from (select NULL as name, 0 as totalsales) x
where not exists (select 1 from g);

答案 1 :(得分:0)

你可以试试这个。

SELECT          
        FirstName + ' ' + e.LastName AS Name,           
       COALESCE(SUM(od.UnitPrice * od.Quantity), 0) AS TotalSales
    FROM 
       dbo.Employees e
       LEFT OUTER JOIN dbo.Orders o ON e.EmployeeID = o.EmployeeID  
          AND o.OrderDate >= @beginDate
          AND o.OrderDate <= @endDate 
       LEFT OUTER JOIN dbo.[Order Details] od ON o.OrderID = od.OrderID
    WHERE 
       (e.EmployeeID = @managerID OR e.ReportsTo = @managerID)
    GROUP BY 
       e.FirstName,
       e.LastName