Jump to content

transform mysql table


tomasd

Recommended Posts

Hi,

I need some help or ideas please...

I've got a pretty large table consisting of 4 fields, there are 3 fields I'm particularly interested in 'check_date','price' and 'out_date'. If I select out_date,price,check_date from my table I'm getting similar output;

  out_date     price      check_date

| 2008-04-02 | 29.99 | 2008-2-20 |

| 2008-04-02 | 29.99 | 2008-2-20 |

| 2008-04-03 | 34.99 | 2008-2-20 |

| 2008-04-03 | 34.99 | 2008-2-20 |

| 2008-04-04 | 24.99 | 2008-2-20 |

| 2008-04-04 | 34.99 | 2008-2-20 |

| 2008-04-05 | 59.99 | 2008-2-20 |

| 2008-04-05 | 39.99 | 2008-2-20 |

| 2008-04-06 | 59.99 | 2008-2-20 |

| 2008-04-06 | 59.99 | 2008-2-20 |

| 2008-04-07 | 34.99 | 2008-2-20 |

| 2008-04-07 | 29.99 | 2008-2-20 |

| 2008-04-08 | 29.99 | 2008-2-20 |

| 2008-04-08 | 29.99 | 2008-2-20 |

| 2008-04-09 | 29.99 | 2008-2-20 |

| 2008-04-09 | 29.99 | 2008-2-20 |

| 2008-04-10 | 34.99 | 2008-2-20 |

| 2008-04-10 | 34.99 | 2008-2-20 |

| 2008-04-11 | 34.99 | 2008-2-20 |

| 2008-04-11 | 24.99 | 2008-2-20 |

| 2008-04-12 | 34.99 | 2008-2-20 |

| 2008-04-12 | 59.99 | 2008-2-20 |

| 2008-04-13 | 14.79 | 2008-2-20 |

| 2008-04-13 | 24.99 | 2008-2-20 |

| 2008-04-14 | 29.99 | 2008-2-20 |

| 2008-04-14 | 29.99 | 2008-2-20 |

| 2008-04-15 | 29.99 | 2008-2-20 |

| 2008-04-15 | 29.99 | 2008-2-20 |

| 2008-04-16 | 29.99 | 2008-2-20 |

| 2008-04-16 | 29.99 | 2008-2-20 |

| 2008-04-17 | 34.99 | 2008-2-20 |

| 2008-04-17 | 34.99 | 2008-2-20 |

| 2008-04-18 | 24.99 | 2008-2-20 |

| 2008-04-18 | 24.99 | 2008-2-20 |

| 2008-04-19 | 34.99 | 2008-2-20 |

| 2008-04-19 | 34.99 | 2008-2-20 |

| 2008-04-20 | 14.79 | 2008-2-20 |

| 2008-04-20 | 24.99 | 2008-2-20 |

| 2008-04-21 | 29.99 | 2008-2-20 |

| 2008-04-21 | 29.99 | 2008-2-20 |

| 2008-04-22 | 29.99 | 2008-2-20 |

| 2008-04-22 | 29.99 | 2008-2-20 |

| 2008-04-23 | 29.99 | 2008-2-20 |

| 2008-04-23 | 29.99 | 2008-2-20 |

| 2008-04-24 | 34.99 | 2008-2-20 |

| 2008-04-24 | 34.99 | 2008-2-20 |

| 2008-04-25 | 39.99 | 2008-2-20 |

| 2008-04-25 | 24.99 | 2008-2-20 |

| 2008-04-26 | 34.99 | 2008-2-20 |

| 2008-04-26 | 34.99 | 2008-2-20 |

| 2008-04-27 | 14.79 | 2008-2-20 |

| 2008-04-27 | 24.99 | 2008-2-20 |

| 2008-04-28 | 29.99 | 2008-2-20 |

| 2008-04-28 | 29.99 | 2008-2-20 |

| 2008-04-29 |  0.02 | 2008-2-20 |

| 2008-04-29 |  0.02 | 2008-2-20 |

| 2008-04-30 | 29.99 | 2008-2-20 |

| 2008-04-30 | 29.99 | 2008-2-20 |

| 2008-04-02 | 29.99 | 2008-2-21 |

| 2008-04-02 | 29.99 | 2008-2-21 |

| 2008-04-03 | 34.99 | 2008-2-21 |

| 2008-04-03 | 34.99 | 2008-2-21 |

| 2008-04-04 | 29.99 | 2008-2-21 |

| 2008-04-04 | 34.99 | 2008-2-21 |

| 2008-04-05 | 59.99 | 2008-2-21 |

| 2008-04-05 | 39.99 | 2008-2-21 |

| 2008-04-06 | 59.99 | 2008-2-21 |

| 2008-04-06 | 59.99 | 2008-2-21 |

| 2008-04-07 | 34.99 | 2008-2-21 |

| 2008-04-07 | 29.99 | 2008-2-21 |

| 2008-04-08 | 29.99 | 2008-2-21 |

| 2008-04-08 | 29.99 | 2008-2-21 |

| 2008-04-09 | 29.99 | 2008-2-21 |

| 2008-04-09 | 29.99 | 2008-2-21 |

| 2008-04-10 | 34.99 | 2008-2-21 |

| 2008-04-10 | 34.99 | 2008-2-21 |

| 2008-04-11 | 34.99 | 2008-2-21 |

| 2008-04-11 | 24.99 | 2008-2-21 |

| 2008-04-12 | 34.99 | 2008-2-21 |

| 2008-04-12 | 59.99 | 2008-2-21 |

| 2008-04-13 | 29.99 | 2008-2-21 |

| 2008-04-13 | 24.99 | 2008-2-21 |

| 2008-04-14 | 29.99 | 2008-2-21 |

| 2008-04-14 | 29.99 | 2008-2-21 |

| 2008-04-15 | 29.99 | 2008-2-21 |

| 2008-04-15 | 29.99 | 2008-2-21 |

| 2008-04-16 | 29.99 | 2008-2-21 |

| 2008-04-16 | 29.99 | 2008-2-21 |

| 2008-04-17 | 34.99 | 2008-2-21 |

| 2008-04-17 | 34.99 | 2008-2-21 |

| 2008-04-18 | 24.99 | 2008-2-21 |

| 2008-04-18 | 24.99 | 2008-2-21 |

| 2008-04-19 | 34.99 | 2008-2-21 |

| 2008-04-19 | 34.99 | 2008-2-21 |

| 2008-04-20 | 14.79 | 2008-2-21 |

| 2008-04-20 | 24.99 | 2008-2-21 |

| 2008-04-21 | 29.99 | 2008-2-21 |

| 2008-04-21 | 29.99 | 2008-2-21 |

| 2008-04-22 | 29.99 | 2008-2-21 |

| 2008-04-22 | 29.99 | 2008-2-21 |

| 2008-04-23 | 29.99 | 2008-2-21 |

| 2008-04-23 | 29.99 | 2008-2-21 |

| 2008-04-24 | 34.99 | 2008-2-21 |

| 2008-04-24 | 34.99 | 2008-2-21 |

| 2008-04-25 | 39.99 | 2008-2-21 |

| 2008-04-25 | 24.99 | 2008-2-21 |

| 2008-04-26 | 34.99 | 2008-2-21 |

| 2008-04-26 | 34.99 | 2008-2-21 |

| 2008-04-27 | 14.79 | 2008-2-21 |

| 2008-04-27 | 24.99 | 2008-2-21 |

| 2008-04-28 | 29.99 | 2008-2-21 |

| 2008-04-28 | 29.99 | 2008-2-21 |

| 2008-04-29 |  0.01 | 2008-2-21 |

| 2008-04-29 |  0.01 | 2008-2-21 |

| 2008-04-30 | 29.99 | 2008-2-21 |

| 2008-04-30 | 29.99 | 2008-2-21 |

 

Is there any way re-arranging fields so it looks like this:

   out_date     price_20 price_21

| 2008-04-02 | 29.99 | 29.99|

| 2008-04-02 | 29.99 | 29.99|

| 2008-04-03 | 34.99 | 34.99|

| 2008-04-03 | 34.99 | 34.99|

| 2008-04-04 | 24.99 | 29.99|

| 2008-04-04 | 34.99 | 34.99|

| 2008-04-05 | 59.99 | 59.99|

| 2008-04-05 | 39.99 | 39.99|

| 2008-04-06 | 59.99 | 59.99|

| 2008-04-06 | 59.99 | 59.99|

| 2008-04-07 | 34.99 | 34.99|

| 2008-04-07 | 29.99 | 29.99|

| 2008-04-08 | 29.99 | 29.99|

| 2008-04-08 | 29.99 | 29.99|

| 2008-04-09 | 29.99 | 29.99|

| 2008-04-09 | 29.99 | 29.99|

| 2008-04-10 | 34.99 | 34.99|

| 2008-04-10 | 34.99 | 34.99|

| 2008-04-11 | 34.99 | 34.99|

etc...

 

is that possible to do?

Any help appreciated!

Link to comment
Share on other sites

Thanks for the suggestion, but not quite.

I'm not sure but I think I need select within select. I tried following;

mysql> select out_date, (select price from STN_MJV where cast(check_date as date)='2008-02-20') as price_20, (select price from STN_MJV where cast(check_date as date)='2008-02-21') as price_21 from STN_MJV;

 

ERROR 1242 (21000): Subquery returns more than 1 row

Link to comment
Share on other sites

It works on a small table...

mysql> select * from STN_MJV;
+---------------------+------------+----------+--------+
| check_date          | out_date   | out_time | price  |
+---------------------+------------+----------+--------+
| 2007-12-20 11:36:24 | 2007-12-21 | 11:25:00 | 139.99 |
| 2007-12-21 11:36:24 | 2007-12-21 | 11:25:00 |  19.99 |
+---------------------+------------+----------+--------+
2 rows in set (0.00 sec)

mysql> select out_date, (select price from STN_MJV where cast(check_date as date)='2007-12-20') as price_20, (select price from STN_MJV where cast(check_date as date)='2007-12-21') as price_21 from STN_MJV;
+------------+----------+----------+
| out_date   | price_20 | price_21 |
+------------+----------+----------+
| 2007-12-21 |   139.99 |    19.99 |
| 2007-12-21 |   139.99 |    19.99 |
+------------+----------+----------+
2 rows in set (0.00 sec)

 

 

Link to comment
Share on other sites

I'm not sure ORDER BY is the right answer. What he's looking for is a pivot table of sorts, however I would like to know the following:

1) What is the other (4th) field

2) What is your key on the table? i.e. index

 

the reason I ask is that this data seems terribly unnormalised as first glance. The 4th field might help identify what's going on.

Link to comment
Share on other sites

Yeah I thought they were just duplicate entries, but if you have an extended look through the data you'll see that some aren't in fact duplicate rows, which thus leads me to believe that the 4th column is significant in identifying the row differences.

Link to comment
Share on other sites

Well... I wrote this php script that does data mining on one of budget airline website in UK. All works very well

Stage 1: curl query

Stage 2: regex on html

Stage 3: write flight date, flight time, flight price to mysql

stage 4: report <- working on that now.

 

Database structure

check_date is timestamp when data was written to the table

out_date is date of the flight  (I check like 300 days to the future from now[i can specify any range])

out_time is time of the flight

price is the price

mysql> describe STN_KUN;
+------------+--------------+------+-----+-------------------+-------+
| Field      | Type         | Null | Key | Default           | Extra |
+------------+--------------+------+-----+-------------------+-------+
| check_date | timestamp    | NO   |     | CURRENT_TIMESTAMP |       |
| out_date   | date         | NO   |     |                   |       |
| out_time   | time         | NO   |     |                   |       |
| price      | decimal(5,2) | YES  |     | NULL              |       |
+------------+--------------+------+-----+-------------------+-------+

 

I'm trying to draw graphs using jpgraph

$sql_getprice = "select price,check_date from STN_KUN where out_date='2008-03-18'";
$sql_query = mysql_query ($sql_getprice);
$ystack = array();
while($row = mysql_fetch_array($sql_query)){
  array_push($ystack,$row['price']);
}
$sql_query = mysql_query ($sql_getprice);
$xstack = array();
while($row = mysql_fetch_array($sql_query)){
  array_push($xstack,$row['check_date']);
}

 

Problem is that I can only have 1 line per graph, unless my mysql query is 'clever'. Currently I'm fetching like

check_date,price which is for a particular out_date. I want to be able to get check_date,price1 for day1, price2 for day 2, price 3 for day 3, price 4 for day 4 so I can have 4 lines on same graph.

 

See graph attached here http://img295.imageshack.us/img295/6721/graphcn0.gif

 

Any ideas on how to transform that table for jpgraph are greatly appreciated!

 

Cheers

Tom

 

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.