Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,360 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,253 people online right now. Registration is fast and FREE... Join Now!




Generating a pivot-style result from a MySQL 4 database

 
Reply to this topicStart new topic

Generating a pivot-style result from a MySQL 4 database, Making a product report

wzeller
post 28 Aug, 2008 - 11:16 AM
Post #1


New D.I.C Head

Group Icon
Joined: 9 Apr, 2006
Posts: 39



Dream Kudos: 25
My Contributions


I find myself in need of creating a specific report without the foggiest idea where to begin. I could pull the data as an external datasource in Excel or run a query directly at the MySql command prompt, but I'm not sure how to structure the query.

The data will be coming from two tables. Here are the relevant fields:

A table called ORDERS has fields ORDERID and DATE. In this table, ORDERID is a primary key.
A table called ORDER_DETAILS has fields ORDERID, PRODUCTCODE, and QUANTITY. ORDERID is not unique in this table: Each line item for a given order has it's own row and each rown within an order has the same ORDERID.

As far as table joins go, ORDERS.ORDERID=ORDER_DETAILS.ORDERID.

ORDERS.DATE is a UNIX-style timestamp.

I need to generate a report that shows the number of each product sold on each day. For instance, if my PRODUCTCODEs are "WIDGET1" thru "WIDGET5" and I'm running the report for the first week of this year, then the output should look something like this:
CODE

              WIDGET1  WIDGET2  WIDGET3  WIDGET4  WIDGET5
01/01/08         2        1        4        5        2
01/02/08         2        3        1        3        1
01/03/08         3        5        2        4        2
01/04/08         2        4        8        2        1
01/05/08         5        2        4        2        5
01/06/08         3        4        8        4        7
01/07/08         2        1        3        2        5


To make it slightly more complicated, it would be especially excellent to exclude dates on which any of the numbers are zero. (We're trying to figure out the percentage of sales generated by each product during times that no product is on backorder and therefore unavailable for sale. In this way, we'll be able to forecast sales and calculate proper order amounts for future orders.) Additional wrinkle: Since we also have discontinued products, I'm hoping to also make it also exclude dates on which WIDGET6 through WIDGETn have quantity sums greater than 0. Ugh. crying.gif (Our product codes aren't so nicely organized, either - each one needs to be specified individually since some are purely numeric and others are alphanumeric and they all come in no particular order. I blame my predecessor who had much to learn about data normalization.)

I know that the numbers will take some form of SUM(ORDER_DETAILS.QUANTITY) grouped by "PRODUCTCODE,DATE", but my experience with subqueries is limited at best and it seems like this is going to require some sort of nested subqueries.

To make things even more difficult, we're still running MySQL 4.1, which has more limited subquery capabilities than MySQL 5, and so this may not even be possible. If absolutely necessary, I can import all the data into a local system running MySQL 5 on a testbed, but I'd rather avoid that step if possible since I'd like this to be a report that we can generate on the fly in the future. (In fact, if I can get the query working in MySQL 4, then I'll be creating a web-based interface for it so that my users can run it whenever they feel like it for whatever date range they need. If it can only happen after exporting the data to another system, then that won't be possible which means I'll have do the export and run the report for them every time they feel like they need the data. Yuck.)

It MAY be far easier to do this as a data import in Excel and generate the table from there, but unfortunately that is another thing that I have only limited experience with and I would still need to figure out how to construct the query to group everything by date (from a UNIX timestamp, no less) and sum up the quantities for each productcode.

Any help would be GREATLY appreciated.

Thank you,
Wayne
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 28 Aug, 2008 - 11:54 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Don't know if this will work in MySql 4, but it should:
CODE

select b.`DATE`,
        sum(case when a.PRODUCTCODE='WIDGET1' then a.QUANTITY else 0 end) as WIDGET1,
        sum(case when a.PRODUCTCODE='WIDGET2' then a.QUANTITY else 0 end) as WIDGET2,
        sum(case when a.PRODUCTCODE='WIDGET3' then a.QUANTITY else 0 end) as WIDGET3
    from ORDER_DETAILS a
        inner join ORDERS b on a.ORDERID=b.ORDERID
    group by b.`DATE`


You'll need to figure out how your database will format or truncate the dates for you.

Hope this helps.
User is online!Profile CardPM

Go to the top of the page

wzeller
post 28 Aug, 2008 - 02:08 PM
Post #3


New D.I.C Head

Group Icon
Joined: 9 Apr, 2006
Posts: 39



Dream Kudos: 25
My Contributions


QUOTE(baavgai @ 28 Aug, 2008 - 12:54 PM) *

Don't know if this will work in MySql 4, but it should:

You'll need to figure out how your database will format or truncate the dates for you.

Hope this helps.


Wow. That's exceptionally cool. I was unaware of the CASE statement in SQL. Looking in the manuals, it appears that it was introduced to MySql in version 5, but this solution looks very good to me and I doubt that it's possible to emulate in version 4. So I'll run it on exported data for now and use this as one more argument for the need to upgrade.

Thank you VERY much!
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 28 Aug, 2008 - 05:02 PM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(wzeller @ 28 Aug, 2008 - 06:08 PM) *

Looking in the manuals, it appears that it was introduced to MySql in version 5


Nope, "CASE was added in MySQL 3.23.3" - 11.3. Control Flow Functions

If you don't want CASE, you can use IF() same way. Good luck.
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:56AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month