SELECT DISTINCT a.customer_no FROM V_CUSTOMER_WITH_PRIMARY_GROUP a WITH (NOLOCK)
JOIN (SELECT a1.customer_no FROM lvs_soft_credit a1 WITH (NOLOCK) WHERE a1.appeal_no IN (2830,3649,3435)
AND a1.cont_dt BETWEEN '2016/01/01' And '2017/06/30 23:59:59'
GROUP BY a1.customer_no HAVING SUM(a1.cont_amt) >= .01) AS e
ON e.customer_no = a.customer_no
JOIN (SELECT a1.customer_no From VXS_CONST_CUST a1 WITH (NOLOCK) WHERE a1.constituency IN (4,25)) AS f
ON f.customer_no = a.customer_no
Where IsNull(a.inactive, 1) = 1
-- START DETAIL!!!
-- Select distinct customer_no from V_CUSTOMER_WITH_PRIMARY_GROUP
SELECT DISTINCT a.customer_no FROM V_CUSTOMER_WITH_PRIMARY_GROUP a WITH (NOLOCK)
-- JOIN patrons with appeals 2830, 3649, 3435 in lvs_soft_credit
JOIN (SELECT a1.customer_no FROM lvs_soft_credit a1 WITH (NOLOCK) WHERE a1.appeal_no IN (2830,3649,3435)
-- AND having a contribution between these dates (part of the JOIN started on line 15)
AND a1.cont_dt between '2016/01/01' And '2017/06/30 23:59:59'
-- GROUP the results of the JOIN by customer_no and select patrons HAVING the SUM(cont_amt) greater than or equal to .01), ends the JOIN started on line 15, names the result of this JOIN 'e'
GROUP BY a1.customer_no HAVING SUM(a1.cont_amt) >= .1) AS e
-- ON, says match the patrons up based on customer_no in lvs_soft_credit being the same as customer_no in V_CUSTOMER_WITH_PRIMARY_GROUP
ON e.customer_no = a.customer_no
-- Then further JOIN patrons who have constituencies 4 and 25 in VXS_CONST_CUST, name the results 'f'
JOIN (SELECT a1.customer_no From VXS_CONST_CUST a1 WITH (NOLOCK) WHERE a1.constituency IN (4,25)) AS f
-- ON, says match the patrons up based on customer_no in VSX_CONST_CUST being the same as customer_no in V_CUSTOMER_WITH_PRIMARY_GROUP
ON f.customer_no = a.customer_no
-- filters out inactives
WHERE IsNull(a.inactive, 1) = 1