如何获取一段时间内所有周的列表

时间:2016-03-15 22:48:32

标签: sql postgresql

我试图在一段时间内获得所有周的列表。我设法创建了以下查询:

select author_name, to_char((author_date)::date, 'day') AS COMMIT_DAY, COUNT(author_email) AS COMMITS_PER_DAY
from commits 
where author_email='abc@xyz.com'
group by author_name, commit_day
order by commits_per_day DESC;

这会产生如下输出:

"Abc"; "wednesday"; 1700
"Abc"; "friday   "; 1685
"Abc"; "thursday "; 1677
"Abc"; "tuesday  "; 1669
"Abc"; "monday   "; 1566
"Abc"; "sunday   "; 1167
"Abc"; "saturday "; 1151

我获得了该时间段(超过5年)之间每天的总提交次数。但是,如何在这段时间之间获得独特的周数和提交?

表格列为:

  

id | author_name | author_email | author_date(timestamp)|   total_lines

1 个答案:

答案 0 :(得分:0)

或者,我没有得到它,或者你正在思考它。 只按周分组

select author_name, to_char((author_date)::date, 'week') AS COMMIT_WEEK, COUNT(author_email) AS COMMITS_PER_DAY
from commits 
where author_email='abc@xyz.com'
group by author_name, commit_week
order by commits_per_day DESC;

如果你想要每年独特的一周,你还必须按年分组:

select author_name, to_char((author_date)::date, 'week') AS COMMIT_WEEK, to_char((author_date)::date, 'year') AS COMMIT_YEAR, COUNT(author_email) AS COMMITS_PER_DAY
from commits 
where author_email='abc@xyz.com'
group by author_name, commit_week, commit_year
order by commits_per_day DESC;