0

我有两个有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

结果表应包含最近日期的数据

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

这里的date_updated列是datetimesql server 的数据类型。我通过使用group by和选择最大值来尝试这个date_updated。但我无法在select语句中包含列类型。当我使用输入时group by,结果不正确,因为类型也被分组。我该如何查询这个。请帮忙

4

4 回答 4

2
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
于 2018-06-19T08:09:08.287 回答
1

一种方法:

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 从表中获取您最近的行,然后 end 语句将它们联合在一起。

为了说这不起作用的人的利益:

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;

这将返回数据集:

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  
于 2018-06-19T08:04:33.100 回答
0

希望这可以帮助:

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;
于 2018-06-19T08:37:16.390 回答
0

可以使用窗口函数来完成:

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
于 2018-06-19T08:55:39.117 回答