Solved

SQL - max date records with one field values belonging to a list of values

Posted on 2009-04-09
1
696 Views
Last Modified: 2012-08-13

Hi:
   I waanted to create a SQL that does the following things.

I have a Table1 with structure:
A B C D E
data in table T1 is:
-------
A B C D E
1 Buy c1  May1 2009  E1
1 Buy c2  May 3 2009 E2
2 Sell C1 Dec 1 2008   E3

I want my SQL to return this:
A B D
1 Buy May 3 2009
2 Sell Dev 1 2008
i.e.
B belongs in a list of values say Buy,Sell,Hold
D = max date record for A
A = key

How should my SQL look?
0
Comment
Question by:LuckyLucks
[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
1 Comment
 
LVL 18

Accepted Solution

by:
daveslash earned 500 total points
ID: 24108023

How about this?

-- DaveSlash


select t1.a,
       t1.b,
       t1.d
from   Table1 t1
where  t1.b in ('Buy','Sell','Hold')
  and  t1.d = (select max(t2.d)
               from   Table1 t2
               where  t2.a = t1.a)

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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