2016-04-05 9 views
1

私はすべてのブランチが表示されているので、100%以来簡単だろうと思う私のパーセンテージ列の合計を作成するのに苦労しています。しかし、私はすべてのブランチが表示されていない時の方程式を理解する必要があります。下の図では、各ブランチのパーセンテージは、処理されたチェックリストの数をチェックリストの総数で割った数によって計算されます。残念ながら、パーセンテージ列の合計を加算して合計列に表示するだけでは、単に「書き込む」方法を理解することはできません。どんな助けでも大歓迎です。CFパーセンテージ列

<cfset result = {} /> 
<cftry> 
    <cfquery datasource="#application.dsn#" name="GetLocationInfo"> 
     SELECT * 
     FROM cl_checklists 
    </cfquery> 

    <cfquery name="allLocCode" dbtype="query"> 
     SELECT DISTINCT trans_location, COUNT(*) AS locationCount FROM GetLocationInfo Where trans_location is not null GROUP BY trans_location ORDER BY trans_location 
    </cfquery> 
    <cfcatch type="any"> 
     <cfset result.error = CFCATCH.message > 
     <cfset result.detail = CFCATCH.detail > 
    </cfcatch> 
</cftry> 
    <cfset columnSum = ArraySum(allLocCode['locationCount'])> 
<table border="1" id="Checklist_Stats"> 
    <thead> 
     <th><strong>Location</strong></th> 
     <th><strong>Percent of Total Checklists</strong></th> 
     <th><strong>Location Total</strong></th> 
    </thead> 
    <tbody> 
    <cfloop query="allLocCode"> 
     <cfset thisLocationName = trim(allLocCode.trans_location) /> 

     <cfquery name="allLocCodeForLocationQry" dbtype="query"> 
      SELECT trans_location,count(*) AS locCntr FROM GetLocationInfo WHERE trans_location='#thisLocationName#' GROUP BY trans_location ORDER BY trans_location 
     </cfquery> 
     <cfoutput query="allLocCodeForLocationQry"> 
     <tr> 
     <td><strong>#thisLocationName#</strong></td> 
     <td>#NumberFormat((allLocCodeForLocationQry.locCntr/columnSum) * 100, '9.99')#%</td> 
     <td>#allLocCodeForLocationQry.locCntr#</td> 
     </tr> 
    </cfoutput> 
    </cfloop> 
     <cfdump var="#allLocCodeForLocationQry.locCntr#"> 
    <tr> 
     <td><strong>Total</strong></td> 
     <td></td> 
     <td><cfoutput>#columnSum#</cfoutput></td> 
    </tr> 
    </tbody> 
</table> 

enter image description here

私はこれの合計を得ることができる方法がわからない:<td>#NumberFormat((allLocCodeForLocationQry.locCntr/columnSum) * 100, '9.99')#%</td>

答えて

2

あなたは、単に配列に計算されたパーセンテージをプッシュすることができ、そこから、あなたはこのように合計を取得することができます:

<!--- Define Array --> 
<cfset checkListPercentage = arrayNew(1)> 

<cfoutput query="allLocCodeForLocationQry"> 
    <cfset currentPercentage = allLocCodeForLocationQry.locCntr/columnSum * 100)> 
    <cfset arrayAppend(checkListPercentage, currentPercentage)> 
    <tr> 
    <td><strong>#thisLocationName#</strong></td> 
    <td>#numberFormat(currentPercentage, '9.99')#%</td> 
    <td>#allLocCodeForLocationQry.locCntr#</td> 
    </tr> 
</cfoutput> 

<!--- Get Total ---> 
<cfoutput>#arraySum(checkListPercentage)#</cfoutput> 
+0

これは私がなく、より簡単でより効率的に伝えるためにしようとしていたものです。 @Beginnerはおそらくエキスパートに似ています。 – TRose

+0

これは基本的にこれを解決するだけで、2小数点をどのように表示するのですか?あなたは数字の書式と配列を同時に使用できますか? –

+0

@DavidBriertonはい、できます。 – Beginner

0

私はこれらの質問のいずれかにする前に多少似て答えを掲載しました。


あなたがパーセントを取得するたびに、あなたが変数として設定し、リストや配列に追加する必要があります:あなたがする必要がどここれはあなたを得るでしょう。このコードのチャンクの例としては:

<cfloop query="allLocCode"> 
     <cfset thisLocationName = trim(allLocCode.trans_location) /> 
     <cfquery name="allLocCodeForLocationQry" dbtype="query"> 
      SELECT trans_location,count(*) AS locCntr FROM GetLocationInfo WHERE trans_location='#thisLocationName#' GROUP BY trans_location ORDER BY trans_location 
     </cfquery> 

     <cfset PercentageList = ""> 
     <cfset thisPercentage = #NumberFormat((allLocCodeForLocationQry.locCntr/columnSum) * 100, '9.99')#> 
     <cfset PercentageList = ListAppend(PercentageList, thisPercentage, ',')> 

     <cfoutput query="allLocCodeForLocationQry"> 
     <tr> 
     <td><strong>#thisLocationName#</strong></td> 
     <td>#thisPercentage#%</td> 
     <td>#allLocCodeForLocationQry.locCntr#</td> 
     </tr> 
    </cfoutput> 
</cfloop> 

すべてのあなたの計算の終わりに、あなたは、割合のリストを持っている必要があります。リストを一緒に追加するには、この関数を含めることができます。

<cfscript> 
function listSum(listStr) 
{ 
    var delim = ","; 
    if(ArrayLen(Arguments) GTE 2) 
    delim = Arguments[2]; 
    return ArraySum(ListToArray(listStr, delim)); 
} 
</cfscript> 

だからあなたの最後の行は次のようになります。

<tr> 
    <td><strong>Total</strong></td> 
    <td>#listSum(PercentageList)#%</td> 
    <td><cfoutput>#columnSum#</cfoutput></td> 
</tr> 

言及する価値:以前は、それは、その後アレイとを使用しては、あなたの計算結果の最後にリストに変換することを私に指摘されましたより良いパフォーマンスで、それは心に留めておくべきことです。

ArraySumはこれに関連する機能です。

他のループやクエリ内のすべてのループとクエリを使用すると、リストを作成するのがやや簡単だったと思っただけです。

E - 原因不明の睡眠薬はうまくありません。