Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Inner join on a Select statement

Posted on 2006-11-03
4
409 Views
Last Modified: 2008-02-01
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 ,
      [ComputerSysvolVersionNumber] [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.DisplayName,
GroupPolicyObjects.CreationTime,
GroupPolicyObjects.ModificationTime,
GroupPolicyObjects.UserDSVersionNumber,
GroupPolicyObjects.UserSysvolVersionNumber,
GroupPolicyObjects.ComputerDSVersionNumber,
GroupPolicyObjects.ComputerSysvolVersionNumber,
GPODescriptions.gpoDescription,
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.
0
Comment
Question by:benhanson
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
mastoo earned 500 total points
ID: 17870433
SELECT GroupPolicyObjects.gpoID,
GroupPolicyObjects.DisplayName,
GroupPolicyObjects.CreationTime,
GroupPolicyObjects.ModificationTime,
GroupPolicyObjects.UserDSVersionNumber,
GroupPolicyObjects.UserSysvolVersionNumber,
GroupPolicyObjects.ComputerDSVersionNumber,
GroupPolicyObjects.ComputerSysvolVersionNumber,
GPODescriptions.gpoDescription,
GPODescriptions.EditByUser,
GPODescriptions.EditDate
FROM GroupPolicyObjects
LEFT OUTER JOIN ( ( Select ID,MAX(EditDate) as MaxDate
  from GPODescriptions
  Group BY ID ) As MyMax
Join GPODescriptions
  On MyMax.ID = GPODescriptions.ID
    And MyMax.MaxDate = GPODescriptions.EditDate)
ON GroupPolicyObjects.gpoID = GPODescriptions.gpoID
ORDER BY DisplayName ASC

0
 
LVL 12

Author Comment

by:benhanson
ID: 17870512
Funny that you mis-typed a few of the gpoID references, since those columns were previously just ID.  Otherwise, I think you've got it!  I'm going to test this in the web interface it is destined for, then accept your comment after testing.  Thanks for the quick answer, my brain was preztelized . . .  Final query(just changed ID to gpoID in a few spots:

 SELECT GroupPolicyObjects.gpoID,
GroupPolicyObjects.DisplayName,
GroupPolicyObjects.CreationTime,
GroupPolicyObjects.ModificationTime,
GroupPolicyObjects.UserDSVersionNumber,
GroupPolicyObjects.UserSysvolVersionNumber,
GroupPolicyObjects.ComputerDSVersionNumber,
GroupPolicyObjects.ComputerSysvolVersionNumber,
GPODescriptions.gpoDescription,
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
0
 
LVL 12

Author Comment

by:benhanson
ID: 17870817
That did the trick, thank you very much for your help!
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17870853
Glad to help.  Sorry for the typos.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question