473,569 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return most recent membership based on year stored in joined table


Hello-

I'm fairly new to writing SQL statements and would greatly appreciate
some help on this one.

I'm working on a project for a non-profit that I volunteer for. Part
of the database tracks membership using tables like this:

PersonInfo
-------------------
PersonID (primary key)
FirstName
LastName
etc..
PeopleMembershi ps
-------------------
PPLMembershipIP (primary key)
PersonID
MembershipTypeI D
FeePaid

MembershipTypes
--------------------
MembershipTypeI D (primary key)
MembershipYear
StandardFee
MembershipDescr iption (varchar)
Just because a person is in PersonInfo, doesn't mean they have anything
in PeopleMembershi ps (they can be in the databse for other reasons and
not have or have ever had a membership).

Membership fees vary by year and type of membership and they want to
retain a history of a person's memberships.

What I'm looking to do here is write a query (a view in SQL Server)
that will return the following Info

PersonID, MostRecentMembe rshipYear, FeePaidForThatM embership,
DescriptionOfTh atMembership

I'm thinking that I'd use max(MembershipY ear), but that requires group
by for the other columns, so I'm getting all of the people's
memberships returned.

I'm pretty sure this can be best done with a subquery, but I'm not sure
how.

Can someone please point me in the right direction or provide a sample
that I can learn from?

Kindly,
Ken

Jul 23 '05 #1
4 3554
Try the following (untested)...

SELECT
PI.PersonID,
SUM(PM.FeePaid) AS FeePaidForThatM embership,
MAX(MT.Membersh ipYear) AS MostRecentMembe rshipYear,
MT.MembershipDe scription AS DescriptionOfTh atMembership

FROM
PersonInfo PI
LEFT JOIN PeopleMembershi ps PM
ON PI.PersonId = PM.PersonId
LEFT JOIN MembershipTypes MT
ON PM.MembershipTy peId = MT.MembershipTy peId

GROUP BY
PI.PersonId,
MT.MembershipDe scription

I'm not sure if you wanted the total of the fee paid, but I've done
this for you as it would make more sense (to me at least).

Hope this helps

Ryan

Ken Post wrote:
Hello-

I'm fairly new to writing SQL statements and would greatly appreciate
some help on this one.

I'm working on a project for a non-profit that I volunteer for. Part
of the database tracks membership using tables like this:

PersonInfo
-------------------
PersonID (primary key)
FirstName
LastName
etc..
PeopleMembershi ps
-------------------
PPLMembershipIP (primary key)
PersonID
MembershipTypeI D
FeePaid

MembershipTypes
--------------------
MembershipTypeI D (primary key)
MembershipYear
StandardFee
MembershipDescr iption (varchar)
Just because a person is in PersonInfo, doesn't mean they have anything in PeopleMembershi ps (they can be in the databse for other reasons and not have or have ever had a membership).

Membership fees vary by year and type of membership and they want to
retain a history of a person's memberships.

What I'm looking to do here is write a query (a view in SQL Server)
that will return the following Info

PersonID, MostRecentMembe rshipYear, FeePaidForThatM embership,
DescriptionOfTh atMembership

I'm thinking that I'd use max(MembershipY ear), but that requires group by for the other columns, so I'm getting all of the people's
memberships returned.

I'm pretty sure this can be best done with a subquery, but I'm not sure how.

Can someone please point me in the right direction or provide a sample that I can learn from?

Kindly,
Ken


Jul 23 '05 #2
Ken Post (nn*******@gmai l.com) writes:
Just because a person is in PersonInfo, doesn't mean they have anything
in PeopleMembershi ps (they can be in the databse for other reasons and
not have or have ever had a membership).

Membership fees vary by year and type of membership and they want to
retain a history of a person's memberships.

What I'm looking to do here is write a query (a view in SQL Server)
that will return the following Info

PersonID, MostRecentMembe rshipYear, FeePaidForThatM embership,
DescriptionOfTh atMembership

I'm thinking that I'd use max(MembershipY ear), but that requires group
by for the other columns, so I'm getting all of the people's
memberships returned.


It is possible that Ryan's query answers your question, but it case it
didn't, I suggest that you follow the standard advice for this type of
question, and that is to include:

o CREATE TABLE command for your tables.
o INSERT statements with sample data.
o The desired output give the sample data.

One reason for this is that gives you good chances to get a tested
solution. Another reason is that the sample data and the desired output
can help to clarify ambiguities in the narrative. There are several
things in your post that I am not sure how to interpret. And since
I don't like to write queries bases on guesses, I refrain for now.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
"Ken Post" <nn*******@gmai l.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .

Hello-

I'm fairly new to writing SQL statements and would greatly appreciate
some help on this one.

I'm working on a project for a non-profit that I volunteer for. Part
of the database tracks membership using tables like this:

PersonInfo
-------------------
PersonID (primary key)
FirstName
LastName
etc..
PeopleMembershi ps
-------------------
PPLMembershipIP (primary key)
PersonID
MembershipTypeI D
FeePaid

MembershipTypes
--------------------
MembershipTypeI D (primary key)
MembershipYear
StandardFee
MembershipDescr iption (varchar)
Just because a person is in PersonInfo, doesn't mean they have anything
in PeopleMembershi ps (they can be in the databse for other reasons and
not have or have ever had a membership).

Membership fees vary by year and type of membership and they want to
retain a history of a person's memberships.

What I'm looking to do here is write a query (a view in SQL Server)
that will return the following Info

PersonID, MostRecentMembe rshipYear, FeePaidForThatM embership,
DescriptionOfTh atMembership

I'm thinking that I'd use max(MembershipY ear), but that requires group
by for the other columns, so I'm getting all of the people's
memberships returned.

I'm pretty sure this can be best done with a subquery, but I'm not sure
how.

Can someone please point me in the right direction or provide a sample
that I can learn from?

Kindly,
Ken


I'm somewhat curious as to why the last year someone was a member is that
significant.
Would have thought it's more significant if they paid this year.
Anyhow.

My guess is you'd need....

A subquery returns people and the max year they was a member.
Then in or join this to a main query getting the other data you want.
It's saturday and I don't really feel like getting my head round the actual
sql at the moment when I'm just guessing what you want...
Look into subqueries.

I also think it'd be easier if you added a column "LastMembership " or
something to PeopleMembershi ps and updated this with a trigger or whatever
when someone pays their fee.
I would guess that you might be able to have a zero in FeePaid and so that
row doesn't count as being a member.
Anyhow, that way the view'd be easy.

--
Regards,
Andy O'Neill
Jul 23 '05 #4
Thanks all for getting back to me!

First, that's an excellent idea about the trigger setting
LastMembership in the people table. That'll make quick work of this
query.

We need to know the current membership status AND the last time they
were a member (if someone was a member in 1999 only, we're not going to
send a renewal letter).

I wound up using a subquery. Suggestions for improvement are QUITE
welcome! Here's what I did, partly based on the info that I got from
you who responded, part from reading some more posts:

SELECT * FROM (
(SELECT PersonInfo.Pers onID, MembershipTypes .MembershipDesc r,
MembershipTypes .MembershipYear , PeopleMembershi ps.FeePaid
FROM PersonInfo LEFT OUTER JOIN
PeopleMembershi ps ON PersonInfo.Pers onID =
PeopleMembershi ps.PersonID LEFT OUTER JOIN
MembershipTypes ON PeopleMembershi ps.MembershipTy peID =
MembershipTypes .MembershipType ID
GROUP BY PersonInfo.Pers onID, MembershipTypes .MembershipDesc r,
MembershipTypes .MembershipYear , PeopleMembershi ps.FeePaid) AS AllInfo
WHERE (MembershipYear = (SELECT MAX(MembershipT ypes.Membership Year) AS
Expr1
FROM MembershipTypes INNER
JOIN

PeopleMembershi ps ON MembershipTypes .MembershipType ID =
PeopleMembershi ps.MembershipTy peID
WHERE PersonID =
AllInfo.PersonI D)) OR
MembershipYear IS NULL)
Thank you all for your help on this one!

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1455
by: matt | last post by:
hello, im working on my first public-facing ASP.NET 2 website, and i have a question/concern about authentication integration. in ASP.NET 1.1, one would typically go w/ a "role yer own" webforms/database role-based model: in your db youd have a Users table, a RoleGroups table, a UserRoles table (see...
9
2678
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio and run it once you've made sure there is no harmful code. Currently we have several stored procedures which final result is a select with several...
1
1113
by: Rusty Hill | last post by:
I am creating a web app to be shared amongst companies. Each user's credentials will be in the common ASP.NET membership database. Each company will have their own data base. I need to be able to correlate each user to their company so I know what connection string (which I want to store in the membership database as well) to use to get...
3
2170
by: Glenn | last post by:
My current classic-ASP site has users, projects, roles and the 2.0 membership looks like a perfect fit, but I'm having trouble finding examples of how to have users that belong to different projects, and have different roles per project. The current model.. * When a user joins my site, they eventually end up joining or creating one or...
5
6681
by: DotNetNewbie | last post by:
Hi, I am developing an application that has to scale and be very efficient, and I am using asp.net membership in my application. I set things up in my Users table (it has extra columns that I need over and above what aspnet_users has): UserID INT Membershipuserid uniqueidentifier
11
2366
by: unionhorse | last post by:
Hello, We use an access database for the membership administration of thousands of musicians. Membership status varies from "active" to "suspended" to "expelled". Musicians go in and out of active membership. The database keeps track of the last time a member was suspended and the last time a member was expelled. Unfortunately, like...
12
2450
kcdoell
by: kcdoell | last post by:
Hello: I just learned how to put crosstabs queries together but this one in particular is adding a new dimension in which I was hoping someone could give me some direction. I have the following tables The Tables: Table Name = tblStaticAllForecast Field Type IndexInfo
1
1236
by: Jeff | last post by:
Hey ASP.NET 2.0 At work my boss have given me the task of developing a new website. Users will be able to register at the website and gain exclusive access to some information etc... Some users will have limited access to the information and some users (Administrators) will have total access. There will be 3 levels of users: Normal User,...
1
1421
by: =?Utf-8?B?Z29sZGVucmF0ZQ==?= | last post by:
Hi everyone, I have a client who wants a solution for the following problem. I'm not sure that its possible but I'll ask anyway. The client has joined godaddy's reseller plan. They've provided him with a complete working site that can be viewed here: https://www.securepaynet.net/gdshop/rhp/hottestreseller.asp?prog_id=438541&ci=3271& ...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.