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

时间:2018-06-19 08:01:22

标签: sql sql-server group-by groupwise-maximum

我有两个包含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列是sql server的datetime数据类型。我使用group by并选择最大date_updated来尝试此操作。但我无法在select语句中包含列类型。当我在group by中使用类型时,结果不正确,因为类型也被分组。我怎么能查询这个。请帮忙

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

一种方法:

formlyConfig.setType({
    name: 'select2',
    template: '<select class="form-control" ng-model="model[options.key]"></select><div class="div-after-select"></div>',
    wrapper: ['bootstrapLabel', 'bootstrapHasError'],
    defaultOptions: function defaultOptions(options) {
        /* jshint maxlen:195 */
        var ngOptions = options.templateOptions.ngOptions || 'option[to.valueProp || \'value\'] as option[to.labelProp || \'name\'] group by option[to.groupProp || \'group\'] for option in to.options';
        return {
            ngModelAttrs: _defineProperty({}, ngOptions, {
                value: options.templateOptions.optionsAttr || 'ng-options'
            })
        };
    },
    apiCheck: function apiCheck(check) {
        return {
            templateOptions: {
                options: check.arrayOf(check.object),
                optionsAttr: check.string.optional,
                labelProp: check.string.optional,
                valueProp: check.string.optional,
                groupProp: check.string.optional
            }
        };
    }
});

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

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

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;

这将返回数据集:

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;

与OP的数据相同:

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

答案 2 :(得分: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;

答案 3 :(得分: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