![]() | |
#1
| |||
| |||
|
|
time_iso | type | +---------------------+------+ 2011-08-04 00:31:19 | S | 2011-08-04 01:05:07 | B | 2011-08-04 08:49:07 | B | 2011-08-04 20:22:55 | V | 2011-08-05 00:22:14 | S | 2011-08-05 01:15:01 | V | +---------------------+------+ |
|
time_iso | S | B | V | tot | +---------------------+---+---+---+-----+ 2011-08-04 00:31:19 | 1 | 2 | 1 | 4 | 2011-08-05 00:22:14 | 1 | | 1 | 2 | +---------------------+---+---+---+-----+ |
|
DROP DATABASE if exists testdb; CREATE DATABASE testdb CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE testdb; CREATE TABLE `mytable` ( time_iso TIMESTAMP NOT NULL DEFAULT 0, type text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `mytable` VALUES ('2011-08-04 00:31:19', 'S'); INSERT INTO `mytable` VALUES ('2011-08-04 01:05:07', 'B'); INSERT INTO `mytable` VALUES ('2011-08-04 08:49:07', 'B'); INSERT INTO `mytable` VALUES ('2011-08-04 20:22:55', 'V'); INSERT INTO `mytable` VALUES ('2011-08-05 00:22:14', 'S'); INSERT INTO `mytable` VALUES ('2011-08-05 01:15:01', 'V'); |
#2
| |||
| |||
|
|
Hi, I have a simple table with some data looking like this: mysql> select * from mytable; +---------------------+------+ | time_iso | type | +---------------------+------+ | 2011-08-04 00:31:19 | S | | 2011-08-04 01:05:07 | B | | 2011-08-04 08:49:07 | B | | 2011-08-04 20:22:55 | V | | 2011-08-05 00:22:14 | S | | 2011-08-05 01:15:01 | V | +---------------------+------+ 6 rows in set (0.00 sec) Notice that the data shown is from two separate dates. I would like to get a "summary" result from a query (grouped by date) in the following way: +---------------------+---+---+---+-----+ | time_iso | S | B | V | tot | +---------------------+---+---+---+-----+ | 2011-08-04 00:31:19 | 1 | 2 | 1 | 4 | | 2011-08-05 00:22:14 | 1 | | 1 | 2 | +---------------------+---+---+---+-----+ 2 rows in set (0.00 sec) Is that at all possible to do (in one query)? If it is, any help on how the query should be constructed is much welcome... // Queux DROP DATABASE if exists testdb; CREATE DATABASE testdb CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE testdb; CREATE TABLE `mytable` ( time_iso TIMESTAMP NOT NULL DEFAULT 0, type text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `mytable` VALUES ('2011-08-04 00:31:19', 'S'); INSERT INTO `mytable` VALUES ('2011-08-04 01:05:07', 'B'); INSERT INTO `mytable` VALUES ('2011-08-04 08:49:07', 'B'); INSERT INTO `mytable` VALUES ('2011-08-04 20:22:55', 'V'); INSERT INTO `mytable` VALUES ('2011-08-05 00:22:14', 'S'); INSERT INTO `mytable` VALUES ('2011-08-05 01:15:01', 'V'); |
|
date(time_iso) | group_concat(type) | +----------------+--------------------+ 2011-08-04 | S,B,B,V | 2011-08-05 | S,V | +----------------+--------------------+ |
|
d | S | B | V | tot | +------------+------+------+------+------+ 2011-08-04 | 1 | 2 | 1 | 4 | 2011-08-05 | 1 | 0 | 1 | 2 | +------------+------+------+------+------+ |
#3
| |||
| |||
|
|
Hi, I have a simple table with some data looking like this: mysql> select * from mytable; +---------------------+------+ | time_iso | type | +---------------------+------+ | 2011-08-04 00:31:19 | S | | 2011-08-04 01:05:07 | B | | 2011-08-04 08:49:07 | B | | 2011-08-04 20:22:55 | V | | 2011-08-05 00:22:14 | S | | 2011-08-05 01:15:01 | V | +---------------------+------+ 6 rows in set (0.00 sec) Notice that the data shown is from two separate dates. I would like to get a "summary" result from a query (grouped by date) in the following way: +---------------------+---+---+---+-----+ | time_iso | S | B | V | tot | +---------------------+---+---+---+-----+ | 2011-08-04 00:31:19 | 1 | 2 | 1 | 4 | | 2011-08-05 00:22:14 | 1 | | 1 | 2 | +---------------------+---+---+---+-----+ 2 rows in set (0.00 sec) Is that at all possible to do (in one query)? If it is, any help on how the query should be constructed is much welcome... // Queux DROP DATABASE if exists testdb; CREATE DATABASE testdb CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE testdb; CREATE TABLE `mytable` ( time_iso TIMESTAMP NOT NULL DEFAULT 0, type text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `mytable` VALUES ('2011-08-04 00:31:19', 'S'); INSERT INTO `mytable` VALUES ('2011-08-04 01:05:07', 'B'); INSERT INTO `mytable` VALUES ('2011-08-04 08:49:07', 'B'); INSERT INTO `mytable` VALUES ('2011-08-04 20:22:55', 'V'); INSERT INTO `mytable` VALUES ('2011-08-05 00:22:14', 'S'); INSERT INTO `mytable` VALUES ('2011-08-05 01:15:01', 'V'); |
|
date(time_iso) | group_concat(type) | +----------------+--------------------+ 2011-08-04 | S,B,B,V | 2011-08-05 | S,V | +----------------+--------------------+ |
|
d | S | B | V | tot | +------------+------+------+------+------+ 2011-08-04 | 1 | 2 | 1 | 4 | 2011-08-05 | 1 | 0 | 1 | 2 | +------------+------+------+------+------+ |
#4
| |||
| |||
|
|
select d, sum(IF(type='S',C,0)) S, sum(IF(type='B',C,0)) B, sum(IF(type='V',1,0)) V, sum(C) tot from (select date(time_iso) d, type, count(type) C from mytable group by date(time_iso), type)x group by d; |
#5
| |||
| |||
|
|
Luuk <Luuk (AT) invalid (DOT) lan> wrote: select d, sum(IF(type='S',C,0)) S, sum(IF(type='B',C,0)) B, sum(IF(type='V',1,0)) V, sum(C) tot from (select date(time_iso) d, type, count(type) C from mytable group by date(time_iso), type)x group by d; Why so complicated? SELECT time_iso, SUM(IF(type='S', 1, 0)) AS S, SUM(IF(type='B', 1, 0)) AS B, SUM(IF(type='V', 1, 0)) AS V, COUNT(*) AS tot FROM mytable GROUP BY DATE(time_iso); XL |

#6
| |||
| |||
|
|
On 06-08-2011 11:19, Axel Schwenke wrote: Luuk <Luuk (AT) invalid (DOT) lan> wrote: select d, sum(IF(type='S',C,0)) S, sum(IF(type='B',C,0)) B, sum(IF(type='V',1,0)) V, sum(C) tot from (select date(time_iso) d, type, count(type) C from mytable group by date(time_iso), type)x group by d; Why so complicated? SELECT time_iso, SUM(IF(type='S', 1, 0)) AS S, SUM(IF(type='B', 1, 0)) AS B, SUM(IF(type='V', 1, 0)) AS V, COUNT(*) AS tot FROM mytable GROUP BY DATE(time_iso); XL When i read the original questionn, my first thought was.... GROUP_CONCAT(.... But i should look into second thoughts more often ![]() |
|
D | G | +------------+----------+ 2011-08-04 | B2,S1,V1 | 2011-08-05 | S1,V1 | +------------+----------+ |
#7
| |||
| |||
|
|
On 06-08-2011 11:51, Luuk wrote: On 06-08-2011 11:19, Axel Schwenke wrote: SELECT time_iso, SUM(IF(type='S', 1, 0)) AS S, SUM(IF(type='B', 1, 0)) AS B, SUM(IF(type='V', 1, 0)) AS V, COUNT(*) AS tot FROM mytable GROUP BY DATE(time_iso); I was not sure if S,B and V where the only type's that existed. |
|
But its hard to make such a query without limiting 'type' to only these 3 values. |
#8
| |||
| |||
|
|
Luuk <Luuk (AT) invalid (DOT) lan> wrote: On 06-08-2011 11:51, Luuk wrote: On 06-08-2011 11:19, Axel Schwenke wrote: SELECT time_iso, SUM(IF(type='S', 1, 0)) AS S, SUM(IF(type='B', 1, 0)) AS B, SUM(IF(type='V', 1, 0)) AS V, COUNT(*) AS tot FROM mytable GROUP BY DATE(time_iso); I was not sure if S,B and V where the only type's that existed. Neither was I. But it's not specified so I chose the easiest solution that matches the required result. But its hard to make such a query without limiting 'type' to only these 3 values. If you want the `tot` column to be the exact sum of S+B+V, then add WHERE type IN ('S', 'B', 'V') |

| And just to mention it: SELECT time_iso ... GROUP BY DATE(time_iso) works in MySQL only; because `time_iso` is neither an aggregate nor in the GROUP BY list. MySQL will chose a (more or less) random value for time_iso from the group. Other RDBMS will reject this query. Again I wrote the query like so because it's consistent with the requirements (though not completely specified). |
![]() |
| Thread Tools | |
| Display Modes | |
| |