MIN()针对SQL Server中Group的分区

时间:2015-11-25 13:12:27

标签: sql sql-server

我希望MIN()针对SQL Server中的分区

MY CODE

DROP TABLE #TEMP_TABLE
CREATE TABLE #TEMP_TABLE (SR_NO INT,DATA VARCHAR(5),DATE DATETIME)
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (1,'A','2015-11-25 18:37:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (2,'A','2015-11-25 18:36:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (3,'A','2015-11-25 18:35:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (4,'B','2015-11-25 18:34:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (5,'B','2015-11-25 18:33:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (6,'B','2015-11-25 18:32:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (7,'B','2015-11-25 18:31:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (8,'B','2015-11-25 18:30:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (9,'A','2015-11-25 18:27:59.120')
INSERT INTO #TEMP_TABLE (SR_NO,DATA,DATE) VALUES (10,'B','2015-11-25 18:26:59.120')

SELECT
    ROW_NUMBER() OVER (PARTITION BY DATA ORDER BY DATA DESC) AS ROW_NUM,
    DATA,
    MAX(DATE) AS MAX_DATE   ,
     MIN(DATE) AS MIN_DATE
FROM
    #TEMP_TABLE
GROUP BY 
    DATA

我的输出

enter image description here

预期数据 expected output

我希望将最小日期作为记录组作为最小日期&最大日期。 意味着可以说上面数据中的第一组记录是

data time
A    5:00
A    4:00
A    3:00
B    2:00
A    1:00

然后我想将输出作为

返回
data    max  min
a      5:00  3:00

2 个答案:

答案 0 :(得分:1)

SELECT
    MAX(DATA) AS DATA,
    MAX([DATE]) AS MAX_DATE,
    MIN([DATE]) AS MIN_DATE
FROM (
    SELECT *, val = 
        ROW_NUMBER() OVER (ORDER BY [DATE] DESC) -
        ROW_NUMBER() OVER (PARTITION BY DATA ORDER BY [DATE] DESC)
    FROM #TEMP_TABLE
) t
GROUP BY val

输出 -

DATA  MAX_DATE                MIN_DATE
----- ----------------------- -----------------------
A     2015-11-25 15:53:12.940 2015-11-25 15:51:12.940
B     2015-11-25 15:50:12.940 2015-11-25 15:48:12.943
C     2015-11-25 15:47:12.943 2015-11-25 15:46:12.943
A     2015-11-25 15:43:12.943 2015-11-25 15:43:12.943
D     2015-11-25 15:45:12.943 2015-11-25 15:42:12.943

更新 -

SELECT *,
    row_id = ROW_NUMBER() OVER (ORDER BY [DATE] DESC),
    row_id_by_data = ROW_NUMBER() OVER (PARTITION BY DATA ORDER BY [DATE] DESC),
    group_id = ROW_NUMBER() OVER (ORDER BY [DATE] DESC) -
    ROW_NUMBER() OVER (PARTITION BY DATA ORDER BY [DATE] DESC)
FROM #TEMP_TABLE

结果 -

enter image description here

答案 1 :(得分:-1)

如果我理解你的问题,我认为你正在寻找像我这里所拥有的东西。 (我改变了表格声明,因为在我测试之后我不想让它闲置)

DECLARE @TEMP_TABLE TABLE (SR_NO INT,DATA VARCHAR(5),DT DATETIME)

INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (1,'A',(DATEADD(MINUTE,11,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (2,'A',(DATEADD(MINUTE,10,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (3,'A',(DATEADD(MINUTE,9,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (4,'B',(DATEADD(MINUTE,8,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (5,'B',(DATEADD(MINUTE,7,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (6,'B',(DATEADD(MINUTE,6,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (7,'C',(DATEADD(MINUTE,5,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (8,'C',(DATEADD(MINUTE,4,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (9,'D',(DATEADD(MINUTE,3,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (10,'D',(DATEADD(MINUTE,2,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (1,'A',(DATEADD(MINUTE,1,GETDATE())))
INSERT INTO @TEMP_TABLE (SR_NO,DATA,DT) VALUES (1,'B',GETDATE())

SELECT DATA, MAX(DT) as MaxDate, MIN(DT) as MinDate
FROM @TEMP_TABLE
GROUP BY DATA