?
Solved

Stored Procedure?

Posted on 2013-05-20
4
Medium Priority
?
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 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 49

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:Scott Pletcher
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

752 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