Sql server 查找SQL查询中出现的日期

Sql server 查找SQL查询中出现的日期,sql-server,group-by,Sql Server,Group By,我有以下简化表格: Create table Services ( ServiceID int not null, Created Datetime null, Authorised DateTime null, Closed Datetime null) 如何按月份对DateTime字段进行分组,以计算在特定月份创建、授权或关闭了多少服务 这就是我想要的结果: Month Year Created Authorised

我有以下简化表格:

Create table Services (
  ServiceID   int        not null,
  Created     Datetime   null,
  Authorised  DateTime   null,
  Closed      Datetime   null)
如何按月份对DateTime字段进行分组,以计算在特定月份创建、授权或关闭了多少服务

这就是我想要的结果:

Month Year   Created   Authorised   Closed
 08    2016     10         12         3
 09    2016     12          4         9
 10    2016      7          9         6
...
...

我无法找出GroupBy语句来实现这一点,尽管它看起来很简单。有人能帮忙吗?

您可以分别选择每月创建计数、每月授权计数和每月结束计数,并将三者完全合并

不幸的是,SQL Server没有使用(列)的标准SQL的
联接表,因此完整的外部联接(尤其是涉及两个以上的表)看起来很笨拙

select 
  coalesce(closed.mon, created.mon, authorised.mon) as [Month],
  coalesce(closed.yr, created.yr, authorised.yr) as [Year],
  created.cnt as [Created],
  created.cnt as [Authorised],
  authorised.cnt as [Closed]
from
(
  select month(created) as mon, year(created) as yr, count(*) as cnt
  from services
  group by month(created), year(created)
) created
full outer join
(
  select month(authorised) as mon, year(authorised) as yr, count(*) as cnt
  from services
  group by month(authorised), year(authorised)
) authorised on authorised.mon = created.mon and authorised.yr = created.yr
full outer join
(
  select month(closed) as mon, year(closed) as yr, count(*) as cnt
  from services
  group by month(closed), year(closed)
) closed on  closed.mon = coalesce(created.mon, authorised.mon)
         and closed.yr = coalesce(authorised.yr, created.yr);

您可以分别选择每月创建计数、每月授权计数和每月关闭计数,并将三者完全合并

不幸的是,SQL Server没有使用(列)
的标准SQL的
联接表,因此完整的外部联接(尤其是涉及两个以上的表)看起来很笨拙

select 
  coalesce(closed.mon, created.mon, authorised.mon) as [Month],
  coalesce(closed.yr, created.yr, authorised.yr) as [Year],
  created.cnt as [Created],
  created.cnt as [Authorised],
  authorised.cnt as [Closed]
from
(
  select month(created) as mon, year(created) as yr, count(*) as cnt
  from services
  group by month(created), year(created)
) created
full outer join
(
  select month(authorised) as mon, year(authorised) as yr, count(*) as cnt
  from services
  group by month(authorised), year(authorised)
) authorised on authorised.mon = created.mon and authorised.yr = created.yr
full outer join
(
  select month(closed) as mon, year(closed) as yr, count(*) as cnt
  from services
  group by month(closed), year(closed)
) closed on  closed.mon = coalesce(created.mon, authorised.mon)
         and closed.yr = coalesce(authorised.yr, created.yr);
我尝试使用分组集。 样本数据-

declare @temp table (
    ServiceID       int         not null,
    Created         Datetime    null,
    Authorised      DateTime    null,
    Closed          Datetime    null
)

insert into @temp
select 1, '2016-08-03', '2016-07-03', '2016-02-03' union
select 2, '2016-10-03', '2016-04-03', '2016-08-03' union
select 3, '2016-04-03', '2016-02-03', '2016-12-03' union
select 4, '2016-04-03', '2016-01-03', '2016-10-03'
实际代码-

;with grouped as (
select  created     = format(created, 'yyyyMM'),
        authorised  = format(authorised, 'yyyyMM'),
        closed      = format(closed, 'yyyyMM'),
        count_val   = count(*)
from @temp
group by grouping sets(
            format(created, 'yyyyMM'),
            format(authorised, 'yyyyMM'),
            format(closed, 'yyyyMM'))
)
,combined as(
select [Month]=right(created,2),[year]=left(created,4), created=count_val, authorised, closed
from grouped
where created is not null
union all
select [Month]=right(authorised,2),[year]=left(authorised,4), created, authorised=count_val, closed
from grouped
where authorised is not null
union all
select [Month]=right(closed,2),[year]=left(closed,4), created, authorised, closed=count_val
from grouped
where closed is not null
)
select [Month],[year],created=sum(created),authroised=sum(authorised),closed=sum(closed)
from combined
group by [Month],[year]
我尝试使用分组集。 样本数据-

declare @temp table (
    ServiceID       int         not null,
    Created         Datetime    null,
    Authorised      DateTime    null,
    Closed          Datetime    null
)

insert into @temp
select 1, '2016-08-03', '2016-07-03', '2016-02-03' union
select 2, '2016-10-03', '2016-04-03', '2016-08-03' union
select 3, '2016-04-03', '2016-02-03', '2016-12-03' union
select 4, '2016-04-03', '2016-01-03', '2016-10-03'
实际代码-

;with grouped as (
select  created     = format(created, 'yyyyMM'),
        authorised  = format(authorised, 'yyyyMM'),
        closed      = format(closed, 'yyyyMM'),
        count_val   = count(*)
from @temp
group by grouping sets(
            format(created, 'yyyyMM'),
            format(authorised, 'yyyyMM'),
            format(closed, 'yyyyMM'))
)
,combined as(
select [Month]=right(created,2),[year]=left(created,4), created=count_val, authorised, closed
from grouped
where created is not null
union all
select [Month]=right(authorised,2),[year]=left(authorised,4), created, authorised=count_val, closed
from grouped
where authorised is not null
union all
select [Month]=right(closed,2),[year]=left(closed,4), created, authorised, closed=count_val
from grouped
where closed is not null
)
select [Month],[year],created=sum(created),authroised=sum(authorised),closed=sum(closed)
from combined
group by [Month],[year]

哇!那是一把。我需要看看这是否可以修改,因为我的真实表实际上有更多的日期/时间列。做8个完整的外部连接看起来不是个好主意!不要太担心。DBMS正是为了做这些事情而设计的。它可能在这里使用哈希连接,这应该很快。请记住,只有少数记录需要合并,因为数据已经聚合。哇。那是一把。我需要看看这是否可以修改,因为我的真实表实际上有更多的日期/时间列。做8个完整的外部连接看起来不是个好主意!不要太担心。DBMS正是为了做这些事情而设计的。它可能在这里使用哈希连接,这应该很快。请记住,只需要连接少数记录,因为数据已经聚合。这看起来很有希望,但我在实际表中有更多的日期/时间列以及其他分组列。我会试试看这对我是否可行。谢谢这看起来很有希望,但我在实际表中有更多的日期/时间列以及其他分组列。我会试试看这对我是否可行。谢谢