sql repeating question
Got exactly the same question as i asked before: sql take one out from the list :) but i cant integrate the
CPA.WANT_EMAIL = 1
instead of
AND CPA.PARTNER_ID = CCD.PARTNER_ID AND CCD.SITE_DOMAIN = 'www.projedepo.com')
i mean want_email is also from COMPANY_PARTNER CPA
everything is the same, except, site_domain is needed to be exactly the same as www.projedepo.com but for want_email, i need just the negative, 0 or positive 1, the formats of values are different.
how do i do it? thx for help everyone!
Here is the code for now:
开发者_StackOverflow<cfquery name="GET_POT_COMPANY" datasource="#DSN#">
SELECT
C.COMPANY_ID,
C.MEMBER_CODE,
C.FULLNAME,
C.PARTNER_ID,
C.RECORD_DATE,
CC.COMPANYCAT
<cfif isdefined('attributes.report_sort2')
and attributes.report_sort2 is 1>
, 'www.projedepo.com' AS SITE_DOMAIN
</cfif>
FROM
COMPANY C,
COMPANY_CAT CC
WHERE
C.COMPANYCAT_ID = #attributes.comp_cat#
AND CC.COMPANYCAT_ID = C.COMPANYCAT_ID
<cfif isdefined('attributes.report_sort2')
and attributes.report_sort2 is 1>
AND EXISTS
( SELECT *
FROM
COMPANY_CONSUMER_DOMAINS CCD,
COMPANY_PARTNER CPA
WHERE C.COMPANY_ID = CPA.COMPANY_ID
AND CPA.PARTNER_ID = CCD.PARTNER_ID
AND CCD.SITE_DOMAIN = 'www.projedepo.com'
)
</cfif>
ORDER BY
C.RECORD_DATE DESC
</cfquery>
this is the previous example, the working one, all i need is to change the AND CPA.PARTNER_ID = CCD.PARTNER_ID AND CCD.SITE_DOMAIN = 'www.projedepo.com'
to CPA.WANT_EMAIL = 1
Will this work for you:
. . .
AND EXISTS
( SELECT *
FROM
COMPANY_PARTNER CPA
WHERE C.COMPANY_ID = CPA.COMPANY_ID
AND CPA.WANT_EMAIL = 1
)
. . .
?
That is, just replace the existing EXISTS subquery with this one.
UPDATE
Looks like a different approach is needed here. Try this:
<cfquery name="GET_POT_COMPANY" datasource="#DSN#">
SELECT
C.COMPANY_ID,
C.MEMBER_CODE,
C.FULLNAME,
C.PARTNER_ID,
C.RECORD_DATE,
CC.COMPANYCAT
<cfif isdefined('attributes.report_sort2')
and attributes.report_sort2 is 1>
, COALESCE(CPA.WANT_EMAIL, 0) AS WANT_EMAIL
</cfif>
FROM
COMPANY C
INNER JOIN COMPANY_CAT CC ON CC.COMPANYCAT_ID = C.COMPANYCAT_ID
<cfif isdefined('attributes.report_sort2')
and attributes.report_sort2 is 1>
LEFT JOIN (
SELECT
COMPANY_ID,
MAX(CAST(WANT_EMAIL AS int)) AS WANT_EMAIL
FROM
COMPANY_PARTNER CPA
GROUP BY COMPANY_ID
) CPA ON C.COMPANY_ID = CPA.COMPANY_ID
</cfif>
WHERE
C.COMPANYCAT_ID = #attributes.comp_cat#
ORDER BY
C.RECORD_DATE DESC
</cfquery>
精彩评论