【问题标题】:Query to group by for last n number of days查询过去 n 天的分组依据
【发布时间】:2022-11-17 11:23:22
【问题描述】:

我有一张员工表,其中包含 empnum、姓名、年龄、加入日期。

我需要找出过去 90 天的记录数,按年龄分组。 如何在 TSQL 中编写查询。

【问题讨论】:

    标签: tsql


    【解决方案1】:
    SELECT age , count(*)  
    from table1 
    where DATEADD(dd,-90,GETUTCDATE()) 
    Group by age 
    

    【讨论】:

    • 你的where条件不对
    【解决方案2】:

    您可以应用过滤器,然后按分组。

    SELECT age, COUNT(*) as CountOfEmployees
    FROM employees
    WHERE dateOfJoin < DATEADD(dd,-90,GETDATE())
    group by age
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-26
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多