cfoutput结果按年份和月份使用组?

时间:2014-06-06 06:13:34

标签: coldfusion

我想从查询中输出结果,结果如下,query = gettotalstars enter image description here

我使用<cfoutput query="GetTotalStars" group="month"> 但这会为所有人重复输出。 有没有其他方法可以不使用“组”或更好的方法来做到这一点?

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

enter image description here

以下是我现在正在使用的代码:

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

3 个答案:

答案 0 :(得分:5)

之前的回复中有很多争议。我将尝试根据您展示的内容提供最简单的解决方案。如果您有多年或者如果您在任何给定月份都有空数据,那么您需要为自己解决许多问题。在试图引导你理解而不实际完成所有工作的同时,我实际上已经为你提供了这一系列事实的精确解决方案。您需要调整并退回其中一些以处理数据中的漏洞或其他可能的边缘情况。总而言之,它应该引导您采用可以构建的方法来处理这些其他可能性。它确实使用查询查询来梳理标题行。我认为这是导致更复杂解决方案的部分。

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

答案 1 :(得分:2)

您没有提及您的DBMS。但是,如果您使用的是SQL Server 2005+,请考虑使用PIVOT。 PIVOT的一个优点是你可以生成所有月份列,即&#34; 1月,2月,...&#34;自动。无需在输出循环中处理丢失的月份。此外,聚合发生在数据库中,这意味着将更少的数据/行拉入CF.

尽管如此,这类报告的所有选项都有利有弊。 PIVOT的一个缺点是它更坚固,更难定制。

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

 DEPT_ID | CSEDEPT_NAME | YEARNO | JANUARY | FEBRUARY | MARCH | ...

只需运行您的cfquery即可。然后循环遍历它并像往常一样输出所有查询列。


SQLFiddle

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

答案 2 :(得分:-5)

这可以提供帮助:

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