2012-11-28 3 views
1

나는 한동안 질문 (Query with grouped results by column (SQL Server))을 요청했습니다. 그리고 SQL 서버에서 작동하는 몇 가지 답변이 있지만 QoQ의 일부로 작동 할 수는 없습니다. CF는 몇 가지 사소한 제한이 있습니다. 사용할 수 없듯이 INNER JOINcoldfusion QoQ 및 조건부

동일한 항목에 대해 여러 항목 이름을 가질 수있는 쿼리를 얻을 수 있지만 내 전화를 걸 때 QoQ, 언어 ID와 일치하는 항목 (있는 경우)을 필터링 (유지)하고 누락 된 경우 다른 항목을 기본값으로 지정합니다.

여러 쿼리에 대해이 작업을 수행하고 있으므로 함수에 코드를 갖기 위해 쿼리를 연결하고 uniqueColumn 이름은 languageId로 지정합니다.

내부 조인을 사용할 수없고 일부 조건에 문제가 있기 때문에 일치하는 languageId 만있는 두 번째 테이블을 만든 다음 다른 하나에서 누락 된 항목을 추가하려고합니다. .

하나의 쿼리에서이 작업을 수행 할 수있는 방법이 있습니까?

답변

0

Q의 Q를 사용하여 내부 조인을 수행 할 수 있습니다. "조인"키워드는 사용할 수 없습니다. where 절에서 쿼리를 조인해야합니다. 다음과 같은 내용이 있습니다.

select whatever 
from query1, query2 
where query1.field1 = query2.field2 
etc 

Q 개의 Q 조합 쿼리는 데이터베이스 쿼리와 동일한 방식으로 수행됩니다. 이런 식으로하려면 "언어 ID와 일치하는 항목을 필터링 (유지)하고, 누락 된 언어 ID가있는 경우 다른 항목을 기본값으로 유지합니다."코드는 다음과 유사합니다.

select query2.actual_value 
from query1, query2 
where query1.field1 = query2.field2 
etc 
union 
select default_value 
from query1 
where field1 not in(ValueList(query2.field2)) 

하지만 올바른 구문 물론 queryparams와

0

는 일을 끝내 꽤 많은 : 기존 솔루션에 비해, 이것은 꽤 :)

<cffunction name="getUniqueName" output="true" returntype="Query"> 
    <cfargument name="q" Type="query" required="true"> 
    <cfargument name="uniqueColumn" Type="string" required="false" default="ID"> 
    <cfargument name="languageId" Type="string" required="false" default=""> 

    <cfif languageID EQ ''><cfset languageID = #session.langID#></cfif> <!--- default language id to user language id ---> 

    <!--- Get all items that match language ID ---> 
    <cfquery dbtype="query" name="qwLangId"> 
     SELECT * 
     FROM 
      q 
     WHERE 
      languageid = #languageId# 
    </cfquery> 

    <!--- Get list of IDs for items found ---> 
    <cfset usedIDs = ArrayToList(qwLangId[uniqueColumn])> 

    <cfif usedIDs NEQ ''> 
     <!--- get all items that were not found in matched query ---> 
     <cfquery dbtype="query" name="qMissing"> 
      SELECT * 
      FROM 
       q 
      WHERE #uniqueColumn# NOT IN(#usedIDs#) 
     </cfquery> 

     <!--- combine items in a new query ---> 
     <cfquery dbtype="query" name="langQ"> 
      SELECT * FROM qwLangId 
      UNION 
      SELECT * FROM qMissing 
      ORDER BY #uniqueColumn# 
     </cfquery> 
     <cfreturn langQ> 
    <cfelse> 
     <cfreturn q> 
    </cfif> 

</cffunction> 
그것을 속도