<legend id='Njv1Y'><style id='Njv1Y'><dir id='Njv1Y'><q id='Njv1Y'></q></dir></style></legend>
    1. <i id='Njv1Y'><tr id='Njv1Y'><dt id='Njv1Y'><q id='Njv1Y'><span id='Njv1Y'><b id='Njv1Y'><form id='Njv1Y'><ins id='Njv1Y'></ins><ul id='Njv1Y'></ul><sub id='Njv1Y'></sub></form><legend id='Njv1Y'></legend><bdo id='Njv1Y'><pre id='Njv1Y'><center id='Njv1Y'></center></pre></bdo></b><th id='Njv1Y'></th></span></q></dt></tr></i><div id='Njv1Y'><tfoot id='Njv1Y'></tfoot><dl id='Njv1Y'><fieldset id='Njv1Y'></fieldset></dl></div>
      <tfoot id='Njv1Y'></tfoot>
        <bdo id='Njv1Y'></bdo><ul id='Njv1Y'></ul>

      <small id='Njv1Y'></small><noframes id='Njv1Y'>

    2. 在 Group By 查询中包含缺失的月份

      时间:2024-04-16
        <bdo id='WqtZ3'></bdo><ul id='WqtZ3'></ul>

            <small id='WqtZ3'></small><noframes id='WqtZ3'>

            • <i id='WqtZ3'><tr id='WqtZ3'><dt id='WqtZ3'><q id='WqtZ3'><span id='WqtZ3'><b id='WqtZ3'><form id='WqtZ3'><ins id='WqtZ3'></ins><ul id='WqtZ3'></ul><sub id='WqtZ3'></sub></form><legend id='WqtZ3'></legend><bdo id='WqtZ3'><pre id='WqtZ3'><center id='WqtZ3'></center></pre></bdo></b><th id='WqtZ3'></th></span></q></dt></tr></i><div id='WqtZ3'><tfoot id='WqtZ3'></tfoot><dl id='WqtZ3'><fieldset id='WqtZ3'></fieldset></dl></div>
              <legend id='WqtZ3'><style id='WqtZ3'><dir id='WqtZ3'><q id='WqtZ3'></q></dir></style></legend><tfoot id='WqtZ3'></tfoot>

                  <tbody id='WqtZ3'></tbody>

                本文介绍了在 Group By 查询中包含缺失的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我觉得我在这里遇到了困难... :(

                I think I have a tough one here... :(

                我正在尝试按月获取订单数,即使是零时也是如此.这是问题查询:

                I am trying to get an order count by month, even when zero. Here's the problem query:

                SELECT datename(month, OrderDate) as Month, COUNT(OrderNumber) AS Orders
                FROM OrderTable
                WHERE OrderDate >= '2012-01-01' and OrderDate <= '2012-06-30'
                GROUP BY year(OrderDate), month(OrderDate), datename(month, OrderDate)
                

                我希望得到的是这样的:

                What I'm looking to get is something like this:

                Month            Orders
                -----            ------
                January          10
                February         7
                March            0
                April            12
                May              0
                June             5
                

                ...但我的查询跳过了三月和五月的一行.我已经尝试过 COALESCE(COUNT(OrderNumber), 0)ISNULL(COUNT(OrderNumber), 0) 但我很确定分组会导致不工作.

                ...but my query skips a row for March and May. I've tried COALESCE(COUNT(OrderNumber), 0) and ISNULL(COUNT(OrderNumber), 0) but I'm pretty sure the grouping is causing that not to work.

                推荐答案

                这个解决方案不需要你硬编码你可能想要的月份列表,你需要做的就是提供任何开始日期和任何结束日期,它将为您计算月份边界.它在输出中包含年份,以便支持 12 个月以上,并且您的开始和结束日期可以跨越年份边界,并且仍然正确排序并显示正确的月份年.

                This solution doesn't require you to hard-code the list of months you might want, all you need to do is provide any start date and any end date, and it will calculate the month boundaries for you. It includes year in the output so that it will support more than 12 months and so that your start and end dates can cross a year boundary and still order correctly and show the correct month and year.

                DECLARE @StartDate SMALLDATETIME, @EndDate SMALLDATETIME;
                
                SELECT @StartDate = '20120101', @EndDate = '20120630';
                
                ;WITH d(d) AS 
                (
                  SELECT DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate), 0))
                  FROM ( SELECT TOP (DATEDIFF(MONTH, @StartDate, @EndDate) + 1) 
                    n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
                    FROM sys.all_objects ORDER BY [object_id] ) AS n
                )
                SELECT 
                  [Month]    = DATENAME(MONTH, d.d), 
                  [Year]     = YEAR(d.d), 
                  OrderCount = COUNT(o.OrderNumber) 
                FROM d LEFT OUTER JOIN dbo.OrderTable AS o
                  ON o.OrderDate >= d.d
                  AND o.OrderDate < DATEADD(MONTH, 1, d.d)
                GROUP BY d.d
                ORDER BY d.d;
                

                这篇关于在 Group By 查询中包含缺失的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同 下一篇:MySQL SELECT 按组最频繁

                相关文章

                  <bdo id='pvmEw'></bdo><ul id='pvmEw'></ul>

              1. <i id='pvmEw'><tr id='pvmEw'><dt id='pvmEw'><q id='pvmEw'><span id='pvmEw'><b id='pvmEw'><form id='pvmEw'><ins id='pvmEw'></ins><ul id='pvmEw'></ul><sub id='pvmEw'></sub></form><legend id='pvmEw'></legend><bdo id='pvmEw'><pre id='pvmEw'><center id='pvmEw'></center></pre></bdo></b><th id='pvmEw'></th></span></q></dt></tr></i><div id='pvmEw'><tfoot id='pvmEw'></tfoot><dl id='pvmEw'><fieldset id='pvmEw'></fieldset></dl></div>
                  <legend id='pvmEw'><style id='pvmEw'><dir id='pvmEw'><q id='pvmEw'></q></dir></style></legend>

                  <small id='pvmEw'></small><noframes id='pvmEw'>

                  <tfoot id='pvmEw'></tfoot>