我在postgresql上有一个大表,我希望得到多个结果集作为SELECT
查询的输出,如果可能的话?
我的表格如下:
select * from dns_table
id | dns_name | ip_source | mac_source | query_time
-----+----------------+------------+-------------------+----------------------------
1 | google.com | /10.0.0.1 | 00-00-00-00-00-01 | 2014-05-12 10:03:03.503938
2 | facebook.com | /10.0.0.1 | 00-00-00-00-00-01 | 2014-05-12 10:03:04.567417
3 | google.com | /10.0.0.5 | 00-00-00-00-00-05 | 2014-05-12 10:03:05.372453
4 | youtube.com | /10.0.0.1 | 00-00-00-00-00-01 | 2014-05-12 10:03:05.636633
5 | facebook.com | /10.0.0.5 | 00-00-00-00-00-05 | 2014-05-12 10:03:06.420067
6 | yahoo.com | /10.0.0.1 | 00-00-00-00-00-01 | 2014-05-12 10:03:06.685871
7 | google.com | /10.0.0.6 | 00-00-00-00-00-06 | 2014-05-12 10:03:07.198905
8 | youtube.com | /10.0.0.5 | 00-00-00-00-00-05 | 2014-05-12 10:03:07.524503
9 | wikipedia.org | /10.0.0.1 | 00-00-00-00-00-01 | 2014-05-12 10:03:07.747143
10 | facebook.com | /10.0.0.6 | 00-00-00-00-00-06 | 2014-05-12 10:03:08.287584
11 | yahoo.com | /10.0.0.5 | 00-00-00-00-00-05 | 2014-05-12 10:03:08.572827
...
...
我想要的是:
SELECT
count(dns_name) as hits,
dns_name,
to_char(query_time - interval '1 month','YYYY,MM,DD,HH24,MI') as qt
FROM dns_table
where dns_name like '**google.com**'
group by dns_name, qt
order by qt;
hits | dns_name | qt
------+------------+------------------
61 | google.com | 2014,04,12,10,03
40 | google.com | 2014,04,12,15,05
45 | google.com | 2014,04,13,09,34
因此,我想知道是否可以将 google.com 替换为参数,以获取每个dns_name
(google.com
的结果集, facebook.com
,yahoo.com
,...)。
不要犹豫任何问题!
答案 0 :(得分:0)
您无法获得多个结果集。您将不得不从您的申请中询问每个系列。 Fisrt得到所有的名字:
select distinct dns_name
from dns_table
order by dns_name;
然后要求将名称作为参数传递的系列:
select
count(dns_name) as hits,
dns_name,
date_trunc('minute', query_time) as qt
from dns_table
where
query_time > now() - interval '1 month'
and
dns_name = :dns_name
group by qt
order by qt;