sql take one out from the list

痴心易碎 提交于 2019-12-13 05:25: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 to list the company is in the partner's definition, so when 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!


回答1:


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>



回答2:


Surely SELECT DISTINCT would do what you need?



来源:https://stackoverflow.com/questions/6266418/sql-take-one-out-from-the-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!