If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Need Help with a Pivot/Cross-tab query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-03-06, 13:59
pgraber pgraber is offline
Registered User
 
Join Date: Mar 2006
Posts: 3
Need Help with a Pivot/Cross-tab query

I have looked everywhere for an explanation; I'm stumped. Here's the problem:

Querying only 1 table.

Here's the basic table:

Year ----------AvgListPrice--------------SqFoot
2006 --------------2----------------------8
2007 ------------- 4-----------------------9
2008--------------3-----------------------4


I want the query to return results like this:

------------------- 2006----------- 2007-----------2008
AvgList Price---------2----------------4 ------------- 3
SqFoot ------------- 8--------------- 9 --------------4


This is only a couple of fields in the table; there are more. I've tried my things with CASE, IF, to no avail.

Any help or insight would be greatly appreciated. Thanks!
Reply With Quote
  #2 (permalink)  
Old 03-03-06, 14:55
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool


If your DBMS is NOT MS Access or SQL2k, then you can dump the table data to a CSV file and either load into an Excel spreadsheet or MS Access table and generate the pivot report.

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 03-03-06, 15:08
pgraber pgraber is offline
Registered User
 
Join Date: Mar 2006
Posts: 3
Building a Web-app / Neeed an SQL soluton

I am using the query and its results for a webpage. The data will not be exported.
Reply With Quote
  #4 (permalink)  
Old 03-03-06, 15:23
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool


Then you will have to "pivot" the data yourself based on a query similar to this:
Code:
SELECT year, SUM(AvgListPrice) AvgListPrice, SUM(SqFoot) SqFoot 
  FROM TheTable GROUP BY year;

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #5 (permalink)  
Old 03-03-06, 15:27
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
This is a job that is much better handled on the client side when possible. Depending on your web server environment (Cold Fusion, IIS/ASP, other) you probably have some client side tool that will do this both more efficiently and with less coding effort than trying to do it using SQL.

-PatP
Reply With Quote
  #6 (permalink)  
Old 03-03-06, 15:43
pgraber pgraber is offline
Registered User
 
Join Date: Mar 2006
Posts: 3
Pivot Followup

I just tried that, yet it doesn't give me the inddividual year colums; There are 3 years.

Using MySQL
Reply With Quote
  #7 (permalink)  
Old 03-03-06, 22:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
moving to mysql forum
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 03-03-06, 22:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the brute force way to get what you want is like this --
Code:
select 'AvgList Price'
     , sum(case when Year=2006
                then AvgListPrice
                else 0 end)   as '2006'
     , sum(case when Year=2007
                then AvgListPrice
                else 0 end)   as '2007'
     , sum(case when Year=2008
                then AvgListPrice
                else 0 end)   as '2008'
  from daTable
union all
select 'SqFoot'
     , sum(case when Year=2006
                then SqFoot
                else 0 end)   as '2006'
     , sum(case when Year=2007
                then SqFoot
                else 0 end)   as '2007'
     , sum(case when Year=2008
                then SqFoot
                else 0 end)   as '2008'
  from daTable
but i agree, you really don't want to be doing this with sql
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On