1

我有一些日期的示例视图。您如何找到每月打开的项目数量。在他们打开和关闭的时间之间说 OpenDate 和 CloseDate 我想知道每个月有多少是开放的,一月,二月等?

十月 已打开 4 个项目。

这是一个包含数据的示例表

CREATE TABLE [dbo].[TestDate](
    [ItemTitle] [nvarchar](50) NULL,
    [ItemAttachAssignDate] [date] NULL,
    [ItemDetachConcludeDate] [date] NULL,
    [Status] [nvarchar](50) NULL,
    [FullName] [nvarchar](100) NULL,
    [OpenDate] [date] NULL,
    [CloseDate] [date] NULL
) ON [PRIMARY]

GO


INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2013-02-18 00:00:00', '2013-02-19 00:00:00', 'Done', 'Jeff Hunter      ', '2013-02-18 00:00:00', '2013-02-19 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-15 00:00:00', '2013-02-05 00:00:00', 'Done', 'Tommy Johnson', '2013-01-22 00:00:00', '2013-01-28 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-15 00:00:00', '2013-02-05 00:00:00', 'Done', 'Jeff Haynes', '2012-10-17 00:00:00', '2013-02-01 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-15 00:00:00', '2013-02-05 00:00:00', 'Done', 'Nancy Belkin', '2012-10-28 00:00:00', '2012-12-14 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-15 00:00:00', '2013-02-05 00:00:00', 'Done', 'Rudolph Porche', '2013-01-16 00:00:00', '2013-02-02 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-20 00:00:00', '2013-02-07 00:00:00', 'Done', 'Pat Franks', '2013-01-20 00:00:00', '2013-01-25 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-20 00:00:00', '2013-02-07 00:00:00', 'Done', 'Leslie Jordan', '2012-11-25 00:00:00', '2012-12-04 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-20 00:00:00', '2013-02-07 00:00:00', 'Done', 'Fred Haney', '2012-10-20 00:00:00', '2013-02-04 00:00:00');

INSERT INTO [TestDate]([ItemAttachAssignDate], [ItemDetachConcludeDate], [Status], [FullName], [OpenDate], [CloseDate]) 
    VALUES('2012-10-20 00:00:00', '2013-02-07 00:00:00', 'Done', 'Henry Hanks', '2012-10-31 00:00:00', '2012-11-15 00:00:00');
4

3 回答 3

1

我不明白你问题的这一部分:

在 OpenDate 和 CloseDate 之间说

这是否意味着您需要进行某种日期计算或比较?

但是,如果您只是在寻找每月汇总,有几种方法可以处理这个问题。您可以有一个要加入的日期引用或维度表,其中包含天到月的映射。或者你可以简单地去掉日期的相关部分,即

SELECT DATEPART(YEAR, OpenDate) AS OpenYear
    , DATEPART(MONTH, OpenDate) AS OpenMonth
    , COUNT(*) AS OpenCount
FROM dbo.TestDate
GROUP BY DATEPART(YEAR, OpenDate)
    , DATEPART(MONTH, OpenDate);

您还可以将计数汇总到该月的第一天,即

SELECT CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,OpenDate),0),120) AS OpenMonth
    , COUNT(*) AS OpenCount
FROM dbo.TestDate
GROUP BY CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,OpenDate),0),120);

这仅取决于您对数据的处理方式以及您需要它的呈现方式。HTH。

于 2013-03-11T17:56:25.043 回答
1

带有 master..spt_values 系统表和APPLY()运算符的选项。仅供参考,计算所有年份的月份 COUNT()

SELECT DATENAME(mm, DATEADD(mm, number-1, '20010101')) AS month, o.cnt
FROM master..spt_values v CROSS APPLY (
                                       SELECT COUNT(*) AS cnt
                                       FROM [dbo].[TestDate]
                                       WHERE v.number = MONTH(OpenDate)
                                       ) o                                       
WHERE v.type = 'P' AND v.number BETWEEN 1 AND 12
于 2013-03-11T22:17:29.960 回答
0

我假设您真正想知道的是“每个月,处于打开状态的项目数量”。例如,一个项目的 opendate 是 2012 年 11 月 15 日,closeate 是 2013 年 1 月 2 日,您考虑这个项目开放 3 个月,并且每个月的开放项目计数应该是 +1,因为这个项目。

如果是这样,以下脚本将为您提供帮助。这是一个简单的逻辑,它必须有更好的整洁代码来替换这个

select YearMonth, SUM(OpenItemCount)
from (
select cast(DATEPART(YYYY,CloseDate) as varchar(4)) + CAST(DATEPART(mm,CloseDate) as varchar(2)) as YearMonth, 
       COUNT(*) as OpenItemCount 
from  [dbo].[TestDate]
group by cast(DATEPART(YYYY,CloseDate) as varchar(4)) + CAST(DATEPART(mm,CloseDate) as varchar(2))    
union all    
select cast(DATEPART(YYYY,OpenDate) as varchar(4)) + CAST(DATEPART(mm,OpenDate) as varchar(2)) as YearMonth, 
       COUNT(*) as OpenItemCount 
from  [dbo].[TestDate]
where (cast(DATEPART(YYYY,OpenDate) as varchar(4)) + CAST(DATEPART(mm,OpenDate) as varchar(2))) <> (cast(DATEPART(YYYY,CloseDate) as varchar(4)) + CAST(DATEPART(mm,CloseDate) as varchar(2)))
group by cast(DATEPART(YYYY,OpenDate) as varchar(4)) + CAST(DATEPART(mm,OpenDate) as varchar(2))    
) as A
group by A.YearMonth
于 2013-03-11T18:25:46.360 回答