Sql 如何从两个表中获取特定字段最大值的行
我有两个表更新了Sql 如何从两个表中获取特定字段最大值的行,sql,sql-server,group-by,groupwise-maximum,Sql,Sql Server,Group By,Groupwise Maximum,我有两个表更新了date\u列 表A如下所示 con_id date_updated type -------------------------------------------- 123 19/06/2018 2 123 15/06/2018 1 123 01/05/2018 3 101
date\u
列
表A
如下所示
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
con_id date_updated type
--------------------------------------------
123 19/06/2018 2
101 07/06/2018 1
这里的date\u updated
列是sql server的datetime
数据类型。我通过使用groupby
并选择最大date\u updated
尝试了这一点。但我无法在select
语句中包含列类型。当我在groupby
中使用type时,结果不正确,因为该类型也已分组。我怎么能对此提出质疑。请帮助一种方法:
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;
顶部的两个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的数据相同:
一种方法:
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;
顶部的两个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的数据相同:
希望这有助于:
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;
希望这有助于:
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;
可以使用窗口功能完成:
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
可以使用窗口功能完成:
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
列,而非字段。列,而非字段。
如以下所示??哪张桌子??您需要从那里添加。tblMain
是两个表的并集,tbl2
是带有附加列seq的tblMain。谢谢,我把它编辑成seq,从哪里??哪张桌子??您需要从那里添加。tblMain
是两个表的并集,tbl2
是带有附加列seq的tblMain。谢谢,谢谢你的回答。但我必须使用con_id将此select语句与另一个表连接。从开始可以连接查询。是的,将联合
查询放在另一个CTE中。此查询不会给出已删除的结果。你可能想用一些数据来试运行它。您获得了更新日期列的表格顶部,但缺少基于非OP样本数据的con\u id
@AmithKumar的分组。是的,但OP说他需要根据con\u id
进行分组。如果有两个以上的con\u id
,或者两个表都有相同con\u id
的最新版本,则提供的查询将忽略另一个con\u id
s。感谢您的回答。但我必须使用con_id将此select语句与另一个表连接。从开始可以连接查询。是的,将联合
查询放在另一个CTE中。此查询不会给出已删除的结果。你可能想用一些数据来试运行它。您获得了更新日期列的表格顶部,但缺少基于非OP样本数据的con\u id
@AmithKumar的分组。是的,但OP说他需要根据con\u id
进行分组。如果有两个以上的con\u id
,或者两个表都有相同con\u id
的最新版本,则提供的查询将忽略另一个con\u id
s。这将不起作用,必须在所有UNION
之后添加一个ORDER BY
。例如,尝试select1作为i ORDER BY i UNION选择2作为i ORDER BY i代码>。您将得到一个语法错误。是的,完全没有找到它。这是行不通的,必须在所有的联合之后创建一个orderby
。例如,尝试select1作为i ORDER BY i UNION选择2作为i ORDER BY i代码>。你会得到一个语法错误。是的,完全错过了。