2016-06-10 49 views
1

[增訂]問:
那利提供工作,但查詢出於某些原因,當我更換了行和列的名稱來查詢,從每點擊成本每登記成本變更和每個許可證的成本,我會得到不同於預期的值。變量值

注意: 下面列出的結果僅用於每個註冊的成本,而不是每個許可的成本。都來自同一張桌子,所以如果一個人是固定的,最有可能的第二個會跟風。我也更新了AdReport表包括許可證柱等

我預計

Date CPR 
1  $31.35 
2  $61.42 
3  $77.85 
4  $78.48 
5  $55.11 

我有什麼

Date CPR 
1  971.9412 
2  1781.2939 
3  2421.733 
4  2355.4679 
5  1598.164 

查詢:

<cfquery name="costPerRegistration" datasource="#dsn#"> 
SELECT ab.AdMonth AS Date, 
    CASE WHEN SUM(ar.Conversions) > 0 THEN SUM(ab.AdBudget)/SUM(ar.Conversions) 
      ELSE 0 
      END AS CPR 
FROM AdBudget AS ab INNER JOIN AdReport AS ar 
     ON DATEPART(MONTH, ar.ReportDate) = ab.AdMonth 
     AND DATEPART(YEAR, ar.ReportDate) = ab.AdYear 
     AND ar.AdSourceID = ab.AdSourceID 
    WHERE ab.AdYear = '2016' 
    AND ar.AdSourceID != 4 
    GROUP BY ab.AdMonth 
    ORDER BY ab.AdMonth 
</cfquery> 

<cfquery name="costPerLic" datasource="#dsn#"> 
SELECT ab.AdMonth AS Date, 
    CASE WHEN SUM(al.Licenses) > 0 THEN CAST(SUM(ab.AdBudget)/SUM(al.Licenses) AS smallmoney) 
      ELSE 0 
      END AS CPL 
FROM AdBudget AS ab INNER JOIN AdReport AS al 
     ON DATEPART(MONTH,al.ReportDate) = ab.AdMonth 
     AND DATEPART(YEAR,al.ReportDate) = ab.AdYear 
     AND al.AdSourceID = ab.AdSourceID 
WHERE ab.AdYear = 2016 
AND  ab.AdSourceID != 4 
GROUP BY ab.AdMonth 
ORDER BY ab.AdMonth 
</cfquery> 

代碼:

<cfloop index = "i" from = "1" to = "#AdBudget.RecordCount#"> 
    <cfset Clicks.Click[i] = AdBudget.Budgeting/Clicks.Click[i]> 
    <cfset Registrations.Conver[i] = AdBudget.Budgeting/Registrations.Conver[i]> 
    <cfset Licenses.License[i] = AdBudget.Budgeting/Licenses.License[i]> 
</cfloop> 

<!--- Bar graph, from Query of Queries ---> 
<cfchart> 
    <cfchartseries type="curve" 
     seriescolor="##5283DA" 
     serieslabel="Cost per Clicks" 
      <cfchartdata item="1" value="#Click#"> 
    </cfchart> 
</cfchart> 

數據:

樣本數據加入,忽視在表中的sourceID和其他ID。

AdBudgetID AdBudget AdMonth AdSourceID AdYear 
    1 7663 1 1 2016 
    2 20301 2 1 2016 
    3 5555 1 2 2016 
    4 16442 2 2 2016 
    5 1706 1 3 2016 
    6 4841 2 3 2016 
    7 11384 3 1 2016 
    8 23726 3 2 2016 
    9 9653 3 3 2016 
    13 17557.98 5 1 2016 
    14 25685.72 5 2 2016' 

AdClickID AdClicks AdMonth AdSourceID AdYear 
1 2229 1 1 2016 
2 1803 1 2 2016 
3 371 1 3 2016 
4 4940 2 1 2016 
5 5855 2 2 2016 
6 673 2 3 2016 
7 2374 3 1 2016 
8 12913 3 2 2016 
9 1400 3 3 2016 
13 2374 4 1 2016 
14 10272 4 2 2016 

    AdReportID ReportDate AdSourceID Clicks Conversions Demos Clients Licenses Onboardings AvgScore 
2430 2016-03-27 1 1 1 0 0 0 0 NULL 
2431 2016-03-27 2 5 0 0 0 0 0 NULL 
2432 2016-03-27 3 1 0 0 0 0 0 NULL 
2433 2016-03-27 5 24 0 0 0 0 0 NULL 
2434 2016-03-27 6 0 0 0 0 0 0 NULL 
2435 2016-03-27 6 0 0 0 0 0 NULL NULL 
2436 2016-03-27 4 0 1 0 0 0 1 NULL 
2437 2016-03-26 1 2 0 0 0 0 0 NULL 

對不起關於表配置,不知道如何使它整潔。此外,我們還有更多的轉換(註冊)和許可證,這些數據在示例數據中沒有顯示,只是發生在前10行的數字較少。

+1

您意識到變量Click是一個數組,對吧? –

+0

...所以如果你想創建一個數據點,對於數組中的每個元素,你需要遍歷它,並在每個元素上創建一個數據點。附註1)cfloop可能不會做你認爲的事情。它將始終在查詢的第一行使用「AdBudget.Budgeting」值。 2)擺脫'Evaulate()'。在這種情況下,它什麼都不做。 – Leigh

+0

我意識到丹,我不知道如何cfloop數組,我認爲這將工作,我只是不知道如何調用從cfloop到圖表的變量。 –

回答

0

如何設置圖表以確保其正常

圖表他們,我會實際上這個問題開始,而不是查詢。從一個小的硬編碼圖表開始,這個圖表很容易調整,以便準確找出應該如何構建標籤以生成所需的圖表。最終的分組將決定你應該如何聚合查詢數據。

假設您想要將2016年1月至5月的「每次點擊費用」和「每次註冊費用」列爲單獨系列。創建一個硬編碼的例子是這樣的:

<cfchart> 
    <cfchartseries type="curve" serieslabel="Cost per Clicks"> 
     <cfchartdata item="1" value="15"> 
     <cfchartdata item="2" value="50"> 
     <cfchartdata item="3" value="47"> 
     <cfchartdata item="4" value="32"> 
     <cfchartdata item="5" value="65"> 
    </cfchartseries> 
    <cfchartseries type="curve" serieslabel="Cost per Registration"> 
     <cfchartdata item="1" value="45.52"> 
     <cfchartdata item="2" value="17.68"> 
     <cfchartdata item="3" value="28.50"> 
     <cfchartdata item="4" value="78.62"> 
     <cfchartdata item="5" value="42.50"> 
    </cfchartseries> 
</cfchart> 

產生以下圖表:

Hard coded chart

望着cfchartdata標籤,表示需要兩個疑問:一個包含了「每次點擊費用」按月,以及另一個月的「每註冊成本」。

計算每次點擊的成本,上月加入AdBudgetAdClick,一年源ID列。集團的結果通過一個月,由點擊劃分預算:

SQLFiddle Example:

<cfquery name="costPerClick" ....> 
SELECT ab.AdMonth 
     , CASE WHEN SUM(ac.AdClicks) > 0 THEN SUM(ab.AdBudget)/SUM(ac.AdClicks) 
       ELSE 0 
      END AS CostPerClick 
FROM AdBudget ab LEFT JOIN AdClick ac 
      ON ac.AdMonth = ab.AdMonth 
      AND ac.AdYear = ab.AdYear 
      AND ac.AdSourceID = ab.AdSourceID 
WHERE ab.AdYear = 2016 
AND  ab.AdSourceID <> 4 
GROUP BY ab.AdMonth 
ORDER BY ab.AdMonth 
</cfquery> 

然後簡單地通過查詢循環來生成cfchartseries:

<cfchart> 
    <cfchartseries type="curve" serieslabel="Cost per Clicks"> 
     <cfoutput query="costPerClick"> 
      <cfchartdata item="#costPerClick.AdMonth#" 
       value="#DecimalFormat(costPerClick.Amount)#"> 
     </cfoutput> 
    </cfchartseries> 
</cfchart> 

使用上面的提示,您應該能夠按照相同的流程生成其他兩個查詢和系列。

更新:

原來我誤會AdReport的結構。由於它包含每個月/年/源ID組合的多個記錄,因此您需要先計算每月的總轉化次數。然後將結果加入AdBudget。因此,「每個註冊成本」查詢需要如下所示:

SELECT ab.AdMonth 
     , CASE WHEN SUM(ar.Conversions) > 0 THEN SUM(ab.AdBudget)/SUM(ar.Conversions) 
       ELSE 0 
      END AS CPR 
FROM AdBudget ab LEFT JOIN 
     (
      /* Calculate total conversions per month */ 
      SELECT AdSourceID 
        , DATEPART(MONTH, ReportDate) AS AdMonth 
        , DATEPART(YEAR, ReportDate) AS AdYear 
        , SUM(Conversions) AS Conversions 
      FROM AdReport 
      --- first of desired year (2016) to first of next year (exclusive) 
      WHERE ReportDate >= '2016-01-01' 
      AND ReportDate < '2017-01-01' 
      AND AdSourceID <> 4 
      GROUP BY AdSourceID 
        , DATEPART(MONTH, ReportDate) 
        , DATEPART(YEAR, ReportDate) 
     ) 
     ar ON ar.AdMonth = ab.AdMonth 
      AND ar.AdYear = ab.AdYear 
      AND ar.AdSourceID = ab.AdSourceID   
GROUP BY ab.AdMonth 
ORDER BY ab.AdMonth