×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Query

Query

(OP)
Hi,

I have the following result from a select statement:

Column: A  B  C
Data  : 55 S  123
Data  : 55 T  123
Data  : 09 S  123
Data  : 09 T  123

I am new to SQL and I cannot figure out a way to do the following: "If there are rows with same values in Column A, select the rows with 'T' in column B."

Your help is greatly appreciated. Thanks.   

RE: Query

(OP)
Thanks for your reply. My example is not a table, it is a result of the select statement. Probably I should create a view and try again.

Column: A  B  C
Data  : 55 S  123
Data  : 55 T  123
Data  : 55 Z  123
Data  : 09 S  123
Data  : 09 T  123
Data  : 09 S  123

Just for clarification: I want to do is "if there are rows with same Column A value, select the last row."
 

RE: Query

(OP)
Sorry for the confusion.

Column: A  B  C
Data  : 55 S  123
Data  : 55 T  123
Data  : 55 Z  123
Data  : 09 S  123
Data  : 09 T  123
Data  : 09 S  123

I sorted the result so what I want is "55 Z  123" and "09 S  123" out of the above table. I have created a view so the original statement does not matter I guess. Thanks a lot for your help.

RE: Query

(OP)
"55 Z  123" is the last row with 55 in Column A, then "09 S  123" is the last row with 09 in Column A. They are sorted by Column A and then Process_Time(let's say it's in Column D). Thanks.
 

RE: Query

ah, the old familiar "hidden column D i forgot to mention" trick winky smile

CODE

SELECT q.A
     , q.B
     , q.C
  FROM ( SELECT A
              , MAX(D) AS latest
           FROM queryresult
         GROUP
             BY A ) AS m
INNER
  JOIN queryresult AS q
    ON q.A = m.A
   AND q.D = m.latest
please let me know if that works for you
 

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: Query

(OP)
Thanks PHV and r937.  

RE: Query

(OP)
r937, thanks. Your code works.  

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close