Solved

Stored Procedure?

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

708 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

15 Experts available now in Live!

Get 1:1 Help Now