Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 获取特定列的默认值为0的Sql查询_Sql Server - Fatal编程技术网

Sql server 获取特定列的默认值为0的Sql查询

Sql server 获取特定列的默认值为0的Sql查询,sql-server,Sql Server,我正面临一个问题和一个疑问 我的问题是:- SELECT MONTH(o.OrderDate) as MonthValue, YEAR(o.OrderDate) as YearValue, C.CustomerTypeID, Count(o.Total) as NoOfOrders FROM Orders o RIGHT JOIN Customers C on C.CustomerID = o.CustomerID WHERE o.O

我正面临一个问题和一个疑问

我的问题是:-

SELECT MONTH(o.OrderDate) as MonthValue, 
       YEAR(o.OrderDate) as YearValue, 
       C.CustomerTypeID, Count(o.Total) as NoOfOrders
FROM Orders o        
RIGHT JOIN Customers C on C.CustomerID = o.CustomerID      

WHERE o.OrderDate >= CONVERT(DATETIME, '1/1/2013 00:00:00 AM')
AND o.OrderDate <= CONVERT(DATETIME, '12/31/2013 23:59:59 PM')
GROUP BY MONTH(o.OrderDate), 
         YEAR(o.OrderDate), 
         C.CustomerTypeID 
ORDER BY MONTH(o.OrderDate),
         YEAR(o.OrderDate),
         C.CustomerTypeID 
对于第2个月,客户类型3没有结果,因此它不会出现在结果中

但我想显示“0”作为它的默认结果,如下所示:-

2              2013            3             0

提前感谢。

您可能会执行一个完整的外部联接,它将为您提供所有客户,并为丢失的数据重新调整null。然后可以选择ISNULL(NoOfOrders,0)来获取0,而不是null


我不确定这是否可行,但您可以尝试一下。

您可能可以进行一次完整的外部联接,它将为您提供所有客户,并为丢失的数据重新调整null。然后可以选择ISNULL(NoOfOrders,0)来获取0,而不是null


我不确定这是否行得通,但你可以试试。

编辑:你必须在所有月份都加入,才能得到缺少的条目。因此,为了获得最佳可读性,请先交叉加入所有客户和月份,然后再加入外部订单

SELECT all_months.MonthValue, 
       all_months.YearValue, 
       C.CustomerTypeID,
       Count(o.Total) as NoOfOrders
FROM 
(
  SELECT distinct MONTH(OrderDate) as MonthValue, YEAR(OrderDate) as YearValue
  FROM orders 
  WHERE YEAR(OrderDate) = 2013
) all_months        
CROSS JOIN Customers C 
LEFT OUTER JOIN Orders o 
  ON o.CustomerID = C.CustomerID 
  AND MONTH(o.OrderDate) = all_months.MonthValue 
  AND YEAR(o.OrderDate) = all_months.YearValue
GROUP BY all_months.MonthValue, 
         all_months.YearValue, 
         C.CustomerTypeID 
ORDER BY all_months.MonthValue,
         all_months.YearValue,
         C.CustomerTypeID ;

已编辑:您必须在所有月份外加入才能获得缺少的条目。因此,为了获得最佳可读性,请先交叉加入所有客户和月份,然后再加入外部订单

SELECT all_months.MonthValue, 
       all_months.YearValue, 
       C.CustomerTypeID,
       Count(o.Total) as NoOfOrders
FROM 
(
  SELECT distinct MONTH(OrderDate) as MonthValue, YEAR(OrderDate) as YearValue
  FROM orders 
  WHERE YEAR(OrderDate) = 2013
) all_months        
CROSS JOIN Customers C 
LEFT OUTER JOIN Orders o 
  ON o.CustomerID = C.CustomerID 
  AND MONTH(o.OrderDate) = all_months.MonthValue 
  AND YEAR(o.OrderDate) = all_months.YearValue
GROUP BY all_months.MonthValue, 
         all_months.YearValue, 
         C.CustomerTypeID 
ORDER BY all_months.MonthValue,
         all_months.YearValue,
         C.CustomerTypeID ;
试试这个:

SELECT  2013 as [Year],
        months.number,
        Amount = SUM(COALESCE(o.Total,0)),
        C.CustomerType
FROM    Customers C
CROSS JOIN
(SELECT number FROM master..spt_values WHERE type='p' and number between 1 and 12) months
LEFT JOIN [Orders] o on C.CustomerId = o.CustomerId and YEAR(o.OrderDate) = 2013 and MONTH(o.OrderDate) = months.number
GROUP BY months.number, C.CustomerType 
ORDER BY months.number, C.CustomerType
试试这个:

SELECT  2013 as [Year],
        months.number,
        Amount = SUM(COALESCE(o.Total,0)),
        C.CustomerType
FROM    Customers C
CROSS JOIN
(SELECT number FROM master..spt_values WHERE type='p' and number between 1 and 12) months
LEFT JOIN [Orders] o on C.CustomerId = o.CustomerId and YEAR(o.OrderDate) = 2013 and MONTH(o.OrderDate) = months.number
GROUP BY months.number, C.CustomerType 
ORDER BY months.number, C.CustomerType


您是否尝试过左连接以显示行,并使用isnull来处理空值?感谢@Brandon的快速重播,但没有空值,因为第3类客户在第2个月内没有任何订单,因此它不会出现在结果中。您的
右连接
没有意义,因为订单应该属于客户,对吗?您是否尝试过
LEFT JOIN
,也就是说,应该显示有订单或没有订单的客户,而没有订单的客户将有0作为
NoOfOrders
?是@Edper,我尝试过,但结果相同。@AnandMeena您确定第二个月有CustomerTypeID=3吗?你能显示你的实际数据吗(如果你允许的话)?你有没有尝试过left join来显示行,isnull来处理空值?谢谢@Brandon的快速重播,但是没有空值,因为第三类客户在第二个月内没有任何订单,所以它不会出现在结果中。您的
右连接
没有意义,因为订单应该属于客户,对吗?您是否尝试过
LEFT JOIN
,也就是说,应该显示有订单或没有订单的客户,而没有订单的客户将有0作为
NoOfOrders
?是@Edper,我尝试过,但结果相同。@AnandMeena您确定第二个月有CustomerTypeID=3吗?你能显示你的实际数据吗(如果你允许的话)?谢谢@Thorsten,但它给出了错误,请看,对不起,我没有花太多时间来写这篇文章,所以它显得相当草率。我又写了一篇,希望它现在能工作。谢谢@Thorsten:-),它现在工作得很好。我们可以得到一年52周的数据吗?您好,您可以得到月份(OrderDate),但周没有周(OrderDate)。您可以使用DATEPART(wk,OrderDate)或DATEPART(isowk,OrderDate)来获取一周。但请仔细阅读以下内容:,因为对于什么是第1周,什么是第52周,甚至是第53周,都有不同的观点。如果不是每个月(或每周)都出现在表顺序中,那么您就必须生成数字。搜索有关如何在SQL Server中生成数字的技术。谢谢@Thorsten,但它给出了错误,请参见抱歉,我没有花太多时间来编写此内容,因此它显得相当草率。我又写了一篇,希望它现在能工作。谢谢@Thorsten:-),它现在工作得很好。我们可以得到一年52周的数据吗?您好,您可以得到月份(OrderDate),但周没有周(OrderDate)。您可以使用DATEPART(wk,OrderDate)或DATEPART(isowk,OrderDate)来获取一周。但请仔细阅读以下内容:,因为对于什么是第1周,什么是第52周,甚至是第53周,都有不同的观点。如果不是每个月(或每周)都出现在表顺序中,那么您就必须生成数字。搜索有关如何在SQL Server中生成数字的技术。感谢@Nisarg,它工作得很好,我从4小时开始就在苦苦挣扎,谢谢你是冠军:-)现在,如果想获得一年52周的数据,比?你能帮我吗?需要查看周信息。我很快就会回来。对于周,你只需要将月(o.OrderDate)替换为日期部分(wk,o.OrderDate),范围将从1更改为52。感谢@Nisarg,它工作正常,我从4小时开始就在努力,谢谢你是冠军:-)现在,如果想获得一年中52周的数据?你能帮我吗?需要每周检查。我会很快回复你。对于每周,你只需要将月份(o.OrderDate)替换为日期部分(wk,o.OrderDate),范围将从1更改为52。