?
Solved

Pivot table in ColdFusion

Posted on 2009-06-29
4
Medium Priority
?
1,304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 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