If I have a MySQL table looking something like this:
company_name action pagecount ------------------------------- Company A PRINT 3 Company A PRINT 2 Company A PRINT 3 Company B EMAIL Company B PRINT 2 Company B PRINT 2 Company B PRINT 1 Company A PRINT 3
Is it possible to run a MySQL query to get output like this:
company_name EMAIL PRINT 1 pages PRINT 2 pages PRINT 3 pages ------------------------------------------------------------- CompanyA 0 0 1 3 CompanyB 1 1 2 0
The idea is that pagecount
can vary so the output column amount should reflect that, one column for each action
/pagecount
pair and then number of hits per company_name
. I'm not sure if this is called a pivot table but someone suggested that?
This basically is a pivot table.
A nice tutorial on how to achieve this can be found here: http://www.artfulsoftware.com/infotree/qrytip.php?id=78
I advise reading this post and adapt this solution to your needs.
Update
After the link above is currently not available any longer I feel obliged to provide some additional information for all of you searching for mysql pivot answers in here. It really had a vast amount of information, and I won't put everything from there in here (even more since I just don't want to copy their vast knowledge), but I'll give some advice on how to deal with pivot tables the sql way generally with the example from peku who asked the question in the first place.
Maybe the link comes back soon, I'll keep an eye out for it.
The spreadsheet way...
Many people just use a tool like MSExcel, OpenOffice or other spreadsheet-tools for this purpose. This is a valid solution, just copy the data over there and use the tools the GUI offer to solve this.
But... this wasn't the question, and it might even lead to some disadvantages, like how to get the data into the spreadsheet, problematic scaling and so on.
The SQL way...
Given his table looks something like this:
Now look into his/her desired table:
The rows (
EMAIL
,PRINT x pages
) resemble conditions. The main grouping is bycompany_name
.In order to set up the conditions this rather shouts for using the
CASE
-statement. In order to group by something, well, use ...GROUP BY
.The basic SQL providing this pivot can look something like this:
This should provide the desired result very fast. The major downside for this approach, the more rows you want in your pivot table, the more conditions you need to define in your SQL statement.
This can be dealt with, too, therefore people tend to use prepared statements, routines, counters and such.
Some additional links about this topic:
For dynamic pivot, use
GROUP_CONCAT
withCONCAT
. The GROUP_CONCAT function concatenates strings from a group into one string with various options.DEMO HERE
A stardard-SQL version using boolean logic:
SQL Fiddle.
How?
TRUE OR NULL
yieldsTRUE
.FALSE OR NULL
yieldsNULL
.NULL OR NULL
yieldsNULL
.And
COUNT
only counts non-null values. Voilá.Correct answer is:
For MySQL you can directly put conditions in
SUM()
function and it will be evaluated as Boolean0
or1
and thus you can have your count based on your criteria without usingIF/CASE
statementsDEMO