2011-07-05 125 views
-1

我使用coldfusion列表系統創建了一個表格,行數是月份,列是城市,變量是銷售額,現在我需要將其添加到每個評估的銷售變量中,公司列表這些銷售,到目前爲止寫這個表和銷售的SQL沒有問題,但我不能定義 - #評估這些公司的名單,屬於表中的每個銷售,因爲它是公司的列表,並不像銷售一個變量,我希望我自己清楚:)這裏是我的代碼:評估ColdFusion變量列表

一些設置查詢:

<cfset month_list_name = "#message13#,#message2#,#message3#,#message4#,#message5#,#message6#,#message7#,#message8#,#message9#,#message10#,#message11#,#message12#"> 
       <cfset type_index = 'NETTOTAL'> 
       <cfquery name="GET_CITY" datasource="#DSN#"> 
        SELECT CITY_ID,CITY_NAME FROM SETUP_CITY ORDER BY CITY_NAME 
       </cfquery> 
       <cfquery name="GET_COMPANY" datasource="#DSN#"> 
        SELECT COMPANY_ID,FULLNAME FROM COMPANY ORDER BY COMPANY_ID 
       </cfquery> 
       <cfset c_index = 'COMPANY_ID'> 
       <cfset no_cities = 0 /> 
       <cfset cities_list = ''> 
       <cfset cities_list_name = ''> 
       <cfset month_list = ''> 
       <cfoutput query="get_city"><cfset cities_list = listappend(cities_list,city_id)><cfset no_cities ++ /></cfoutput> 
       <cfoutput query="get_city"><cfset cities_list_name = listappend(cities_list,city_name)></cfoutput> 
       <cfloop from="1" to="12" index="i"><cfset month_list=listappend(month_list,i)></cfloop> 
       <cfquery name="GET_SALES_TOTAL" datasource="#dsn#"> 
        SELECT 
         SUM(NETTOTAL) NETTOTAL, 
         SC.CITY_ID, 
         DATEPART(MM,INVOICE_DATE) AY, 
         C.FULLNAME, 
         C.COMPANY_ID AS COMPANY_ID 
        FROM  
         #DSN2_ALIAS#.INVOICE I, 
         SETUP_CITY SC, 
         COMPANY C 
        WHERE 
         MONTH(INVOICE_DATE) >= 1 
         AND MONTH(INVOICE_DATE) < 12 
         AND C.COMPANY_ID=I.COMPANY_ID 
         AND SC.CITY_ID=C.CITY 
         AND PURCHASE_SALES=1 
        GROUP BY 
         DATEPART(MM,INVOICE_DATE), 
         SC.CITY_ID, 
         C.FULLNAME, 
         C.COMPANY_ID 
       </cfquery> 

這裏是個對於名單è定義:

<cfloop list="#month_list#" index="month_index"> 
       <cfloop list="#cities_list#" index="city_index"> 
        <cfoutput query="GET_SALES_TOTAL"> 
         <cfif city_id eq city_index and AY eq month_index> 
          <cfloop list="#type_index#" index="tt_index"> 
           <cfset 'alan_#tt_index#_#city_index#_#month_index#' = evaluate(tt_index)> 
           <cfset 'alan2_#city_index#_#tt_index#_#month_index#' = evaluate(tt_index)> 
          </cfloop> 
          <cfloop list="#c_index#" index="cc_index"> 
           <cfif cc_index eq company_id> 
            <cfset 'company_#cc_index#_#city_index#_#month_index#' = evaluate(cc_index)> 
           </cfif> 
          </cfloop> 
         </cfif> 
        </cfoutput> 
       </cfloop> 
      </cfloop> 

和這裏的桌子:

<cfloop list="#month_list#" index="m_index"> 
        <tr class="color-row" height="20"> 
         <td width="150" nowrap><b><cfoutput>#left(listgetat(month_list_name,listfind(month_list,m_index)),20)#</cfoutput></b></td> 
         <cfloop list="#cities_list#" index="ddd_other"> 
          <cfloop list="#type_index#" index="ii_index"> 
           <td align="center"> 
            <cfif isdefined('alan_#ii_index#_#ddd_other#_#m_index#') and len(evaluate('alan_#ii_index#_#ddd_other#_#m_index#'))> 
             <cfset all_toplam=evaluate('alan_#ii_index#_#ddd_other#_#m_index#')+all_toplam> 
             <cfset 'total_#ii_index#_#m_index#'=evaluate('total_#ii_index#_#m_index#') + #evaluate('alan_#ii_index#_#ddd_other#_#m_index#')#> 
             <cfset 'total2_#ddd_other#'=evaluate('total2_#ddd_other#') + #evaluate('alan2_#ddd_other#_#ii_index#_#m_index#')#> 
             <cfif listfindnocase('NETTOTAL',ii_index)> 
              <cfif isdefined('attributes.money') and attributes.money is 2> 
               <cfoutput>#TLFormat((evaluate('alan_#ii_index#_#ddd_other#_#m_index#')/get_money.rate2),2)#</cfoutput> 
              <cfelse> 
               <cfoutput>#TLFormat(evaluate('alan_#ii_index#_#ddd_other#_#m_index#'),2)#</cfoutput> 
              </cfif> 
             <cfelse> 
              <cfif isdefined('attributes.money') and attributes.money is 2> 
               <cfoutput>#TLFormat((evaluate('alan_#ii_index#_#ddd_other#_#m_index#')/get_money.rate2),2)#</cfoutput> 
              <cfelse> 
               <cfoutput>#TLFormat(evaluate('alan_#ii_index#_#ddd_other#_#m_index#'),2)#</cfoutput> 
              </cfif> 
             </cfif> 
            <cfelse> 
             0 
            </cfif> 
            <cfloop list="#c_index#" index="co_index"> 
             <cfif listfindnocase('COMPANY_ID',co_index)> 
              <cfoutput>#evaluate('company_#co_index#_#ddd_other#_#m_index#')#</cfoutput> 
             </cfif> 
            </cfloop> 
           </td> 
          </cfloop> 
         </cfloop> 
         <cfloop list="#type_index#" index="kk_ind"> 
          <td align="center" class="txtbold"> 
           <cfif isdefined('attributes.money') and attributes.money is 2> 
            <cfoutput>#TLFormat((evaluate('total_#kk_ind#_#m_index#')/get_money.rate2),2)#</cfoutput> 
           <cfelse> 
            <cfoutput>#TLFormat(evaluate('total_#kk_ind#_#m_index#'),2)#</cfoutput> 
           </cfif> 
          </td> 
         </cfloop> 
        </tr> 
        </cfloop> 

它說:變量company_COMPANY_ID_1_1是不確定的。我的錯誤在哪裏? 我不能像評估NETTOTAL一樣評估company_idies的LIST,因爲淨總數不是列表,但只有一個值...並感謝大家的幫助和關注! ;)

+1

太多的代碼... –

+0

@richard它足以看看最後兩個與c_index,其他人就像一個例子 – user775917

回答

1

在這段代碼中你可以改進很多東西。

增加循環內的城市數量是不必要的。您可以在建立您的列表(listlen(cities_list)後直接從SQL(select count(*) from setup_city)或CF中獲取計數。

對於這個問題,它手動是不必要的遍歷列表時,你可以簡單地使用值列表有CF爲你做的工作:

cities_list = ValueList(get_city.city_id); 
cities_list_name = ValueList(get_city.city_name); 

沒有理由建月份列表,如果你'將要做的是從1到12循環。改爲使用1到12循環。

type_index如果您只有一個項目,則不需要列表。改用該變量。只有在實際需要列表時纔將其替換爲列表;這也將消除使用evaluate的需要,這會使您的代碼難以閱讀並且難以讓人們遵循。 cc_index同樣的事情。

因爲範圍可以像訪問結構那樣訪問,所以使用structkeyexists(attributes, "money")而不是isdefined("attributes.money")通常更好(並且速度可以更快)。指定isdefined的範圍確實有幫助,當然...... isdefined("money")不僅速度緩慢,而且如果您不小心可能會返回完全不同的變量。

說到結構,我肯定會推薦你使用它們而不是連接變量名。要解析你所寫的內容是非常困難的,以便理解你想要做的事情:使用結構可以更容易地遵循。

最後,在我看來,您在ColdFusion中做了大量工作,以便您的數據庫能夠更輕鬆地完成一些操作。如果你需要城市公司的總計,爲什麼不這樣做呢?

<cfquery name="GET_SALES_GRAND_TOTAL" datasource="#dsn#"> 
    SELECT 
     SUM(NETTOTAL) NETTOTAL, 
     SC.CITY_ID, 
     C.FULLNAME, 
     C.COMPANY_ID AS COMPANY_ID 
    FROM  
     #DSN2_ALIAS#.INVOICE I, 
     SETUP_CITY SC, 
     COMPANY C 
    WHERE 
     MONTH(INVOICE_DATE) >= 1 
     AND MONTH(INVOICE_DATE) < 12 
     AND C.COMPANY_ID=I.COMPANY_ID 
     AND SC.CITY_ID=C.CITY 
     AND PURCHASE_SALES=1 
    GROUP BY 
     SC.CITY_ID, 
     C.FULLNAME, 
     C.COMPANY_ID 
</cfquery> 

是否需要檢查月份是否在1到12之間?如果INVOICE_DATE存在,則必須有1到12個月的時間。我會完全刪除查詢中的這些行或將其替換爲INVOICE_DATE IS NOT NULL

然後,您應該能夠得到你的結果表的東西看起來大致是這樣的:

<cfoutput query="GET_SALES_TOTAL" group="AY"> 
    <!--- months first ---> 
    <tr> 
     <!--- month-specific stuff goes here ---> 
     <td>month_header</td> 
     <cfoutput group="city_id"> 
      <!--- city-specific stuff ---> 
      <td><!--- format NETTOTAL however you want here ---></td> 
     </cfoutput> 
    </tr> 
</cfoutput> 
<tr> 
    <td>grand_total_header</td> 
    <cfoutput query="GET_SALES_GRAND_TOTAL" group="city_id"> 
     <td><!--- format NETTOTAL here ---></td> 
    </cfoutput> 
</tr> 

希望這是對你有所幫助。我強烈建議你閱讀一下數組和結構;使用evaluate的用戶通常會在管理事情變得更加簡單的情況下采用這種方式,並且您應該可以從中學到很多有關CF和其他地方的示例。

+0

謝謝你所有這些指示,我會盡量改變它在更好方式,因爲你可能理解,即時通訊只是一個新手在ColdFusion和SQL,所以我做了我可以))我會接受你的答案是正確的,因爲沒有多少人回答它,反而把壞))) – user775917

+0

順便說一句,它工作完美,更快,更容易! :)但仍然有一些錯誤))http://stackoverflow.com/questions/6607421/group-by-cities – user775917

+0

@用戶,很高興我可以幫助;如果你有更多的問題,這是一個好地方。 –