Jump to content

pivot table/inner join


raytri

Recommended Posts

I've been reading up on ways to turn columns into rows, but I'm just not able to turn it into working code.

 

I have a table with data that looks like this:

 

Account

Month

Transactions

100

May 2009

200

100

June 2009

180

101

May 2009

50

101

June 2009

80

101

July 2009

68

102

June 2009

400

 

Yes, I know this should be broken up into related tables. But this is what I have to work with.

 

I want to pull a report that displays the data so there is one line per account, and a column for each distinct month in the database, like so:

 

Account

May 2009

June 2009

July 2009

100

200

180

0

101

50

80

68

102

0

400

0

 

There are a lot of months, so I'd like the query to identify each distinct month, rather than me hardcoding it into the query.

 

I've looked at pivot tables, inner joins, etc., but I'm just not grokking what I need to do to break out the months into columns.

Link to comment
Share on other sites

Hmm. The link helped me get the query to work, but only by manually plugging in a SUM(IF()) statement for each month:

 

SELECT Account, 
SUM(IF(`Period Ending`='2009-12-31',`No_ Trans`,0)) As Dec09, 
SUM(IF(`Period Ending`='2010-01-31',`No_ Trans`,0)) As Jan10, 
SUM(IF(`Period Ending`='2010-03-31',`No_ Trans`,0)) As Mar10
FROM Merchant GROUP BY Account

 

There are quite a few months, with another being added every month. So is there a way to have the query do this dynamically? (create a column for each month in the table, and have each row show the number of transactions for that account in each month).

Link to comment
Share on other sites

So is there a way to have the query do this dynamically? (create a column for each month in the table...

 

that is why I pointed you first to page 5.... there is an example showing how to do that... the example use Perl... but the important thing are the concepts about how to build the query dynamically ... page 4 shows how to use CONCAT to build part of the query.... you can develop the same concept in PHP or in whatever you feel comfortable.

Link to comment
Share on other sites

I tried the CONCAT approach, but couldn't get it to work.

 

Again, I'm trying to do this totally in MySQL, without reference to PHP or anything like that. I want to be able to do this in direct queries against the database, not have to run it through a server-side script.

Link to comment
Share on other sites

the fact that the final query has a variable (dynamic) number of fields to be selected (the sum by month)  make no possible (imho) to build it using only direct queries... you must build the final query in some way... either manually or with some kind of programming... other alternatives to do this using only Mysql resources is use Stored Procedures or Functions

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.