Solved

Stored Procedure?

Posted on 2013-05-20
4
87 Views
Last Modified: 2014-12-14
Hi Everyone, I am trying to fill a need and I think the best way to do it may be a stored procedure.

So...  I have this query which pulls back data.  This query runs fine.

select a.household, a.record_id, a.FirstName, a.LastName, b.MemberStatus from tblPeopleMain As a, tblPeopleChurchInfo b Where Not Exists (Select HouseHold, AmountPledged from tblPledgesN As c Where a.HouseHold = HouseHold And PledgeYear = '2013' and AmountPledged <> 0) AND a.FamilyPos = 'Head' and b.PeopleID = a.RecordID and b.MemberStatus = 'Active' order by LastName asc

From the data that returns I then want to add a column called Contributed2013 and for each record above see if the person contributed in 2013 by running this query and adding a Y or a N in the column I added.

Select Amount from tblContributionN where record_idreturnedfromabove = PeopleID and Amount <> 0

If it returns a record or multiple records I want it to put a Y in the column.

Any help formulating this would be great.  I am thinking it is pretty easy for most of you!  Thanks in advance.
0
Comment
Question by:jwebster77
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39180884
SELECT
      a.household
    , a.record_id
    , a.FirstName
    , a.LastName
    , b.MemberStatus
   , isnull(c.indicator, 'N')
, c.sum_amount /* is this the contibuted 2013 ? I'm not clear on this */
FROM tblPeopleMain AS a
INNER JOIN tblPeopleChurchInfo b ON b.PeopleID = a.RecordID
    AND b.MemberStatus = 'Active'
LEFT JOIN (
            SELECT PeopleID
                , 'Y' as indicator
                , sum(Amount) AS sum_amount
            FROM tblContributionN
            WHERE Amount <> 0
            GROUP BY PeopleID
            ) AS C ON b.PeopleID = C.PeopleID
WHERE a.FamilyPos = 'Head'
    AND NOT EXISTS (
                    SELECT HouseHold
                        , AmountPledged
                    FROM tblPledgesN AS c
                    WHERE a.HouseHold = HouseHold
                        AND PledgeYear = '2013'
                        AND AmountPledged <> 0
                    )
ORDER BY LastName ASC

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39180905
notes on the above:
I have introduce the term INNER JOIN (which is what you were doing anyway)
The additional query you wanted is contained within a LEFT JOIN
- this will match when contributions do exist, but be null if contributions do not exist
- so, in the selection list use ISNULL(c.indicator,'N') to produce the Y or N you asked for

What I don't see in the wanted additional query is any date restriction, e.g.

Select Amount from tblContributionN where record_idreturnedfromabove = PeopleID and Amount <> 0 (and the contributions are in 2013)

so, under line 17 above, some added where conditions are probably needed (I think)
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39181821
SELECT
    a.household, a.record_id, a.FirstName, a.LastName,
    b.MemberStatus,
    CASE WHEN EXISTS(SELECT 1 FROM dbo.tblContributionN AS n WHERE n.PeopleID = a.record_id AND n.Amount <> 0) THEN 'Y' ELSE 'N' END AS Contributed2013
FROM dbo.tblPeopleMain AS a
INNER JOIN dbo.tblPeopleChurchInfo AS b ON
    b.PeopleID = a.RecordID AND
    b.MemberStatus = 'Active'
WHERE
    a.FamilyPos = 'Head' AND
    NOT EXISTS (SELECT HouseHold, AmountPledged FROM tblPledgesN AS c WHERE a.HouseHold = c.HouseHold AND c.PledgeYear = '2013' AND c.AmountPledged <> 0)
ORDER BY
    a.LastName ASC
0
 
LVL 27

Expert Comment

by:tliotta
ID: 39183612
    CASE WHEN EXISTS(SELECT 1 FROM dbo.tblContributionN AS n WHERE n.PeopleID = a.record_id AND n.Amount <> 0) THEN 'Y' ELSE 'N' END AS Contributed2013

Open in new window

That CASE is either correct or it needs a minor change. It would depend on a minor detail of how transactions are record.

Say, for example, that an Amount is entered and later backed out. There would be two recorded transactions that would SUM() to zero, yet each transaction would cause a simple EXISTS() to report as 'TRUE'. If that's the case, then the SELECT needs to GROUP BY PeopleID and SUM(Amount) before testing for zero.

But if Amount is already a summary value, then the CASE should be okay. (Untested, but it looks good.)

Tom
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now