如何在表格中找到最大值

时间:2016-10-18 09:38:28

标签: sql-server sql-server-2012

我们有一张桌子。

ID     |department_ID|mod_date
---------------------------------
 A     | 33          | 12/03/2014
 A     | 44          | 23/04/2015
 A     | 55          | 20/05/2016
 B     | 44          | 26/01/2014
 B     | 33          | 24/02/2015
 B     | 12          | 24/12/2015
 B     | 55          | 12/02/2016
 C     | 13          | 22/05/2014
 C     | 14          | 01/05/2015
 C     | 55          | 02/07/2016
 C     | 14          | 03/08/2016
 C     | 55          | 05/10/2016

在此表格中,我们需要提取其中department_id为55但在12/10/15之后更改为55以外的任何内容的ID。

请帮助查询,因为我需要为大量数据执行此操作。

5 个答案:

答案 0 :(得分:1)

您可以使用子查询来实现此目的:

CREATE TABLE #0 (ID varchar(100), department_ID int, mod_date date)

SET DATEFORMAT DMY

INSERT #0 VALUES
('A',33,'12/03/2014'),('A',44,'23/04/2015'),('A',55,'20/05/2016')
,('B',44,'26/01/2014'),('B',33,'24/02/2015'),('B',12,'24/12/2015')
,('B',55,'12/02/2016'),('C',13,'22/05/2014'),('C',14,'01/05/2015')
,('C',55,'02/07/2016'),('C',14,'03/08/2016'),('C',55,'05/10/2016');

SELECT DISTINCT latest.ID
FROM
(
    SELECT ID, mod_date = MAX(mod_date)
    FROM #0
    GROUP BY ID
) maximum
JOIN #0 latest
    ON latest.ID = maximum.ID
    AND latest.mod_date = maximum.mod_date
JOIN #0 older
    ON older.ID = latest.ID
    AND older.mod_date < latest.mod_date
    AND (older.department_ID <> latest.department_ID OR older.department_ID IS NULL)
WHERE latest.department_ID = 55
    AND older.mod_date > '2015-10-12'

答案 1 :(得分:1)

如果理解正确,那么您可以查找此脚本

SELECT ID
FROM TableName
WHERE department_ID = 55 AND mod_date > '<your_date>'
GROUP BY ID
Having COUNT(department_ID) = 1

答案 2 :(得分:0)

也许这个?

declare @temp table (id varchar(1), department_id int, mod_Date date)

insert into @temp values ('A', 33, '20140312')
insert into @temp values ('A', 44, '20150423')
insert into @temp values ('A', 55, '20160520')
insert into @temp values ('B', 44, '20140126')
insert into @temp values ('B', 33, '20150224')
insert into @temp values ('B', 12, '20151224')
insert into @temp values ('B', 55, '20160212')
insert into @temp values ('C', 13, '20140522')
insert into @temp values ('C', 14, '20150501')
insert into @temp values ('C', 55, '20160702')
insert into @temp values ('C', 14, '20160803')
insert into @temp values ('C', 55, '20161005')

select id, max(mod_Date) as lastchanged
from   @temp
where  department_id = 55 
and    mod_date > '20151012'
group by id
having count(department_id) = 1

它返回:

id  lastchanged 
A   2016-05-20  
B   2016-02-12  

答案 3 :(得分:0)

按日期排序: -

SELECT * FROM table WHERE mod_date > '2015/10/12' AND 
department_ID=55 ORDER BY mod_date DESC Limit 1;

按ID排序: -

SELECT * FROM table WHERE mod_date > '2015/10/12' AND 
department_ID=55 ORDER BY ID Limit 1;

答案 4 :(得分:0)

您可以使用ROW_NUMBER()两次执行此操作。将以下两列添加到数据中:

SELECT  ID,
        Department_ID,
        mod_date,
        RowNum = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY mod_date DESC),
        RowNum2 = ROW_NUMBER() OVER(PARTITION BY ID, Department_ID ORDER BY mod_date)
FROM    #T
WHERE   mod_Date > '20151012';

给出

ID  Department_ID   mod_date    RowNum  RowNum2
--------------------------------------------------
A   55              2016-05-20  1       1
B   12              2015-12-24  2       1
B   55              2016-02-12  1       1
C   14              2016-08-03  2       1
C   55              2016-07-02  3       1
C   55              2016-10-05  1       2

由于您只对当前部门ID为55的位置感兴趣,因此您需要过滤RowNum = 1 AND Department_ID = 55,其中包含:

ID  Department_ID   mod_date    RowNum  RowNum2
--------------------------------------------------
A   55              2016-05-20  1       1
B   55              2016-02-12  1       1
C   55              2016-10-05  1       2

请注意RowNum2的{​​{1}}为2,因为这是C在55期间的第二次,您只对以前ID未被更改的位置感兴趣到55,所以你需要ID = C的进一步谓词给出最终查询:

RowNum2 = 1

完整的工作演示

SELECT  ID,
        Department_ID,
        mod_date
FROM    (   SELECT  ID,
                    Department_ID,
                    mod_date,
                    RowNum = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY mod_date DESC),
                    RowNum2 = ROW_NUMBER() OVER(PARTITION BY ID, Department_ID ORDER BY mod_date)
            FROM    #T
            WHERE   mod_Date > '20151012'
        ) AS t
WHERE   Department_ID = 55
AND     RowNum = 1      -- CURRENT RECORD   
AND     RowNum2 = 1;    -- FIRST TIME ID HAS BEEN THIS DEPARTMENT

<强>结果

CREATE TABLE #T (ID CHAR(1), Department_ID INT, mod_date DATE);
INSERT #T
VALUES
    ('A', 33, '20140312'),
    ('A', 44, '20150423'),
    ('A', 55, '20160520'),
    ('B', 44, '20140126'),
    ('B', 33, '20150224'),
    ('B', 12, '20151224'),
    ('B', 55, '20160212'),
    ('C', 13, '20140522'),
    ('C', 14, '20150501'),
    ('C', 55, '20160702'),
    ('C', 14, '20160803'),
    ('C', 55, '20161005');

SELECT  ID,
        Department_ID,
        mod_date
FROM    (   SELECT  ID,
                    Department_ID,
                    mod_date,
                    RowNum = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY mod_date DESC),
                    RowNum2 = ROW_NUMBER() OVER(PARTITION BY ID, Department_ID ORDER BY mod_date)
            FROM    #T
            WHERE   mod_Date > '20151012'
        ) AS t
WHERE   Department_ID = 55
AND     RowNum = 1      -- CURRENT RECORD   
AND     RowNum2 = 1;    -- FIRST TIME ID HAS BEEN THIS DEPARTMENT