Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TSQL-按2种类型选择最新日期_Sql_Sql Server_Tsql - Fatal编程技术网

TSQL-按2种类型选择最新日期

TSQL-按2种类型选择最新日期,sql,sql-server,tsql,Sql,Sql Server,Tsql,我需要在SQL Server中选择数据。 我有一个这样的数据示例 DECLARE @tblA TABLE (ID int, SubId int, Createdate DateTime) INSERT INTO @tblA VALUES (1, 1, '10/21/2020') , (2, 1, '10/21/2020') , (3, 1, '10/27/2020')

我需要在SQL Server中选择数据。 我有一个这样的数据示例

DECLARE @tblA TABLE (ID int, SubId int, Createdate DateTime)

INSERT INTO @tblA VALUES (1, 1, '10/21/2020')
                         , (2, 1, '10/21/2020')
                         , (3, 1, '10/27/2020')
                         , (4, 2, '10/21/2020')
                         , (5, 2, '10/21/2020')
                         , (6, 1, '10/21/2020')
                         , (7, 2, '10/23/2020')
                         , (8, 2, '10/22/2020')
                         , (9, 1, '10/25/2020')
                         , (10, 3, '10/21/2020')
我想在表中获得4条记录,这些记录将有很多子ID 2按子ID=1记录最新日期 2按子ID=2记录最新日期 下面的示例中,我希望Select具有类似的表输出

DECLARE @tblOutput Table (ID int, SubId int, Createdate DateTime)

INSERT INTO @tblOutput VALUES (3, 1, '10/27/2020')
                        , (9, 1, '10/25/2020')
                        , (7, 2, '10/23/2020')
                        , (8, 2, '10/22/2020')
工具提示

我试着与工会合作,但这只是工会之后的秩序。这不是我想要的结果

请帮我选择这个。 谢谢。

您可以使用行号功能,如下所示:

Create table tblA  (ID int, SubId int, Createdate DateTime)

Insert Into tblA values   (1,  1, '10/21/2020')
                         , (2, 1, '10/21/2020')
                         , (3, 1, '10/27/2020')
                         , (4, 2, '10/21/2020')
                         , (5, 2, '10/21/2020')
                         , (6, 1, '10/21/2020')
                         , (7, 2, '10/23/2020')
                         , (8, 2, '10/22/2020')
                         , (9, 1, '10/25/2020')
                         , (10, 3, '10/21/2020')
select Id
 , SubId
 , Createdate
from(
  select *
     , Row_Number() Over (Partition By SubId order by Createdate desc) as RNo
  from tblA
)temp where RNo <= 2 and SubId in (1, 2)
Order by SubId
您可以使用行数功能,如下所示:

Create table tblA  (ID int, SubId int, Createdate DateTime)

Insert Into tblA values   (1,  1, '10/21/2020')
                         , (2, 1, '10/21/2020')
                         , (3, 1, '10/27/2020')
                         , (4, 2, '10/21/2020')
                         , (5, 2, '10/21/2020')
                         , (6, 1, '10/21/2020')
                         , (7, 2, '10/23/2020')
                         , (8, 2, '10/22/2020')
                         , (9, 1, '10/25/2020')
                         , (10, 3, '10/21/2020')
select Id
 , SubId
 , Createdate
from(
  select *
     , Row_Number() Over (Partition By SubId order by Createdate desc) as RNo
  from tblA
)temp where RNo <= 2 and SubId in (1, 2)
Order by SubId