Solved

Pivot table in ColdFusion

Posted on 2009-06-29
4
1,287 Views
Last Modified: 2012-05-07
I have this select statement:
<CFQUERY name="dataQuery_R1" datasource="#datasource#" result = "rptSQL">
   
SELECT 'ELECTRONIC' AS "MEDIA_TYPE"                                                  
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 1 AND 5 THEN 1 ELSE 0 END)    
    AS "ST_001_005"                                                  
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 1 AND 5 THEN A.Student_CNT ELSE 0 END)                  
     AS "ST_CUM_001_005"                                                  
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 6 AND 10 THEN 1 ELSE 0 END)    
     AS "ST_006_010"                                                  
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 6 AND 10 THEN A.Student_CNT ELSE 0 END)                        
     AS "ST_CUM_006_010"                                                

 ,SUM(CASE WHEN A.Student_CNT BETWEEN 11 AND 15 THEN 1 ELSE 0 END)
    AS "ST_011_015"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 11 AND 15 THEN A.Student_CNT ELSE 0 END)                        
    AS "ST_CUM_011_015"                                                
                                               
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 16 AND 20 THEN 1 ELSE 0 END)
    AS "ST_016_020"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 16 AND 20 THEN A.Student_CNT ELSE 0 END)                          
    AS "ST_CUM_016_020"  
                                               
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 21 AND 25 THEN 1 ELSE 0 END)
    AS "ST_021_025"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 21 AND 25 THEN A.Student_CNT ELSE 0 END)                        
    AS "ST_CUM_021_025"                                                
                                             
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 26 AND 30 THEN 1 ELSE 0 END)
    AS "ST_026_030"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 26 AND 30 THEN A.Student_CNT ELSE 0 END)                        
    AS "ST_CUM_026_030"                                                
                                               
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 31 AND 35 THEN 1 ELSE 0 END)
    AS "ST_031_035"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 31 AND 35 THEN A.Student_CNT ELSE 0 END)                          
    AS "ST_CUM_031_035"                                                
                                               
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 36 AND 40 THEN 1 ELSE 0 END)
    AS "ST_036_040"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 36 AND 40 THEN A.Student_CNT ELSE 0 END)                        
    AS "ST_CUM_036_040"        
                                                 
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 41 AND 45 THEN 1 ELSE 0 END)
    AS "ST_041_045"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 41 AND 45 THEN A.Student_CNT ELSE 0 END)                          
    AS "ST_CUM_041_045"                                                
                                               
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 46 AND 50 THEN 1 ELSE 0 END)
    AS "ST_046_050"                                                
 ,SUM(CASE WHEN A.Student_CNT BETWEEN 46 AND 50 THEN A.Student_CNT ELSE 0 END)                          
    AS "ST_CUM_046_050"                                                
                                                 
    FROM STUDENTS  A                                            
    WHERE STUDENT_STUS_CD = 'CMPLTE'
    AND  Student_YR = '2008';
</cfquery>
 I have result as one row, but I need to present this like:
Range       Stnt Cnt       Stnt Cumm       Count             Cumm
1 - 5              3,090       3,090       7,340             7,340
6 - 10       1,248       4,338       9,641             16,981
11 - 15           643       4,981       8,272             25,253
16 - 20          414       5,395       7,467             32,720
21 - 25         277       5,672       6,322             39,042
26 - 30         201       5,873       5,650             44,692
31 - 35        179       6,052       5,865             50,557
36 - 40       127       6,179       4,819             55,376
41 - 45       100       6,279       4,310             59,686
46 - 50        89       6,368       4,250             63,936
0
Comment
Question by:Albert3162
  • 2
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 24742114
I would start by making a separate record for each value

This will give the number of records and the cumulative sum of the counts by a 5 number range

Then you can easily display them in any way..

select floor(A.Student_CNT/5) as RangeStart
     , count(a.ID)         as Qty
     , sum(a.Student_CNT)  as cumm
from STUDENTS  a 
group by floor(A.Student_CNT/5)
WHERE STUDENT_STUS_CD = 'CMPLTE'
AND   Student_YR = '2008'
and   A.Student_CNT > 0

Open in new window

0
 

Author Comment

by:Albert3162
ID: 24745037
good idea but not clear for me.
where you take (a.ID) as Qty, this column not exists
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 24745090
substitute ID with whatever the primary key column name is for that table
0
 

Author Closing Comment

by:Albert3162
ID: 31597922
Good aproach
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
COLD FUSION 6 29
Cold Fusion Not Recognizing .NET DLL Updates 7 59
Apache error.log 19 72
How to get javascript to talk to coldfusion in < 3 page loads 4 57
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question