Solved

Stored Procedure?

Posted on 2013-05-20
4
90 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: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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

838 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