Grouping with operands question
Posted
by Filip
on Stack Overflow
See other posts from Stack Overflow
or by Filip
Published on 2009-09-15T11:56:33Z
Indexed on
2010/03/20
18:01 UTC
Read the original article
Hit count: 330
I have a table:
mysql> desc kursy_bid;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| datetime | datetime | NO | PRI | NULL | |
| currency | varchar(6) | NO | PRI | NULL | |
| value | varchar(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
I would like to select some rows from a table, grouped by some time interval (can be one day) where I will have the first row and the last row of the group, the max(value) and min(value).
I tried:
select
datetime,
(select value order by datetime asc limit 1) open,
(select value order by datetime desc limit 1) close,
max(value),
min(value)
from kursy_bid_test
where datetime > '2009-09-14 00:00:00'
and currency = 'eurpln'
group by
month(datetime),
day(datetime),
hour(datetime);
but the output is:
| open | close | datetime | max(value) | min(value) |
+--------+--------+---------------------+------------+------------+
| 1.4581 | 1.4581 | 2009-09-14 00:00:05 | 4.1712 | 1.4581 |
| 1.4581 | 1.4581 | 2009-09-14 01:00:01 | 1.4581 | 1.4581 |
As you see open and close is the same (but they shouldn't be). What should be the query to do what I want?
© Stack Overflow or respective owner