Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL Most Recent Date

Posted on 2009-07-14
5
Medium Priority
?
298 Views
Last Modified: 2012-05-07
Hi, I would like to select a number of tables based on the most recent date and where it is inactive.  The current problem is that if the dates are equal multiple tables come up (I just want one; the first one that it finds).  I tried using DISTINCT, but it didn't seem to work.


Here's how it's set up:
Table1:  Number
Table2:  id, Table1$Number, Table2$id
Table3:  id, IsActive

Table1 has many Table2.
Table2 has one Table1.
Table2 has one Table3.
Table3 has many Table2.


Here's what I have:
Table1.Number - Table2.Date - Table3.IsActive
123 - 7/17/2009 - Y
123 - 7/13/2009 - N
123 - 7/14/2009 - N
475 - 7/14/2009 - Y
475 - 7/14/2009 - Y
475 - 7/14/2009 - N
475 - 7/14/2009 - N


Here's what I want to output:
Table1.Number - Table2.Date - Table3.IsActive
123 - 7/14/2009 - N
475 - 7/14/2009 - N

Here's what I get:
Table1.Number - Table2.Date - Table3.IsActive
123 - 7/14/2009 - N
475 - 7/14/2009 - N     ' I'd like to have just one
475 - 7/14/2009 - N


Also if I can do the query below a better way, please show how.  I want to use MAX though.
I'm using Microsoft SQL Server 2005.
Thanks.
SELECT  Table1.Name
        CONVERT(varchar,Table2.Date,101) as Date
FROM    Table1                                          LEFT OUTER JOIN
        Table2 ON Table1.Name      = Table2.Table1$Name LEFT OUTER JOIN
        Table3 ON Table2.Table3$id = Table3.id
WHERE   Table2.Date IN (
          SELECT MAX(Date)
          FROM   Table2 x
          WHERE  Table1.name     = x.Table1$name
             AND x.Table3$id     = Table3.id
             AND Table3.IsActive = 'N'
        )

Open in new window

0
Comment
Question by:no1leftypitcher
[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
  • 4
5 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24855112
Since you are specifically looking for IsActive = 'N', you can do this since the other values can be grouped on.
SELECT t1.Number, MAX(t2.Date) AS Date, t3.IsActive
FROM Table1 t1
INNER JOIN (
	Table2 t2 INNER JOIN Table3 t3
		ON t3.id = t2.Table3$id
) ON t2.Table1$Number = t1.Number
WHERE t3.IsActive = 'N'
GROUP BY t1.Number, t3.IsActive

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24855127
If there is more to it than that, you can use an analytical statement with OVER keyword and ROW_NUMBER function to get the results you need given I have the joins correct.
SELECT t1.Number
, CONVERT(varchar, t2.Date, 101) AS Date
, t3.IsActive
FROM (
	SELECT t1.Number, t2.Date, t3.IsActive
	, ROW_NUMBER() OVER (PARTITION BY t1.Number ORDER BY t2.Date DESC) AS row
	FROM Table1 t1
	INNER JOIN (
		Table2 t2 INNER JOIN Table3 t3
			ON t3.id = t2.Table3$id
	) ON t2.Table1$Number = t1.Number
	WHERE t3.IsActive = 'N'
) t
WHERE t.row = 1;

Open in new window

0
 

Author Comment

by:no1leftypitcher
ID: 24858318
Thanks for the comments, but when I ran the query, it said that OVER is not recognized/supported and that ROW_NUMBER() is not a valid function.  I guess my version of MS SQL is too old for these; is there another way?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24858735
These should work in MS SQL Server 2005, but in either case I provided this method http:#24855112, does it not work?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 24858759
For MS SQL Server 2000, you can simulate ROW_NUMBER() like this:
SELECT t1.Number
, CONVERT(varchar, t2.Date, 101) AS Date
, t3.IsActive
FROM (
        SELECT t1.Number, t2.Date, t3.IsActive
        , (SELECT COUNT(*) FROM Table1 INNER JOIN Table2 ON Table1$Number = Number WHERE Number = t1.Number AND Date >= t2.Date) AS row
        FROM Table1 t1
        INNER JOIN (
                Table2 t2 INNER JOIN Table3 t3
                        ON t3.id = t2.Table3$id
        ) ON t2.Table1$Number = t1.Number
        WHERE t3.IsActive = 'N'
) t
WHERE t.row = 1;

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

715 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