So I have a table structured similarly to this:
GoalID | GoalName | GoalType | UsedTimeframe | ChangedTimeframe | GoalUpperBound | GoalLowerBound | GoalValue | EffectiveDate | EndDate |
---|---|---|---|---|---|---|---|---|---|
1 | Sales | Single Target | Annually | Annually | NULL | NULL | 5,000,000 | 01-01-2021 | 12-31-2021 |
2 | Unique Purchasers | Range | Monthly | Monthly | 22,000 | 20,000 | NULL | 9-01-2021 | 9-30-2021 |
3 | Sales | Single Target | Monthly | Annually | NULL | NULL | 500,000 | 10-01-2021 | 10-31-2021 |
4 | Unique Purchasers | Range | Monthly | Monthly | 24,000 | 21,000 | NULL | 10-01-2021 | 10-31-2021 |
5 | New Customers | Single Target | Monthly | Annually | NULL | NULL | 5,000 | 01-01-2021 | 12-31-2021 |
6 | Products Sold | Range | Monthly | Monthly | 10,000 | 9,000 | NULL | 10-01-2021 | 10-31-2021 |
7 | Products Sold | Range | Monthly | Monthly | 12,000 | 10,000 | NULL | 10-15-2021 | 10-31-2021 |
8 | Sales | Single Target | Annually | Annually | NULL | NULL | 4,750,000 | 01-01-2020 | 12-31-2020 |
9 | Referrals | Single Target | Monthly | Annually | NULL | NULL | 1,000 | 01-01-2021 | 12-31-2021 |
What is the most optimal way to pull each unique GoalName and their corresponding GoalUpperBound/GoalLowerBound or GoalValue? So in this example, if I wanted to pull all goals that change Monthly, I'd want to see GoalIDs (3, 4, 5, 7, 9) and if I was pulling goals that change Annually, I'd see GoalID (1). I was thinking pulling the MAX(GoalID) for each DISTINCT GoalName and UsedTimeframe and then joining back the GoalID to this table or maybe the MAX(EffectiveDate)?