benhanson
asked on
Inner join on a Select statement
I have two tables that I am trying to join, but I need to perform a MAX function against the second table before they are joined.
CREATE TABLE [dbo].[GroupPolicyObjects] (
[gpoID] [varchar] (38) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,
[DisplayName] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,
[CreationTime] [datetime] NOT NULL ,
[ModificationTime] [datetime] NOT NULL ,
[Owner] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,
[IsUserEnabled] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,
[IsComputerEnabled] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,
[UserDSVersionNumber] [int] NOT NULL ,
[UserSysvolVersionNumber] [int] NOT NULL ,
[ComputerDSVersionNumber] [int] NOT NULL ,
[ComputerSysvolVersionNumb er] [int] NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[GPODescriptions] (
[pkAuto] [int] IDENTITY (1, 1) NOT NULL ,
[gpoID] [varchar] (38) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL ,
[gpoDescription] [varchar] (8000) COLLATE SQL_Latin1_General_CP1_CI_ AS NULL ,
[editDate] [datetime] NOT NULL ,
[editByUser] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_ AS NOT NULL
) ON [PRIMARY]
"SElect ID,MAX(EditDate) as Date from GPODescriptions Group BY ID" returns the correct set of rows from the second table, but I can't figure out how to join this with results from GroupPolicyObjects. GPODescriptions will contain history of descriptions, so there will be multiple records for any gpoID, I just want to return the most recent, based on editDate.
This query:
SELECT GroupPolicyObjects.gpoID,
GroupPolicyObjects.Display Name,
GroupPolicyObjects.Creatio nTime,
GroupPolicyObjects.Modific ationTime,
GroupPolicyObjects.UserDSV ersionNumb er,
GroupPolicyObjects.UserSys volVersion Number,
GroupPolicyObjects.Compute rDSVersion Number,
GroupPolicyObjects.Compute rSysvolVer sionNumber ,
GPODescriptions.gpoDescrip tion,
GPODescriptions.EditByUser ,
GPODescriptions.EditDate
FROM GroupPolicyObjects
LEFT OUTER JOIN GPODescriptions
ON GroupPolicyObjects.gpoID = GPODescriptions.gpoID
ORDER BY DisplayName ASC
Returns the right set of columns, with proper joining, but I get an entry for each description when I just want the latest.
CREATE TABLE [dbo].[GroupPolicyObjects]
[gpoID] [varchar] (38) COLLATE SQL_Latin1_General_CP1_CI_
[DisplayName] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_
[CreationTime] [datetime] NOT NULL ,
[ModificationTime] [datetime] NOT NULL ,
[Owner] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_
[IsUserEnabled] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_
[IsComputerEnabled] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_
[UserDSVersionNumber] [int] NOT NULL ,
[UserSysvolVersionNumber] [int] NOT NULL ,
[ComputerDSVersionNumber] [int] NOT NULL ,
[ComputerSysvolVersionNumb
) ON [PRIMARY]
CREATE TABLE [dbo].[GPODescriptions] (
[pkAuto] [int] IDENTITY (1, 1) NOT NULL ,
[gpoID] [varchar] (38) COLLATE SQL_Latin1_General_CP1_CI_
[gpoDescription] [varchar] (8000) COLLATE SQL_Latin1_General_CP1_CI_
[editDate] [datetime] NOT NULL ,
[editByUser] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_
) ON [PRIMARY]
"SElect ID,MAX(EditDate) as Date from GPODescriptions Group BY ID" returns the correct set of rows from the second table, but I can't figure out how to join this with results from GroupPolicyObjects. GPODescriptions will contain history of descriptions, so there will be multiple records for any gpoID, I just want to return the most recent, based on editDate.
This query:
SELECT GroupPolicyObjects.gpoID,
GroupPolicyObjects.Display
GroupPolicyObjects.Creatio
GroupPolicyObjects.Modific
GroupPolicyObjects.UserDSV
GroupPolicyObjects.UserSys
GroupPolicyObjects.Compute
GroupPolicyObjects.Compute
GPODescriptions.gpoDescrip
GPODescriptions.EditByUser
GPODescriptions.EditDate
FROM GroupPolicyObjects
LEFT OUTER JOIN GPODescriptions
ON GroupPolicyObjects.gpoID = GPODescriptions.gpoID
ORDER BY DisplayName ASC
Returns the right set of columns, with proper joining, but I get an entry for each description when I just want the latest.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did the trick, thank you very much for your help!
Glad to help. Sorry for the typos.
ASKER
SELECT GroupPolicyObjects.gpoID,
GroupPolicyObjects.Display
GroupPolicyObjects.Creatio
GroupPolicyObjects.Modific
GroupPolicyObjects.UserDSV
GroupPolicyObjects.UserSys
GroupPolicyObjects.Compute
GroupPolicyObjects.Compute
GPODescriptions.gpoDescrip
GPODescriptions.EditByUser
GPODescriptions.EditDate
FROM GroupPolicyObjects
LEFT OUTER JOIN ( ( Select gpoID,MAX(EditDate) as MaxDate
from GPODescriptions
Group BY gpoID ) As MyMax
Join GPODescriptions
On MyMax.gpoID = GPODescriptions.gpoID
And MyMax.MaxDate = GPODescriptions.EditDate)
ON GroupPolicyObjects.gpoID = GPODescriptions.gpoID
ORDER BY DisplayName ASC