?
Solved

Retrieve last two versions of same id in T-SQL

Posted on 2007-10-02
6
Medium Priority
?
259 Views
Last Modified: 2011-10-03
Am sure there's an easy way around this:

Important thing to note :-  Although I'm building the SQL in an dedicated analyzer the report itself is going to be run in EXCEL through Microsoft Query.  I only have READ-ONLY access to the database, thanks to my super helpful IT department,

I've got a table that contains ALL my controls including previous versions of that control.  The relevant columns are:

ControlNumber - unique identifier for that control in the system
ControlVersion
ControlState - Can be Retired, Edit or Released.  The controlversion increments itself when it's in Edit State.
ReleaseDate - The unique date & time it was released.

There can be many instances of the same ControlNumber in the system, each with it's own version number, State, and unique ReleaseDate.  !!However!! Version number is NOT Unique to the ControlNumber.

For example :
ControlNumber  Controlversion  ControlState  ReleaseDate
000012      Released      1      2006-12-22 11:22:02.387
000012      Retired      2      2007-01-03 09:28:48.257
000025      Released      2      2007-01-03 10:48:06.697
000025      Retired      3      2007-01-03 10:48:15.710
000027      Released      1      2006-12-06 13:27:35.117
000027      Released      2      2007-02-23 16:45:05.083
000027      Released      3      
000027      Retired      3      2007-09-27 10:02:33.513
000028      Released      1      2007-02-23 16:45:05.083
000028      Released      2      
000028      Retired      2      2007-09-27 10:02:33.513
000029      Released      1      2007-02-23 16:45:05.083
000029      Released      2      2007-09-27  09:12:43.513
000029      Edit      3      2007-09-27 10:02:33.513

Where the state is EDIT, I want to bring back a report that tells me the current version (in edit state) and the last version, so:

000029      Released      2      2007-09-27  09:12:43.513
000029      Edit      3      2007-09-27 10:02:33.513

Because controlversion is not unique when I run a report on the most current control I use the following:

Select  a.ControlNumber, a.ControlVersion, a.ReleaseDate, a.ControlState
from controls a,
 (select  max(releasedate) as release , ControlNumber   from controls  group by ControlNumber) b
where  a.releasedate = b.release and  a.ControlNumber = b.ControlNumber

However, I can't figure out how to modify it to give me the last two releasedates.  I've tried subqueries and nested queries.  'Having' doesn't seem to work and I can't use a max in the Where Clause.  

HEEEELP!!!!


0
Comment
Question by:ITmonkeys
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Yveau
ID: 20001565
try this:

Select  a.ControlNumber, a.ControlVersion, a.ReleaseDate, a.ControlState
from controls a,
 (select  top 2 releasedate as release , ControlNumber   from controls  group by ControlNumber order by releasedate) b
where  a.releasedate = b.release and  a.ControlNumber = b.ControlNumber

hope it helps ...
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20001631
actually, it should be

Select  a.ControlNumber, a.ControlVersion, a.ReleaseDate, a.ControlState
from controls a,
 (select  top 2 releasedate as release , ControlNumber   from controls  group by releasedate, ControlNumber order by releasedate) b
where  a.releasedate = b.release and  a.ControlNumber = b.ControlNumber

hope this helps ...
0
 

Author Comment

by:ITmonkeys
ID: 20005045
Hi,
Sorry I worded that wrong, what I really want is a script that does the following :

While State = 'Edit'
For each Controlnumber
If count (controlnumber) => 2 Then
Select ControlNumber, max(ControlVersion), ReleaseDate, State
Select ControlNumber, max(ControlVersion)-1, ReleaseDate, State
Else
Select ControlNumber, ControlVersion, ReleaseDate, State

Thanks

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
Yveau earned 2000 total points
ID: 20010186
... I have to admit, it is a bit lame ... But the good part is that it does the job:

Select  a.ControlNumber
,       a.ControlVersion
,       a.ReleaseDate
,       a.ControlState
from    controls a
inner   join (select max(releasedate) as release , ControlNumber from controls where ControlVersion <> 'Edit' group by ControlNumber) b
on      a.releasedate = b.release and  a.ControlNumber = b.ControlNumber
union
Select  a.ControlNumber
,       a.ControlVersion
,       a.ReleaseDate
,       a.ControlState
from    controls a
inner   join (select max(releasedate) as release , ControlNumber from controls where ControlVersion = 'Edit' group by ControlNumber) b
on      a.releasedate = b.release and  a.ControlNumber = b.ControlNumber


Hope this helps ...

0
 

Author Comment

by:ITmonkeys
ID: 20094786
Bit of modification but it works well, thanks a lot!!
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20095767
Glad I could be of any help and thanks for the grade !
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

864 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