2014-06-06 46 views
1

我想要的輸出結果從具有這樣的結果的查詢,查詢= gettotalstars enter image description here按年份和月份使用組的cfoutput結果?

我使用<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> 
+0

作爲@邁克爾建議,優選如果可能的話,以在數據庫中聚集。 1)你的DBMS是什麼? 2)這些結果是否跨越多年(2013,2014,...)? – Leigh

回答

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

某些早期版本的CF不允許使用CFLOOP的組屬性。如果使用一個,只需切換到

+0

你是正確的,至少我的版本不允許這樣, – user3591637

+0

@GerryGurevich非常好的Gerry +1 upvote爲圓滑和評論的代碼! –

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

減號1.您有三個列標題和兩個數據列。另外你有硬編碼的月份名稱,他們可能是查詢結果。 –

2

你沒有提到你的DBMS。但是,如果您使用的是SQL Server 2005+,請考慮使用PIVOT。 PIVOT的優點是可以自動生成所有月份列,即「1月,2月......」。無需在輸出循環中處理丟失的月份。另外,聚合發生在數據庫中,這意味着將更少的數據/行拖入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 
+0

哇,我有Microsoft SQL Server Management Studio中\t \t \t \t \t \t 10.0.2531.0,SQL Server 2008中,將嘗試使用它,纔去尋找更多的到它 – user3591637

+0

這是一個很酷的解決方案從來沒有使用支點! –

+0

謝謝。 'PIVOT'是SQL Server 2005的一個很好的補充。在此之前,你必須使用舊的'CASE'黑客。它完成了工作,但是.. tres醜陋;-) – Leigh