在infobright中对结果集进行分组

时间:2013-09-25 09:36:29

标签: mysql sql infobright

我正在用java编写一个程序来从infobright数据库中获取记录。以下是表格结构,查询和我需要的输出格式。

表格结构:

column name             type
------------          ----------
id                    integer
date                  integer
product_id            integer
search_engine_id      integer
visitor               integer

示例查询:

select   date,
         product_id, 
         search_engine_id, 
         sum(visitor)
from     report
where    date in (201300910, 20130919)
group by search_engine_id, 
         product_id, 
         date
order by product_id, 
         date

上述查询的结果集将采用以下格式:

+----------+-------------+----------------+------------------+
| date     | product_id | search_engine_id | sum(visitor)    |
+----------+-------------+----------------+------------------+
| 20130910 |      108   |              2 |                7  |
| 20130910 |      108   |              1 |                15 |
| 20130919 |      108   |              1 |                2  |
| 20130919 |      108   |              2 |                3  |
| 20130910 |      107   |              1 |                3  |
| 20130910 |      107   |              2 |                2  |
| 20130919 |      107   |              1 |                2  |
| 20130919 |      107   |              2 |                3  |
| 20130919 |      106   |              1 |                10 |

search_engine_id有两种类型,即1和2.

对于给定的一天,一个product_id最多可以存在两次,即search_engine_id 1的一个记录和search_engine_id的其他记录2.我想要的是将结果分组到product_id。所以它看起来像:

+-------------------+-------------+----------------+------------------+
| date              | product_id | search_engine_id | sum(visitor)    |
+-------------------+-------------+----------------+------------------+
| 20130910,20130919 |      108   |        2,1,1,2 |      7,15,2,3     |

在sql端执行此操作的主要原因是为每个product_id准备数据,以便在java端使用较少的内存。我尝试了group_contact功能,但似乎不支持infobright。有没有办法可以重写查询/使用其他一些函数来实现这个目的?

0 个答案:

没有答案