Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
按加入日期分组+;在where-sql中添加month_Sql_Sql Server - Fatal编程技术网

按加入日期分组+;在where-sql中添加month

按加入日期分组+;在where-sql中添加month,sql,sql-server,Sql,Sql Server,我有下表: 人 身份证 名字 创建日期 离开的日期 我想要的是所有月份的列表,以及加入的用户数和离开的用户数 我已经有了以下查询:它返回我过去一个月加入的新用户数量: select MONTH(date_created) 'Month', YEAR(date_created) 'Year', count(*) as 'New Users' from person p where YEAR(date_created) = 2018 and MONTH( p.date_created) = 5

我有下表:

  • 身份证
  • 名字
  • 创建日期
  • 离开的日期
我想要的是所有月份的列表,以及加入的用户数和离开的用户数

我已经有了以下查询:它返回我过去一个月加入的新用户数量:

select MONTH(date_created) 'Month', YEAR(date_created) 'Year', count(*) as 'New Users'
from person p 
where YEAR(date_created) = 2018 and MONTH( p.date_created) = 5
group by MONTH(date_created), YEAR(date_created)
它返回我想要的:

我将如何编辑此内容以包含一份年度报告,并在“新用户”报告旁边添加列“Users left”

我的结果是:

MONTH YEAR NEW USERS  USERS LEFT
1    2019  10          5
我将使用
交叉应用
“取消打印”数据:

select v.[year], v.[month], sum(v.iscreated) as num_created,
       sum(v.isleft) as num_left
from person p cross apply
     (values (year(p.date_created), month(p.date_created), 1, 0),
             (year(p.date_left), month(p.date_left), 0, 1)
     ) v([year], [month], iscreated, isleft)
group by v.[year], v.[month]
order by v.[year], v.[month];

直截了当的方法可能是将所有入口和所有叶完全外部连接起来。SQL Server没有使用,这让这有点尴尬,因此我们必须在上使用
,而在月份和年份上使用
合并

select
  coalesce(pin.year, pout.year) as year,
  coalesce(pin.month, pout.month) as month,
  coalesce(pin.cnt, 0) as count_in,
  coalesce(pout.cnt, 0) as count_out
from
(
  select year(date_created) as year, month(date_created) as month, count(*) as cnt
  from person 
  group by year(date_created), month(date_created)
) pin
full outer join
(
  select year(date_left) as year, month(date_left) as month, count(*) as cnt
  from person
  group by month(date_left), year(date_left)
) pout on pout.year = pin.year and pout.month = pin.month
order by year, month;

也许你可以用一个子选区来做?现在就尝试使用ORACLE语法,我不确定它是否在SQL Server中工作

SELECT * FROM 
( 
    select MONTH(date_created) 'Month_C', YEAR(date_created) 'Year_C', count(*) as 'New Users'
    from person p 
    where YEAR(date_created) = 2018 and MONTH( p.date_created) = 5
    group by MONTH(date_created), YEAR(date_created)
)created_user,
(
    select MONTH(date_left) 'Month_L', YEAR(date_left) 'Year_L', count(*) as 'New Users'
    from person p 
    where YEAR(date_left) = 2018 and MONTH( p.date_left) = 5
    group by MONTH(date_left), YEAR(date_left)
) left_user
where created_user.Year_C = left_user.Year_L
and created_user.Month_C = left_user.Month_L

我看不到图像了吗?有人能看到吗?你能提供一些样本数据和预期结果吗?我们不知道您的数据库架构,因此如果没有更多信息,我们将无法帮助您。另一方面,我会在
WHERE
子句中避免类似
YEAR(date\u created)
的内容;使用类似的东西会导致查询不可搜索,导致索引被忽略,从而导致性能低下。这是您添加的预期结果的图像,但示例数据和模式在这里非常重要。我们只知道您的表现在有一列,
date\u created
。仅此栏无法告诉我们用户何时离开。您的查询在
行中的
)处返回一个错误,它说
预期为AS、ID或引用的ID
啊,是的,
中的
是一个保留字。我已经更新了答案并更改了表格别名。