加入两个表时如何获取表二的Max ID列

时间:2017-07-03 10:01:47

标签: sql sql-server left-join

我想要具有最大ID的行。我需要id为2的行。

我的表格
请使用以下脚本创建表格。

 /****** Object:  Table [dbo].[Customers]    Script Date: 03/07/2017 14:48:14 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Customers](
        [CustomerID] [bigint] IDENTITY(1,1) NOT NULL,
        [Name] [nchar](10) NULL,
     CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
    (
        [CustomerID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO
    /****** Object:  Table [dbo].[Instalments]    Script Date: 03/07/2017 14:48:14 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Instalments](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [Amount] [int] NULL,
        [Date] [date] NULL,
        [CustomerID] [bigint] NULL,
     CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO
    SET IDENTITY_INSERT [dbo].[Customers] ON 

    INSERT [dbo].[Customers] ([CustomerID], [Name]) VALUES (1, N'Muddassir ')
    INSERT [dbo].[Customers] ([CustomerID], [Name]) VALUES (2, N'Irshad    ')
    SET IDENTITY_INSERT [dbo].[Customers] OFF
    SET IDENTITY_INSERT [dbo].[Instalments] ON 

    INSERT [dbo].[Instalments] ([id], [Amount], [Date], [CustomerID]) VALUES (1, 500, CAST(N'2014-09-30' AS Date), 1)
    INSERT [dbo].[Instalments] ([id], [Amount], [Date], [CustomerID]) VALUES (2, 600, CAST(N'2015-09-30' AS Date), 1)
    INSERT [dbo].[Instalments] ([id], [Amount], [Date], [CustomerID]) VALUES (3, 800, CAST(N'2014-09-30' AS Date), 2)
    SET IDENTITY_INSERT [dbo].[Instalments] OFF

我的查询

这是我正在使用的查询。

SELECT a.CustomerID,a.[Name],b.Amount,b.Date,b.id from Customers as a

left join Instalments as b On b.CustomerID=a.CustomerID

我的结果 这是我查询的结果。

CustomerID  Name        Amount  Date        id
1           Muddassir   500     2014-09-30  1
1           Muddassir   600     2015-09-30  2
2           Irshad      800     2014-09-30  3

我想要什么结果? 但我想要低于结果。我想要所有只有最大ID的行

CustomerID  Name        Amount  Date        id
1           Muddassir   600     2015-09-30  2
2           Irshad      800     2014-09-30  3

3 个答案:

答案 0 :(得分:3)

试试这个

SELECT CustomerID
    ,[Name]
    ,Amount
    ,[Date]
    ,id
FROM (
    SELECT a.CustomerID
        ,a.[Name]
        ,b.Amount
        ,b.DATE
        ,b.id
        ,Row_NUmber() OVER (
            PARTITION BY a.CustomerID ORDER BY b.DATE DESC
            ) AS Rno
    FROM Customers AS a
    LEFT JOIN Instalments AS b ON b.CustomerID = a.CustomerID
    ) DT
WHERE DT.Rno = 1

结果

CustomerID  Name    Amount  Date    id
1   Muddassir   600 2015-09-30  2
2   Irshad      800 2014-09-30  3

答案 1 :(得分:1)

您可以使用MAX()获取子查询中的最高ID,并将其与当前ID Instalments进行比较。 试试这个:

SELECT a.CustomerID,a.[Name],b.Amount,b.Date,b.id FROM Customers AS a
LEFT JOIN Instalments AS b ON b.CustomerID=a.CustomerID
WHERE b.id = (SELECT MAX(id) FROM Instalments AS i WHERE i.CustomerID = a.CustomerID);

答案 2 :(得分:1)

您确定不需要始终为每位客户提供最新的分期付款吗?

这将是:

    SELECT a.CustomerID
          ,a.Name
          ,b.Amount
          ,b.Date
          ,b.id 
      FROM Customers AS a
 LEFT JOIN Instalments AS b 
        ON b.CustomerID=a.CustomerID
       AND b.Date = (SELECT MAX(Date) 
                       FROM Instalments T 
                      WHERE T.CustomerID = b.CustomerID 
                    )