Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL View with TOP Clause

Status
Not open for further replies.

ljwilson

Programmer
May 1, 2008
65
0
0
US
I need some help. Here is the situation, I need to only show the record with the highest activity level from the CHAOS_Awards table. In the CHAOS_Awards table, they can be several records for each award_fk. That table has a column for ActivityNumber which increments for each record of a specific award_fk. How can get the view to just return the one with the highest ActivityNumber?

Here is the current SQL for the view:
Code:
SELECT     ca.Mnemonic, sv.award_fk, pp.visit_fk, pp.AccountNumber, SUM(pp.quantity * p.UnitPrice) AS totalVisitCost, pc.amount AS pendingChangeTotal, 
                      sv.subject_fk, ISNULL(st.FirstName, '') + ' ' + ISNULL(st.MiddleInitial, '') + ' ' + ISNULL(st.LastName, '') AS participantName, sv.is_reconciled
FROM         dbo.Procedures_Performed AS pp INNER JOIN
                      dbo.ProceduresPermitted_tbl AS p ON p.ProcedureID = pp.procedure_fk INNER JOIN
                      dbo.Scheduled_Visits AS sv ON sv.id = pp.visit_fk INNER JOIN
                      dbo.Subjects_tbl AS st ON sv.subject_fk = st.SubjectID LEFT OUTER JOIN
                      CHAOS.dbo.CHAOS_Awards AS ca ON ca.AwardNumber = sv.award_fk AND ca.Mnemonic IS NOT NULL LEFT OUTER JOIN
                      dbo.Reconciliation_Pending_Changes AS pc ON pc.visit_fk = sv.id AND pc.completeDate IS NULL
WHERE     (sv.award_fk <> 1865)
GROUP BY ca.Mnemonic, sv.award_fk, pp.visit_fk, pp.AccountNumber, pc.amount, sv.subject_fk, ISNULL(st.FirstName, '') + ' ' + ISNULL(st.MiddleInitial, '') 
                      + ' ' + ISNULL(st.LastName, ''), sv.is_reconciled

Thanks in advance.
 
Well since you want the max for a specific column try the Max function and leave everyhting else the same

Example

Max(activitynumvber) as Highest,***Just replace the field***
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top