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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't seem to create this stored procedure

Status
Not open for further replies.

Goalie3533

Programmer
Apr 14, 2004
53
US
Hi guys. I apologize if this sounds a little confusing but unfortunately, I'm not a database expert and I really need some help on this issue.

I need to retrieve about a dozen fields from various tables for a report that prints out our clients service plans however I'm not sure how to go about successfully grabbing this particular data.

I need to grab items such as the client name, client phone #, email, the name of the clients service plan, and things of that nature. The problem is the final item I need to grab which is the date the service plan ends.

The situation is there are a few clients of ours who for whatever reason, have service plans from last year who do not yet have new plans implemented for this year yet. Now, just because they don't have a plan for this year doesn't mean they're no longer clients of ours so I need to have these guys show up on the report as well. So these guys have service plans that ended in 2003.

Now here's the tricky part, all service plans for all clients for every year we've been in business is on the database. So my problem is I can't select all service plans or I'll get all of these old ones as well but I also can't select only the service plans that are for this year because as I mentioned above, some may have ended in 2003.

So the way the report needs to go is I need to select all of the fields I mentioned above and when it comes to selecting the service plan end date, I need to select the LATEST one for that particular client.

Any idea on how to go about doing this?

Thanks in advance.

-Goalie35
 
Use MAX(ServicePlanDate). If you GROUP BY the ClientID, this will give you the ClientID's MAX (latest) ServicePlanDate.

Check out MAX in the BOL.

-SQLBill

BOL=Books OnLine=Microsoft SQL Server's HELP
Installed as part of the Client Tools
Found at Start>Programs>Microsoft SQL Server>Books OnLine

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top