查找具有2个日期列的sql server中特定月份打开的项目数

时间:2013-03-11 17:20:55

标签: sql sql-server sql-server-2008 tsql

我有一些带有日期的示例视图。您如何找到每月打开的物品数量。比如他们打开和关闭的时间OpenDate和CloseDate我想知道1月,2月等每个月有多少人开放?

十月开了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');

3 个答案:

答案 0 :(得分: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。

答案 1 :(得分:1)

使用master..spt_values系统表和APPLY() operator.FYI,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

答案 2 :(得分:0)

我认为你真正想知道的是“每个月,积极处于开放状态的物品数量”。例如,一个项目的营业额是2012年11月15日,封闭是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