使用MAX()分组

时间:2016-05-19 16:55:09

标签: sql-server-2008 group-by

我正在尝试从任务的项目和组中获取最新的结束日期(ProjectsTasksEndDate),但没有成功。 Bellow是SQLFiddle。有什么建议吗?

PS:评论的选择是构建我想要的选择的基础。

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Projects 
(
  id int identity primary key, 
  ProjectID varchar(20),
  ProjectStartDate date,
  ProjectEndDate date
 );

CREATE TABLE ProjectsTasks 
(
  id int identity primary key, 
  ProjectID varchar(20), 
  ProjectsTasksStartDate date,
  ProjectsTasksEndDate date
 );

INSERT INTO Projects
(ProjectID, ProjectStartDate, ProjectEndDate)
VALUES
('1', '2015-01-05', '2015-04-08'),
('2', '2015-01-06', '2015-07-20'),
('3', '2015-02-05', '2015-07-22'),
('4', '2015-03-05', '2015-08-08'),
('5', '2015-03-05', '2015-09-18'),
('6', '2015-04-05', '2015-10-02'),
('7', '2016-07-05', '2016-11-20');

INSERT INTO ProjectsTasks
(ProjectID, ProjectsTasksStartDate, ProjectsTasksEndDate)
VALUES
('1', '2015-01-05', '2015-01-10'),
('1', '2015-01-06', '2015-02-20'),
('1', '2015-02-05', '2015-03-20'),
('1', '2015-03-01', '2015-03-02'),
('1', '2015-01-05', '2015-04-08'),
('2', '2015-06-01', '2015-06-20'),
('3', '2015-12-20', '2015-12-21');

查询1

/*
SELECT
    YEAR (Projects.ProjectEndDate) AS [Year],
    MONTH (Projects.ProjectEndDate) AS [Month],
    COUNT (*) AS [Total]
FROM
    Projects
GROUP BY
    YEAR (Projects.ProjectEndDate),
    MONTH (Projects.ProjectEndDate)
*/


SELECT
    YEAR (MAX(ProjectsTasks.ProjectsTasksEndDate)) AS [Year],
    MONTH (MAX(ProjectsTasks.ProjectsTasksEndDate)) AS [Month],
    COUNT (*) AS [Total]
FROM  Projects 
RIGHT JOIN ProjectsTasks  
  ON Projects.ProjectID = ProjectsTasks.ProjectID
GROUP BY  YEAR (ProjectsTasks.ProjectsTasksEndDate),
          MONTH (ProjectsTasks.ProjectsTasksEndDate)

[预期结果]

| Year | Month | Total |
|------|-------|-------|
| 2015 |     4 |     1 |
| 2015 |     6 |     1 |
| 2015 |    12 |     1 |

1 个答案:

答案 0 :(得分:0)

如果我理解你的意图,你可以使用:

SELECT TOP 1 
     [Year]  = YEAR(ProjectsTasks.ProjectsTasksEndDate)
    ,[Month] = MONTH(ProjectsTasks.ProjectsTasksEndDate)
    ,[Total] = COUNT (*)
FROM  Projects 
RIGHT JOIN ProjectsTasks  
  ON Projects.ProjectID = ProjectsTasks.ProjectID
GROUP BY YEAR(ProjectsTasks.ProjectsTasksEndDate),
         MONTH(ProjectsTasks.ProjectsTasksEndDate)
ORDER BY Year DESC, Month DESC

LiveDemo

修改

SELECT TOP 1 WITH TIES [Year],[Month],[Total]
FROM (
  SELECT  ProjectsTasks.ProjectID
    ,[Year]  = YEAR(MAX(ProjectsTasks.ProjectsTasksEndDate))
    ,[Month] = MONTH(MAX(ProjectsTasks.ProjectsTasksEndDate))
    ,[Total] = COUNT (*)
  FROM  Projects 
  RIGHT JOIN ProjectsTasks  
    ON Projects.ProjectID = ProjectsTasks.ProjectID
  GROUP BY ProjectsTasks.ProjectID,
           YEAR(ProjectsTasks.ProjectsTasksEndDate),
           MONTH(ProjectsTasks.ProjectsTasksEndDate)
) s
ORDER BY ROW_NUMBER() OVER(PARTITION BY ProjectID 
                           ORDER BY [Year] DESC, [Month] DESC);

LiveDemo 2

输出:

╔══════╦═══════╦═══════╗
║ Year ║ Month ║ Total ║
╠══════╬═══════╬═══════╣
║ 2015 ║     4 ║     1 ║
║ 2015 ║     6 ║     1 ║
║ 2015 ║    12 ║     1 ║
╚══════╩═══════╩═══════╝