dbTalk Databases Forums  

Agreggate and Scalar Combination

comp.databases comp.databases


Discuss Agreggate and Scalar Combination in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Anthony Antognoli
 
Posts: n/a

Default Agreggate and Scalar Combination - 07-19-2004 , 02:15 PM






I have a table that tracks projects and funding sources. The columns
are as follows:
1. UPN (Universal Project Number) (PK) (FK-Constraint to Projects)
2. Funding Source Type (PK) (FK-Constraint to FundSourceTypes)
3. Stage Type (PK) (FK_Constraint to Stages)
4. Amount
5. Date

Each project may have multiple funding source types, and each funding
source type has 4 stages that they go through (thus, the composite
primary key). History is tracked, so the stage type is not updated, a
new row is added. I need to return the amount for the most recent
stage of each funding source for each UPN. The stages are in order,
so the MAX function should work.

select UPN, TC_SourceID, MAX(TC_StageID)
FROM TC_Versions
GROUP BY UPN, TC_SourceID

Returns the most recent stage for each source of each UPN. All fine
and dandy. However, I also need to return the value in the amount
column of each row returned by the above query. Adding amount to the
query doesnt work, because it has to either be part of an aggregate or
in the GROUP BY to be in the select list. It is possible for the
amount to change at each stage so grouping by amount doesnt work. Any
ideas? Thank you in advance.

Reply With Quote
  #2  
Old   
Adam Machanic
 
Posts: n/a

Default Re: Agreggate and Scalar Combination - 07-19-2004 , 06:47 PM






Try this:

select UPN, TC_SourceID, TC_StageID, Amount
FROM TC_Versions AS TC1
WHERE TC_StageID = (SELECT MAX(TC_StageID)
FROM TC_Versions AS TC2
WHERE TC1.UPN = TC2.UPN
AND TC1.TC_SourceID =
TC2.TC_SourceID);


"Anthony Antognoli" <anthony (AT) poeticcode (DOT) net> wrote

Quote:
I have a table that tracks projects and funding sources. The columns
are as follows:
1. UPN (Universal Project Number) (PK) (FK-Constraint to Projects)
2. Funding Source Type (PK) (FK-Constraint to FundSourceTypes)
3. Stage Type (PK) (FK_Constraint to Stages)
4. Amount
5. Date

Each project may have multiple funding source types, and each funding
source type has 4 stages that they go through (thus, the composite
primary key). History is tracked, so the stage type is not updated, a
new row is added. I need to return the amount for the most recent
stage of each funding source for each UPN. The stages are in order,
so the MAX function should work.

select UPN, TC_SourceID, MAX(TC_StageID)
FROM TC_Versions
GROUP BY UPN, TC_SourceID

Returns the most recent stage for each source of each UPN. All fine
and dandy. However, I also need to return the value in the amount
column of each row returned by the above query. Adding amount to the
query doesnt work, because it has to either be part of an aggregate or
in the GROUP BY to be in the select list. It is possible for the
amount to change at each stage so grouping by amount doesnt work. Any
ideas? Thank you in advance.



Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Agreggate and Scalar Combination - 07-19-2004 , 11:29 PM



anthony (AT) poeticcode (DOT) net (Anthony Antognoli) wrote in message news:<ef55064b.0407191115.6b8f2740 (AT) posting (DOT) google.com>...
Quote:
I have a table that tracks projects and funding sources. The columns
are as follows:
1. UPN (Universal Project Number) (PK) (FK-Constraint to Projects)
2. Funding Source Type (PK) (FK-Constraint to FundSourceTypes)
3. Stage Type (PK) (FK_Constraint to Stages)
4. Amount
5. Date

Each project may have multiple funding source types, and each funding
source type has 4 stages that they go through (thus, the composite
primary key). History is tracked, so the stage type is not updated, a
new row is added. I need to return the amount for the most recent
stage of each funding source for each UPN. The stages are in order,
so the MAX function should work.

select UPN, TC_SourceID, MAX(TC_StageID)
FROM TC_Versions
GROUP BY UPN, TC_SourceID

Returns the most recent stage for each source of each UPN. All fine
and dandy. However, I also need to return the value in the amount
column of each row returned by the above query. Adding amount to the
query doesnt work, because it has to either be part of an aggregate or
in the GROUP BY to be in the select list. It is possible for the
amount to change at each stage so grouping by amount doesnt work. Any
ideas? Thank you in advance.
Something like:

select UPN, TC_SourceID, amount
FROM TC_Versions V
WHERE TC_StageID = (
SELECT MAX(TC_StageID) from TC_Versions
WHERE UPN = V.UPN
AND TC_SourceID = V.TC_SourceID
)

HTH
/Lennart


Reply With Quote
  #4  
Old   
Anthony Antognoli
 
Posts: n/a

Default Re: Agreggate and Scalar Combination - 07-20-2004 , 09:14 AM



That is indeed it. Thank you both very much. This is a correlated
subquery, correct?



lennart (AT) kommunicera (DOT) umea.se (Lennart Jonsson) wrote in message news:<6dae7e65.0407192029.7045a88e (AT) posting (DOT) google.com>...
Quote:
anthony (AT) poeticcode (DOT) net (Anthony Antognoli) wrote in message news:<ef55064b.0407191115.6b8f2740 (AT) posting (DOT) google.com>...
I have a table that tracks projects and funding sources. The columns
are as follows:
1. UPN (Universal Project Number) (PK) (FK-Constraint to Projects)
2. Funding Source Type (PK) (FK-Constraint to FundSourceTypes)
3. Stage Type (PK) (FK_Constraint to Stages)
4. Amount
5. Date

Each project may have multiple funding source types, and each funding
source type has 4 stages that they go through (thus, the composite
primary key). History is tracked, so the stage type is not updated, a
new row is added. I need to return the amount for the most recent
stage of each funding source for each UPN. The stages are in order,
so the MAX function should work.

select UPN, TC_SourceID, MAX(TC_StageID)
FROM TC_Versions
GROUP BY UPN, TC_SourceID

Returns the most recent stage for each source of each UPN. All fine
and dandy. However, I also need to return the value in the amount
column of each row returned by the above query. Adding amount to the
query doesnt work, because it has to either be part of an aggregate or
in the GROUP BY to be in the select list. It is possible for the
amount to change at each stage so grouping by amount doesnt work. Any
ideas? Thank you in advance.

Something like:

select UPN, TC_SourceID, amount
FROM TC_Versions V
WHERE TC_StageID = (
SELECT MAX(TC_StageID) from TC_Versions
WHERE UPN = V.UPN
AND TC_SourceID = V.TC_SourceID
)

HTH
/Lennart

Reply With Quote
  #5  
Old   
Adam Machanic
 
Posts: n/a

Default Re: Agreggate and Scalar Combination - 07-20-2004 , 08:56 PM




"Anthony Antognoli" <anthony (AT) poeticcode (DOT) net> wrote

Quote:
That is indeed it. Thank you both very much. This is a correlated
subquery, correct?
Yes.




Reply With Quote
  #6  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Agreggate and Scalar Combination - 07-21-2004 , 01:36 AM



anthony (AT) poeticcode (DOT) net (Anthony Antognoli) wrote in message news:<ef55064b.0407200614.9f9745d (AT) posting (DOT) google.com>...
Quote:
That is indeed it. Thank you both very much. This is a correlated
subquery, correct?

Yes.

/L

[...]


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 - 2012, Jelsoft Enterprises Ltd.