I have a table that has employee addresses and the date each address came into effect. I only want to select the address associated with the most recent date, since that by definition is the current address. My EmployeeAddress table looks like:EmployeeID|Address------------------|City---------|State|ChangeDate1000------|2121 Fifth Ave-----------|New York-----|NY---|2012-08-20 1000------|80 Main Plaza------------|San Francisco|CA---|2012-05-01 1000------|3050 Rose Avenue---------|Tulsa--------|OK---|2012-01-14 2000------|111 Jiffy Lane-----------|Chicago------|IL---|2011-07-31 3000------|555 Tulip RD-------------|Indianapolis-|IN---|2011-09-19 3000------|3333 Peach Tree Terrace--|Atlanta------|GA---|2011-03-17
I want the SELECT to show this:EmployeeID|Address------------------|City---------|State|ChangeDate1000------|2121 Fifth Ave-----------|New York-----|NY---|2012-08-20 2000------|111 Jiffy Lane-----------|Chicago------|IL---|2011-07-31 3000------|555 Tulip RD-------------|Indianapolis-|IN---|2011-09-19
The query to get the first result is:SELECT [EmployeeID] ,[Address] ,[City] ,[State] ,[ChangeDate] FROM [Northwind].[dbo].[EmployeeAddress] ORDER BY EmployeeID, ChangeDate DESCThe script to create the table is:USE [Northwind]GO/****** Object: Table [dbo].[EmployeeAddress] Script Date: 06/12/2012 09:52:35 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[EmployeeAddress]( [EmployeeID] [nvarchar](10) NULL, [Address] [nvarchar](30) NULL, [City] [nvarchar](20) NULL, [State] [nvarchar](2) NULL, [ChangeDate] [datetime] NULL) ON [PRIMARY]GOINSERT [dbo].[EmployeeAddress] ([EmployeeID], [Address], [City], [State], [ChangeDate]) VALUES (N'1000', N'3050 Rose Avenue', N'Tulsa', N'OK', CAST(0x00009FD800000000 AS DateTime))INSERT [dbo].[EmployeeAddress] ([EmployeeID], [Address], [City], [State], [ChangeDate]) VALUES (N'1000', N'2121 Fifth Ave', N'New York', N'NY', CAST(0x0000A0B300000000 AS DateTime))INSERT [dbo].[EmployeeAddress] ([EmployeeID], [Address], [City], [State], [ChangeDate]) VALUES (N'1000', N'80 Main Plaza', N'San Francisco', N'CA', CAST(0x0000A04400000000 AS DateTime))INSERT [dbo].[EmployeeAddress] ([EmployeeID], [Address], [City], [State], [ChangeDate]) VALUES (N'2000', N'111 Jiffy Lane', N'Chicago', N'IL', CAST(0x00009F3100000000 AS DateTime))INSERT [dbo].[EmployeeAddress] ([EmployeeID], [Address], [City], [State], [ChangeDate]) VALUES (N'3000', N'3333 Peach Tree Terrace', N'Atlanta', N'GA', CAST(0x00009EA900000000 AS DateTime))INSERT [dbo].[EmployeeAddress] ([EmployeeID], [Address], [City], [State], [ChangeDate]) VALUES (N'3000', N'555 Tulip RD', N'Indianapolis', N'IN', CAST(0x00009F6300000000 AS DateTime))