0
I have two tables (Opportunity and Stage). I need to pick for each Opportunity the table row Stage where the StageTypeId is the same as input parameter.
Table Opportunity
Id, etc
Table Stage
Id, Createdon, Opportunityid, Stagetypeid.
Suppose I have the opportunities "opportunity1" and "opportunity2" each with several Stages registered.
Passing the StageTypeId I need to pick up the Opportunities who have this Id as the most recent in the table Stage.
I’m trying to query below but is replicating the Stage that exists for a Opportunity for all others Opportunities.
Looks like you’re ignoring that line: AND {Stage}.[OpportunityId] = ID
SELECT {Opportunity}.[Id] ID,
{Opportunity}.[Name],
{Opportunity}.[PotentialAmount],
{Contact}.[FirstName],
{Contact}.[LastName],
(SELECT * FROM
(
SELECT {Stage}.[StageTypeId]
FROM {Stage}
WHERE {Stage}.[StageTypeId] = @StageTypeId
AND {Stage}.[OpportunityId] = ID
ORDER BY {Stage}.[CreatedOn] DESC
)
WHERE ROWNUM = 1) AS StageTypeId
FROM {Opportunity}
LEFT JOIN {Contact}
ON {Opportunity}.[ContactId] = {Contact}.[Id]
Thank you.
one
maxmaybe help here, but honestly it’s hard to imagine the tables, it would be more practical to post the structure (relevant fields) and an example of data to be clear– Ricardo Pontual
@Denis, you’ve solved your problem?
– João Martins
@Joãomartins yes!
– Denis