2013-08-03 47 views
1

难以让sql从postgresql数据库中进行报告。来自日志表的Postgres日期报告

  • cohead:cohead_id,cohead_number,cohead_orderdate
  • 评论:COMMENT_ID,comment_source_id,COMMENT_DATE,COMMENT_TEXT

两个可我有两个表,有关列如下工作加入cohead_id = comment_source_id查找与订单相关的所有评论。

当我们向我们的订购准备服务提交订单时,我们通过插入带有相关订单的文本“已提交”的评论进行登录。当我们使用订单准备服务关闭订单时,我们为该订单插入“已开票”评论。

我想要做的是获得一个时间段(比如上个月)的每一天的列表,并计算当天或之前提交但尚未开具发票的订单数那天。

我遇到了一些概念问题,我尝试过的连接速度很慢。

任何想法?

+0

。 。你能提供一些样本数据和预期结果吗? –

回答

2

拍摄日期'20130731','20130805'作为示例的开始和结束日期,这个查询将每天在这两个日期之间返回您需要的数量。您可以更改真实查询的参数。

;with cte as (
    select d::date as d 
    from generate_series('20130731', '20130805', interval '1 day') as d 
) 
select 
    cte.d, 
    count(o.cohead_id) as cnt 
from cte 
    left outer join cohead as o on 
     o.cohead_orderdate <= cte.d and 
     not exists (
      select * 
      from comment as c 
      where 
       c.comment_date <= cte.d and 
       c.comment_text = 'Invoiced' and 
       c.comment_source_id = o.cohead_id 
     ) 
group by cte.d 
order by cte.d 

请参阅SQL FIDDLE EXAMPLE - 您可以添加/删除行并检查它是否正常工作。

希望有所帮助。

UPDATE: 如果你想获得提交日期,而不是订单日期,你不必来查询订单表都:

;with cte as (
    select d::date as d 
    from generate_series('20130731', '20130805', interval '1 day') as d 
), cte2 as (
    select 
     c1.comment_date as submitted_date, 
     c2.comment_date as invoiced_date, 
     count(*) as cnt 
    from comment as c1 
     left outer join comment as c2 on 
      c2.comment_source_id = c1.comment_source_id and 
      c2.comment_text = 'Invoiced' 
    where c1.comment_text = 'Submitted' 
    group by c1.comment_date, c2.comment_date 
) 
select c1.d, sum(c2.cnt) 
from cte as c1 
    left outer join cte2 as c2 on 
     c2.submitted_date <= c1.d and 
     (c2.invoiced_date is null or c2.invoiced_date > c1.d) 
group by c1.d 
order by c1.d 

看到SQL FIDDLE与更新的查询

更新2由于OP说他有查询性能的问题,我试着用窗口函数编写另一个。这个想法是获取所有类型提交的日期计数减去发票类型的评论,然后获得滚动总额。

;with cte1 as (
    select d::date as d 
    from generate_series('20130731', '20130805', interval '1 day') as d 
), cte2 as (
    select 
     greatest('20130731', c.comment_date) as comment_date, 
     c.comment_text, count(*) as cnt 
    from comment as c 
    where 
     c.comment_text in ('Invoiced', 'Submitted') and 
     c.comment_date <= '20130805' 
    group by greatest('20130731', c.comment_date), c.comment_text 
), cte3 as (
    select 
     coalesce(cs.cnt, 0) - coalesce(ci.cnt, 0) as cnt, 
     coalesce(cs.comment_date, ci.comment_date) as comment_date 
    from (select * from cte2 where comment_text = 'Submitted') as cs 
     full outer join (select * from cte2 where comment_text = 'Invoiced') as ci on 
      cs.comment_date = ci.comment_date 
) 
select c1.d, sum(c3.cnt) over (order by c1.d) 
from cte1 as c1 
    left outer join cte3 as c3 on c3.comment_date = c1.d 
order by c1.d 

SQL FIDDLE

+0

让我走上正确的轨道。我需要添加一个查询,而不仅仅是加入,所以我可以使用提交日期而不是订单日期(它们可以不同)。但逻辑起作用。仍然需要一段时间才能运行,但由于它可能是一夜之间更新,所以没什么大不了的。谢谢您的帮助! – Dustin

+0

没有问题,增加了另一个查询,可能我会帮你消除性能问题。 –

+0

@Dustin对于这样的大规模更新感到抱歉,但我认为可能与查询窗口功能将执行比以前的两个查询更好:)请参阅更新2 –