Larry:
>Hi,
Hi Larry,
[...]
>Here is how data is inserted into this table:
>
>INSERT INTO `OPT_FTSEMIB` (`ID`, `SDATE`, `SPOT`, `EXPIRY`, `TYPE`,
>`STRIKE`, `SETTLEMENT`) VALUES
> (1, '2012-08-20', 14971, 'DEC15', 'C',10000, 4473),
> (2, '2012-08-20', 14971, 'DEC15', 'C', 11000, 3893),
> (3, '2012-08-20', 14971, 'DEC15', 'C', 12000, 3332),
> (4, '2012-08-20', 14971, 'DEC15', 'C', 13000, 2858),
>...
> (912, '2012-08-21', 15330, 'DEC15', 'C', 10000, 13),
> (913, '2012-08-21', 15330, 'DEC15', 'C', 11000, 21),
> (914, '2012-08-21', 15330, 'DEC15', 'C', 12000, 30),
> (915, '2012-08-21', 15330, 'DEC15', 'C', 13000, 46),
[...]
>I would like to have data shown like this (where TYPE + STRIKE is
>name of column):
>
>.....SDATE...SPOT...EXPIRY...C10000...C11000...C12000...C13000
>2012-08-20...14971..DEC15....4473.....3893.....3332.....2858..
>2012-08-21...15330..DEC15....13.......21.......30.......46....
>
>Do you think this can actually be done?
Yes. Can be done.
I think.
It depends a bit on what situations can occur, and whether or not you
want to prevent rows from occuring multiple times.
>
>So I would basically like to SELECT SDATE, SPOT, EXPIRY and those 4
>columns (which are rows actually) FROM table WHERE EXPIRY="DEC15" AND
>TYPE="C"
My questions:
a) Do you _always_ have groups of exactly four rows for each
EXPIRY=value AND TYPE=value?
b) What would you want to have returned if rows were present like:
(982, '2012-08-21', 15330, 'DEC15', 'C', 10000, 13),
(983, '2012-08-21', 15330, 'DEC15', 'C', 10000, 14),
(984, '2012-08-21', 15330, 'DEC15', 'C', 10000, 15),
OR
(982, '2012-08-21', 15330, 'DEC15', 'C', 10000, 13),
(983, '2012-08-21', 15331, 'DEC15', 'C', 11000, 13),
(984, '2012-08-21', 15332, 'DEC15', 'C', 12000, 13),
OR EVEN
(982, '2012-08-21', 15330, 'DEC15', 'C', 10000, 13),
(983, '2012-08-21', 15330, 'DEC15', 'C', 10000, 13),
(984, '2012-08-21', 15330, 'DEC15', 'C', 10000, 13),
?
Depending on your answers, a solution could go into the direction of a
query with several subqueries (one for each additional STRIKE field
value):
SELECT
t1.sdate,
t1.spot,
t1.expiry,
t1.settlement AS c1,
(SELECT t2.settlement AS s2 FROM `OPT_FTSEMIB` t2
WHERE t2.expiry='DEC15' AND t2.type='C'
AND
t2.id>
t1.id AND t2.sdate=t1.sdate
ORDER BY
t2.id LIMIT 1) AS c2,
[...etc...two more times]
FROM `OPT_FTSEMIB` t1
WHERE t1.expiry='DEC15' AND t1.type='C'
ORDER BY <whatever order you like to have>
[Neither tested nor optimized. Just a first thought.
And be prepared to receive questions about the normalization of
your data model from several denizens here in the group]
--
Erick