0

我不确定是否已经存在这样的问题,找不到我的解决方案,如果这是重复的,很抱歉:我有一张带日期的表格:

  |date (date)| tax (numeric) |  (stuff in brackets is the type)
  |2012-12-12 | 5.00          |
  |2012-12-12 | 10.00         |
  |2012-12-13 | 2.00          |

我希望我的输出看起来像这样:

  |date (date)| tax (numeric) |  (stuff in brackets is the type)
  |2012-12-12 | 15.00         |
  |2012-12-13 | 2.00          |

我正在考虑进行 CTE 类型查询,因为在数据库中,我将内容存储为没有时区的日期时间,以及一堆税这是我查询的开始:

 with date_select as
 (
 select CAST(r.datetime as DATE), sum(r.tax) as tax
 from suezensalon.receipt r
 where r.datetime between '2012-12-12 00:00:00' and '2012-12-18 23:59:59'
 group by r.datetime  
 order by r.datetime
 )

这给了我最高的桌子。做这个的最好方式是什么?是通过“平均日期”吗?

4

2 回答 2

1

这就是最终的工作:

 with date_select as
 (
 select CAST(r.datetime as DATE), sum(r.tax) as tax
 from suezensalon.receipt r
 where r.datetime between '2012-12-12 00:00:00' and '2012-12-18 23:59:59'
 group by r.datetime  
 order by r.datetime 
 ) 
 select extract(Month from datetime) || '-' || extract(Day from datetime) || '-' ||   extract(Year from datetime) as Date, sum(tax)
 from date_select
 group by Date
order by Date;
于 2012-12-21T02:37:10.010 回答
0

我认为最简单的选项,并且最有可能在存在索引的情况下表现良好,是这样的:

SELECT
    datetime::date as "Date",
    sum(tax)
  FROM suezensalon.receipt
  WHERE datetime >= '2012-12-12 00:00:00'
    AND datetime <  '2012-12-19 00:00:00'
  GROUP BY datetime::date
  ORDER BY "Date";
于 2012-12-21T04:18:27.180 回答