dbTalk Databases Forums  

How to query for MAX value, by CA_ID, while pulling field from athird table

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss How to query for MAX value, by CA_ID, while pulling field from athird table in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
broy@omegasoftwareinc.com
 
Posts: n/a

Default How to query for MAX value, by CA_ID, while pulling field from athird table - 08-13-2012 , 09:16 PM






Let's say that I have the following two tables:

Table1
-------
TABLE1_ID
CA_ID
FK_TABLE2_ID
TRX_DATE

Table2
-------
PK_TABLE2_ID
TRX_DESC

Table1 can have multiple records with the same CA_ID. In addition, for eachCA_ID, there could be multiple records, some with different values of FK_TABLE2_ID.

What I want is a query that pulls back CA_ID, TRX_DESC, & TRX_DATE. For each CA_ID in Table 1, I want to find the record that has the MAX value for TRX_DATE. The returned TRX_DATE will be that MAX value, for each CA_ID, and the TRX_DESC will be the TRX_DESC that corresponds to that record with the MAX value for TRX_DATE (i.e. foreign key ==> Table1.FK_TABLE2_ID = Table2.PK_TABLE2_ID).

How could I construct such a query?

Reply With Quote
  #2  
Old   
broy@omegasoftwareinc.com
 
Posts: n/a

Default Re: How to query for MAX value, by CA_ID, while pulling field fromathird table - 08-22-2012 , 05:26 PM






Actually, there could very well be multiple records, for the same CA_ID, with the same TRX_DATE. Any ideas in that case?

Reply With Quote
  #3  
Old   
rpresser
 
Posts: n/a

Default Re: How to query for MAX value, by CA_ID, while pulling field fromathird table - 08-22-2012 , 06:13 PM



On Wednesday, August 22, 2012 6:26:34 PM UTC-4, (unknown) wrote:
Quote:
Actually, there could very well be multiple records, for the same CA_ID, with the same TRX_DATE. Any ideas in that case?
You would need to decide which TRX_DESC you want returned if there is a match.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.