开发者

sql take one out from the list

开发者 https://www.devze.com 2023-03-10 19:41 出处:网络
here is the situation, i have a few companies, and in every company there are few partners(workers), and i need to make a list of these companies, the problem that the defining factor to list or not t

here is the situation, i have a few companies, and in every company there are few partners(workers), and i need to make a list of these companies, the problem that the defining factor to list or not to list the company is in the partner's definition, so wh开发者_开发技巧en i need to be listed the companies, they are listed a few times, because there are a few partners in each one :) i hope it is everything clear, so far i have wrote a sql code:

   <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>
                    ,CCD.SITE_DOMAIN
                    </cfif>
                FROM 
                    COMPANY C, 
                    COMPANY_CAT CC
                    <cfif isdefined('attributes.report_sort2') and attributes.report_sort2 is 1>
                    ,COMPANY_CONSUMER_DOMAINS CCD,
                    COMPANY_PARTNER CPA
                    </cfif>
                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 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>

As you can see i have to list the companies in which the partners have an access to a website projedepo, but instead it repeats the name of the company each time the partner inside the company have an access to this website, because it sorts the partners rather than companies, how do i delete these repeating companies? i saw that there is a way to achieve it though deleting the repeating the same company id, but how to do it, i dont know, need help, and thx everyone for help!


Try this one:

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

It would also be good if you used the JOIN syntax and not the (implicit JOIN with) WHERE:

<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
      JOIN COMPANY_CAT CC
          ON CC.COMPANYCAT_ID = C.COMPANYCAT_ID
    WHERE
          C.COMPANYCAT_ID = #attributes.comp_cat# 
        <cfif isdefined('attributes.report_sort2')
          and attributes.report_sort2 is 1>
          AND EXISTS
            ( SELECT *
              FROM   COMPANY_CONSUMER_DOMAINS CCD
                JOIN COMPANY_PARTNER CPA
                    ON CPA.PARTNER_ID = CCD.PARTNER_ID
              WHERE C.COMPANY_ID = CPA.COMPANY_ID
                AND CCD.SITE_DOMAIN = 'www.projedepo.com'
            )
        </cfif>
    ORDER BY C.RECORD_DATE DESC
</cfquery>


Surely SELECT DISTINCT would do what you need?

0

精彩评论

暂无评论...
验证码 换一张
取 消