[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL help

Posted on 2011-09-30
4
Medium Priority
?
185 Views
Last Modified: 2012-05-12
I am having problems with my query. What I need to do is to display additional columns when using MAX and subquery. I need to be able to only get the records that have the latest revision. Right now I can get this by using only doc id and max(rev) but I need other columns to tell me what project this belongs to, etc.

I need to add these columns dh.DhCreateDate, a.AdLabel, a.AdValue, p.PjName but I don't know how to do that without getting all the max values

With those additional fields I get:
DhDocID      Latest_Rev      DhCreateDate      AdLabel      AdValue      PjName
ED - 17928      1      2011-07-08 09:19:35.793      Job      519-15251      NULL
ED - 17928      2      2011-08-02 13:22:58.963      Job      519-15251      NULL
ED - 17928      3      2011-08-12 08:47:00.520      Job      519-15251      519-15251


But I need to only grab the latest  revision which in this case is 3.


    max2.sql
select  dh.DhDocID,  MAX( convert(varchar(10),dhrev)) as Latest_Rev, dh.DhCreateDate, a.AdLabel, a.AdValue, p.PjName

from Doc_Header dh INNER JOIN Attribute_Detail AS a ON dh.DhID = a.AdDhID LEFT OUTER JOIN
                      Project_Document_Relations AS pr ON pr.PdDhID = dh.DhID LEFT OUTER JOIN
                      Projects AS p ON p.PjID = pr.PdPjID 
where 
 (a.AdLabel LIKE '%job%')
 AND (dh.DhCreateDate > CONVERT(DATETIME, '2010-10-20 00:00:00', 102)) and dhdocid = 'ED - 17928'


group by DhDocID,dh.DhCreateDate, a.AdLabel, a.AdValue, p.PjName

Open in new window

0
Comment
Question by:NuclearLogistics
[X]
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
  • 3
4 Comments
 
LVL 8

Expert Comment

by:Crashman
ID: 36894102
try this
SELECT * FROM (
select  dh.DhDocID,
		dhrev as Latest_Rev,
        dh.DhCreateDate,
        a.AdLabel,
        a.AdValue,
        p.PjName
        RANK() OVER (PARTITION BY dh.DhDocID,dhrev as Latest_Rev, dh.DhCreateDate, a.AdLabel, a.AdValue,p.PjName ORDER BY dhrev DESC) AS RNK 
from    Doc_Header dh
        INNER JOIN Attribute_Detail AS a ON dh.DhID = a.AdDhID
        LEFT OUTER JOIN Project_Document_Relations AS pr ON pr.PdDhID = dh.DhID
        LEFT OUTER JOIN Projects AS p ON p.PjID = pr.PdPjID
where   ( a.AdLabel LIKE '%job%' )
        AND ( dh.DhCreateDate > CONVERT(DATETIME, '2010-10-20 00:00:00', 102) )
        and dhdocid = 'ED - 17928'
group by DhDocID,
        dh.DhCreateDate,
        a.AdLabel,
        a.AdValue,
        p.PjName) derivate WHERE rnk = 1

Open in new window

0
 
LVL 8

Expert Comment

by:Crashman
ID: 36894106
Ups, Sorry, without group

SELECT * FROM (
select  dh.DhDocID,
		dhrev as Latest_Rev,
        dh.DhCreateDate,
        a.AdLabel,
        a.AdValue,
        p.PjName
        RANK() OVER (PARTITION BY dh.DhDocID,dhrev as Latest_Rev, dh.DhCreateDate, a.AdLabel, a.AdValue,p.PjName ORDER BY dhrev DESC) AS RNK 
from    Doc_Header dh
        INNER JOIN Attribute_Detail AS a ON dh.DhID = a.AdDhID
        LEFT OUTER JOIN Project_Document_Relations AS pr ON pr.PdDhID = dh.DhID
        LEFT OUTER JOIN Projects AS p ON p.PjID = pr.PdPjID
where   ( a.AdLabel LIKE '%job%' )
        AND ( dh.DhCreateDate > CONVERT(DATETIME, '2010-10-20 00:00:00', 102) )
        and dhdocid = 'ED - 17928'
) derivate WHERE rnk = 1

Open in new window

0
 

Author Comment

by:NuclearLogistics
ID: 36894279
this shows all three records

DhDocID      Latest_Rev      DhCreateDate      AdLabel      AdValue      PjName      RNK
ED - 17928      1      2011-07-08 09:19:35.793      Job      519-15251      NULL      1
ED - 17928      2      2011-08-02 13:22:58.963      Job      519-15251      NULL      1
ED - 17928      3      2011-08-12 08:47:00.520      Job      519-15251       519-15251      1
0
 
LVL 8

Accepted Solution

by:
Crashman earned 2000 total points
ID: 36894291
change for this

RANK() OVER (PARTITION BY dh.DhDocID ORDER BY dhrev DESC) AS RNK 

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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