Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 向SQL查询结果添加行_Postgresql - Fatal编程技术网

Postgresql 向SQL查询结果添加行

Postgresql 向SQL查询结果添加行,postgresql,Postgresql,我的Java应用程序中有一个自定义查询,如下所示: select to_char(search.timestamp,'Mon') as mon, COUNT(DISTINCT(search.ip_address)) from searches WHERE searches.city = 1 group by 1; 它应该返回数据库中发生的所有月份,以及每个月内不同IP地址的数量。但是,此时,某些月份没有任何条目,并且它们在SQL查询结果中丢失。如何确

我的Java应用程序中有一个自定义查询,如下所示:

select 
    to_char(search.timestamp,'Mon') as mon, 
    COUNT(DISTINCT(search.ip_address))
from 
    searches 
WHERE 
    searches.city = 1 
group by 1;
它应该返回数据库中发生的所有月份,以及每个月内不同IP地址的数量。但是,此时,某些月份没有任何条目,并且它们在SQL查询结果中丢失。如何确保所有月份都显示在那里,即使它们的计数为0

让它与:

select  
    to_char (gs.m,'Mon') as mon,  
    count (distinct search.ip_address) 
from
    generate_series (
          date_trunc('month', current_date - interval '11 month'),
          current_date,
          '1 month'
      ) gs (m)
    left join searches
    on date_trunc('month', search.timestamp) = gs.m AND search.city = 1
group by gs.m 
order by gs.m;
类似这样(未经测试):

如果你也想在那里呆上几年,试试这样(未经测试):

类似这样(未经测试):

如果你也想在那里呆上几年,试试这样(未经测试):


您需要一个包含月份的表作为基本表,然后将
搜索
加入其中。@bernie,这似乎是个好主意。我会把月份的名称存储在那里吗?时间戳对应于给定年份的月份(例如20162017年的所有月份等)。当然,最好存储月份和年份的名称。
distinct
不是一个函数。不需要括号
distinct a
distinct(a)
@a_horse__,没有名字谢谢你的提示!您需要一个包含月份的表作为基本表,然后将
搜索
加入其中。@bernie,这似乎是个好主意。我会把月份的名称存储在那里吗?时间戳对应于给定年份的月份(例如20162017年的所有月份等)。当然,最好存储月份和年份的名称。
distinct
不是一个函数。不需要括号
distinct a
distinct(a)
@a_horse__,没有名字谢谢你的提示!此方法是否可以从最早的月份开始按顺序生成最后12个月?@Coldoaldo,此方法仅返回一个月->“Mar->count”的表。我希望看到类似这样的内容:“四月->计数”、“五月->计数”、“六月->计数”等:(-这背后的业务逻辑:我需要查看月份,即使其中没有记录。此方法是否可以从最早的月份开始按序生成最后12个月?@Coldoaldo,这只返回一个月的表->“三月->计数”。我希望看到类似这样的内容:“四月->计数”,“五月->计数”,“六月->计数”等:(-这背后的业务逻辑:我需要查看月份,即使其中没有记录。
select 
    months.mon
    , COUNT(DISTINCT(searchs.ip_address))
from 
    (select 
        to_char(searches.timestamp,'Mon') as mon
     from 
        searches
     group by 1
    ) months
    left join searches 
    on to_char(searchs.timestamp,'Mon') = months.mon
    and searches.city = 1 
group by 1;
select 
    months.mon
    , COUNT(DISTINCT(searchs.ip_address))
from 
    (select 
        extract(year from searches.timestamp) as yr
        , to_char(searches.timestamp,'Mon') as mon
        , to_char(yr,'9999') || mon yrmon
     from 
        searches
     group by 1
    ) months
    left join searches 
    on to_char(extract(year from searches.timestamp),'9999' || 
        to_char(searchs.timestamp,'Mon') = months.yrmon
    and searches.city = 1 
group by 1;
select 
    to_char (gs.m,'Mon') as mon, 
    count (distinct(search.ip_address))
from 
    searches
    right join
    generate_series (
        date_trunc('month', current_date - interval '1 year'),
        current_date,
        '1 month'
    ) gs (m) on date_trunc('month', search.timestamp) = gs.m
where searches.city = 1 
group by gs.m
order by gs.m;