sql重复问题

时间:2011-06-24 12:38:20

标签: sql

与我之前提出的问题完全相同:sql take one out from the list :)但我无法整合

CPA.WANT_EMAIL = 1

而不是

AND CPA.PARTNER_ID = CCD.PARTNER_ID AND CCD.SITE_DOMAIN = 'www.projedepo.com')

我的意思是want_email也来自COMPANY_PARTNER CPA一切都是一样的,除了,site_domain需要与www.projedepo.com完全相同,但对于want_email,我只需要负数,0或正数1,值的格式不同。

我该怎么办? thx帮助大家!

以下是现在的代码:

<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>

这是前面的例子,工作的,我需要的是改变AND CPA.PARTNER_ID = CCD.PARTNER_ID AND CCD.SITE_DOMAIN = 'www.projedepo.com'

CPA.WANT_EMAIL = 1

1 个答案:

答案 0 :(得分:1)

这对你有用吗?

. . .
      AND EXISTS
        ( SELECT *
          FROM
            COMPANY_PARTNER CPA
          WHERE C.COMPANY_ID = CPA.COMPANY_ID
            AND CPA.WANT_EMAIL = 1
        )
. . .

也就是说,只需用现有的EXISTS子查询替换它。


<强>更新

看起来这里需要一种不同的方法。试试这个:

<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>