cfchart 中的变量值

variable value in cfchart

[已更新] 问题:
Leigh 提供的查询有效,但出于某种原因,当我替换行和列的名称以将查询从每次点击成本更改为每次注册成本和每个许可证成本时,我会得到与预期不同的值。

注意: 我在下面列出的结果仅针对每次注册的成本,而不是每次许可的成本。两者都来自同一个 table,因此如果一个是固定的,那么第二个很可能会效仿。我还更新了 AdReport table 以包括许可证列等。

如我所料

Date    CPR  
1      .35   
2      .42   
3      .85   
4      .48   
5      .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>

数据:

添加了示例数据,请忽略table中的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

抱歉 table 配置,不确定如何使它整洁。此外,我们有更多的转换(注册)和许可证未显示在示例数据中,只是碰巧前 ~10 行的数字较低。

how to set the chart up to chart them correctly

我实际上会从那个问题开始,而不是查询。从一个易于调整的小型硬编码图表开始,以便准确了解应如何构建标签以生成所需的图表。最终的分组将决定您应如何聚合查询数据。

假设您想要将 2016 年 1 月至 5 月的 "Cost per click" 和 "Cost per Registration" 绘制为单独的系列。创建这样的硬编码示例:

<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>

生成此图表:

查看 cfchartdata 标签,表明需要两个查询:一个包含按月的 "Cost per Click",另一个包含按月的 "Cost per Registration"。

要计算每次点击费用,请在月、年 源 ID 列中加入 AdBudgetAdClick。按月对结果进行分组,然后将 预算除以 点击次数:

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的结构。由于它包含每个 month/year/sourceID 组合的 多个 记录,因此您需要先按月计算总转化次数。然后将该结果加入到 AdBudget。所以 "Cost per Registration" 查询需要像这样:

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