如何根据特定字段的最大值从两个表中获取行

How to get rows from two tables on maximum value of particular field

我有两个 table 有 date_updated 列。

TableA 如下所示

 con_id         date_updated         type     
--------------------------------------------
123              19/06/2018          2
123              15/06/2018          1     
123              01/05/2018          3  
101              06/04/2018          1
101              05/03/2018          2 

我有 TableB 也有相同的结构

 con_id         date_updated         type     
--------------------------------------------
123              15/05/2018          2  
123              01/05/2018          1  
101              07/06/2018          1

结果 table 应该有最近日期的数据

 con_id         date_updated         type     
--------------------------------------------
123              19/06/2018          2
101              07/06/2018          1  

这里的 date_updated 列是 sql 服务器的 datetime 数据类型。我尝试使用 group by 并选择最大值 date_updated。但我无法在 select 语句中包含列类型。当我在 group by 中使用类型时,结果不正确,因为类型也被分组了。我怎么查询这个。请帮忙

一种方法:

WITH A AS(
    SELECT TOP 1 con_id,
                 date_updated,
                 type
    FROM TableA
    ORDER BY date_updated DESC),
B AS(
    SELECT TOP 1 con_id,
                 date_updated,
                 type
    FROM TableB
    ORDER BY date_updated DESC),
U AS(
    SELECT *
    FROM A
    UNION ALL
    SELECT *
    FROM B)
SELECT *
FROM U;

顶部的 2 个 CTE 从表中获取最近的行,然后结束语句将它们合并在一起。

为了说这行不通的人的利益:

USE Sandbox;
GO

CREATE TABLE tablea (con_id int, date_updated date, [type] tinyint);
CREATE TABLE tableb (con_id int, date_updated date, [type] tinyint);
GO

INSERT INTO tablea 
VALUES
(123,'19/06/2018',2),
(123,'15/06/2018',1),     
(123,'01/05/2018',3),  
(101,'06/04/2018',1),
(101,'05/03/2018',2); 

INSERT INTO tableb
VALUES
(123,'15/05/2018',2),  
(123,'01/05/2018',1), 
(101,'07/06/2018',1);
GO
WITH A AS(
    SELECT TOP 1 con_id,
                 date_updated,
                 [type]
    FROM TableA
    ORDER BY date_updated DESC),
B AS(
    SELECT TOP 1 con_id,
                 date_updated,
                 [type]
    FROM TableB
    ORDER BY date_updated DESC),
U AS(
    SELECT *
    FROM A
    UNION ALL
    SELECT *
    FROM B)
SELECT *
FROM U;

GO
DROP TABLE tablea;
DROP TABLE tableb;

这个returns数据集:

con_id      date_updated type
----------- ------------ ----
123         2018-06-19   2
101         2018-06-07   1

与 OP 的数据相同:

 con_id         date_updated         type     
--------------------------------------------
123              19/06/2018          2
101              07/06/2018          1  
SELECT *
FROM
    (SELECT *, ROW_NUMBER() OVER(Partition By con_id ORDER BY date_updated DESC) as seq
     FROM
        (SELECT * FROM TableA
        UNION ALL
        SELECT * FROM TableB) as tblMain) as tbl2
WHERE seq = 1

希望对您有所帮助:

WITH combined 
     AS( 
select * FROM tableA 
UNION 
select * FROM tableB) 

SELECT t1.con_id, 
       t1.date_updated, 
       t1.type 
FROM  ( 
                SELECT   con_id, 
                         date_updated, 
                         type, 
                         row_number() OVER(partition BY con_id ORDER BY date_updated DESC) AS rownumber
                FROM     combined) t1 
WHERE  rownumber = 1;

可以使用 window 函数完成:

declare @TableA table (con_id int, date_updated date, [type] int)
declare @TableB table (con_id int, date_updated date, [type] int)

insert into @TableA values
  (123, '2018-06-19', 2)
, (123, '2018-06-15', 1)     
, (123, '2018-05-01', 3)  
, (101, '2018-04-06', 1)
, (101, '2018-03-05', 2) 

insert into @TableB values
  (123, '2018-05-15', 2)  
, (123, '2018-05-01', 1)  
, (101, '2018-06-07', 1)

select distinct con_id
    , first_value(date_updated) over (partition by con_id order by con_id, date_updated desc) as con_id
    , first_value([type]) over (partition by con_id order by con_id, date_updated desc) as [type]
from
(Select * from @TableA UNION Select * from @TableB) x