Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coldfusion CFO使用组按年度和月份输出结果?_Coldfusion - Fatal编程技术网

Coldfusion CFO使用组按年度和月份输出结果?

Coldfusion CFO使用组按年度和月份输出结果?,coldfusion,Coldfusion,我想从具有如下结果的查询中输出结果,query=gettotalstars 我使用 但这会对所有人重复输出。 有没有其他方法不使用“组”或更好的方法 最后,我希望它像这样显示输出:显然,从我现在设置表格的方式来看,它不会以我想要的方式给我结果 下面是我现在使用的代码: <cfoutput query="GetTotalStars" group="month"> <table cellpadding="0" cellspacing="0" class="tablesort

我想从具有如下结果的查询中输出结果,query=gettotalstars

我使用 但这会对所有人重复输出。 有没有其他方法不使用“组”或更好的方法

最后,我希望它像这样显示输出:显然,从我现在设置表格的方式来看,它不会以我想要的方式给我结果

下面是我现在使用的代码:

<cfoutput query="GetTotalStars" group="month">   
<table cellpadding="0" cellspacing="0" class="tablesorter">  
  <thead><th></th><th>January</th><th>Frebruary</th>......</thead>  
  <tbody>  
  <cfloop query="GetTotalStars" >  
  <tr>  
  <td>#csedept_name#</td>  
  <td><div align="right">#TOTALSTARS#</div></td>  
  </tr>  
  </cfloop>  
  </tbody>  
  </cfoutput>  

  </table> 
这有助于:

<table border="1" cellpadding="5">
<tr><th>Dep</th><th>May</th><th>June</th></tr>
<cfoutput query="GetTotalStars" group="dept_id">
<tr><td>#csedept_name#</td><cfoutput><td>#totalstars#</td></cfoutput></tr>

</cfoutput>
</table>

在之前的回答中有很多争议。我将根据您所展示的内容,尝试提出最简单的解决方案。如果您有多年的工作经验,或者您有任何给定月份的空数据,那么您必须自己解决一些问题。虽然我试图引导你们去理解而不是去做所有的工作,但事实上我已经给了你们一个关于这组事实的精确的解决方案。为了处理数据中的漏洞或其他可能的边缘情况,您需要调整并取消其中的一些设置。总之,它应该引导您找到一种方法,您可以在此基础上处理这些其他可能性。它确实使用query的查询来梳理标题行。我认为这是导致更复杂解决方案的部分

<cfscript>
    GetTotalStars = querynew(
        "dept_id,csedept_name,totalstars,year,month",
        "integer, varChar, integer, integer, integer",
        [
            {
            dept_id:1
            ,csedept_name:'department 1'
            ,totalstars:5
            ,year:2014
            ,month:5
            }
            ,{
            dept_id:1
            ,csedept_name:'department 1'
            ,totalstars:4
            ,year:2014
            ,month:6
            }
            ,{
            dept_id:2
            ,csedept_name:'department 2'
            ,totalstars:3
            ,year:2014
            ,month:5
            }
            ,{
            dept_id:2
            ,csedept_name:'department 2'
            ,totalstars:6
            ,year:2014
            ,month:6
            }
        ]
    );

writedump(GetTotalStars);
</cfscript>


<!---
    I agree with previous arguments about exessive use of query of queries.
    But for your header row (and to get a full list of possible months), you
    really need to pull that out of the original query in a single list or array or struct
--->

<cfquery dbtype="query" name="qryMonths">
    select distinct [month] from GetTotalStars order by [month]
</cfquery>


<cfoutput>

    <table border="1">
    <thead>
        <tr>
            <th>Department</th>

            <!---
                If you have gaps in the data, it might make sense to build an
                array here to keep track of what month is in what column
             --->
            <cfloop query="qryMonths">
                <th>#MonthAsString(qryMonths.month)#</th>
            </cfloop>
        </tr>
    </thead>
        <tbody>
            <!---
                This is just a straightforward nested query and group loop.
                If you have nulls in the data, you would check against the array
                that you built in the top section to figure out which column you
                are in
             --->
            <cfloop query="GetTotalStars" group="csedept_name">
                <tr>
                    <td>#GetTotalStars.csedept_name#</td>
                    <cfloop group="month">
                        <td>#GetTotalStars.totalstars#</td>
                    </cfloop>
                </tr>
            </cfloop>
        </tbody>
    </table>
</cfoutput>

你没有提到你的数据库管理系统。但是,如果使用SQL Server 2005 +,请考虑使用。PIVOT的一个优点是您可以生成所有月份列,例如一月、二月。。。自动地不需要在输出循环中处理丢失的月份。此外,聚合发生在数据库中,这意味着将更少的数据/行拉入CF

也就是说,这类报告的所有选项都有利弊。PIVOT的一个缺点是它有点僵硬,更难定制

下面是一个使用虚拟表的快速示例。它生成一个包含部门详细信息的结果集,每个月加一列:

 DEPT_ID | CSEDEPT_NAME | YEARNO | JANUARY | FEBRUARY | MARCH | ...
只需运行您的cfquery。然后循环遍历它并像往常一样输出所有查询列


减1。您有三个列标题和两个数据列。此外,您还有硬编码的月份名称,它们可能是查询结果。正如@Michael所建议的,如果可能,最好在数据库中聚合。1您的数据库管理系统是什么?2这些结果是否跨越多年20132014,…?CF的一些早期版本不允许使用CFLOOP的group属性。如果使用一个,只要切换到你是正确的,好吧,至少我的版本不允许这样,@GerryGurevich Very nice Gerry+1 upvote为光滑和注释代码!这对于提出的问题没有问题。但是,可能有必要对跨越1个日历年以上的报表执行某些操作。@user3591637如果去掉没有任何属性的最外层标记,则此代码将在旧版本中运行。然后用cfoutput替换每个cfloop,并且不要更改属性。应该可以正常工作。哇,我有Microsoft SQL Server Management Studio 10.0.2531.0,SQL Server 2008,将尝试使用它,在进一步研究它之前不要使用pivot。PIVOT是SQLServer2005的一个不错的补充。在此之前,你必须使用旧的案件黑客。它完成了任务,但是。。丑陋-@user3591637-如果您遇到关于如何将当前查询插入--dummy表以模拟。。。部分,让我知道。@leigh你知道我在哪里可以找到一个关于我如何才能做到这一点的托图里亚吗?我更改查询以获取我需要的数据want@user3591637-我回答中的链接解释了如何使用PIVOT。理论上,只需替换注释下面的select--虚拟表。。。与您当前的加入。如果遇到问题,请将表结构和当前查询发布到,以便我们可以查看,并解释如何修改它。
SELECT *
FROM  (
        --- dummy table to simulate the current query
        SELECT dept_id
          , csedept_name
          , datePart(yyyy, ratingDate) AS YearNo
          , dateName(mm, ratingDate) AS MonthName
          , totalStars
        FROM  yourResults
      ) 
      AS r
      PIVOT
      (
        --- Using month names for clarity and demo purposes, but 
        --- you could just as easily use month numbers instead
        SUM(totalStars)
        FOR [MonthName] IN (
            [January],[February],[March],[April]
            , [May],[June],[July],[August], [September]
            , [October],[November],[December]
           )
      ) 
      AS pvt