Solved

Stored Procedure?

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

685 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