简体   繁体   English

从周日开始按周分组

[英]Group by Week beginning Sunday

I have a table with eventdatetime , userid etc. The data is inserted in the table daily. 我有一个带有eventdatetime,userid等的表。每天将数据插入到表中。

For the report , I need to give count of userid , projectid grouped by week : Tue-Mon for a month range at a time. 对于报告,我需要按周:Tue-Mon分组,一次提供一个月范围内的userid,projectid计数。

I need help on grouping the data by week for month. 我需要每周按月对数据进行分组的帮助。 I'm using Oracle. 我正在使用Oracle。

select count(distinct( table1.projectid))as Projects, count(distinct( table2.userid)) as Users,?? 
from table1
join table 2
on table1.a= table2.a
where table1.e='1'
and  table1.eventdatetime between sysdate-30 and sysdate-1  

group by ?? 通过...分组 ??

I want the output to be grouped by week like : WeekBegin 2013-04-14 我希望将输出按周进行分组,如:WeekBegin 2013-04-14

2013-04-21 2013-04-21

http://www.techonthenet.com/oracle/functions/to_char.php Use the To_Char function with IW to get the week. http://www.techonthenet.com/oracle/functions/to_char.phpIW使用To_Char函数来获取星期。 Then you can GROUP BY that IW value. 然后,您可以GROUP BYIW值。

Note that the date the Oracle week starts on is dependent on the language settings of the database. 请注意,Oracle周开始的日期取决于数据库的语言设置。 Some countries start on Sunday and some Monday. 一些国家/地区在周日和某些星期一开始。 You'll have to look at your settings to see. 您必须查看您的设置才能看到。 If it already starts on Sunday, then you're in luck! 如果已经在周日开始,那么您很幸运!

if the example you have posted is your work in progress version - before worrying about getting the days of the week in you should look into getting the basics of the query right 如果您发布的示例是正在进行的版本-在担心获取星期几之前,应先了解正确的查询基础

you are selecting e.projectid and u.userid but you haven't got any tables named e or u in your query - it looks like you want to alias them as e and u? 您正在选择e.projectid和u.userid,但查询中没有任何名为e或u的表-您似乎想为它们别名为e和u?

the where clause of your query is also looking for the table e which isn't present 您的查询的where子句还在寻找不存在的表e

in that case you should change 在那种情况下,你应该改变

from table1 
join table2
on table1.a= table2.a

to

from table1 e        -- select from table1 using alias e
  join table2 u      -- join table2 using alias u
    on ( e.a = u.a ) -- joining on column a from table1 (e) = a from table2 (u)

once you have replaced the a's in the on section with the column names you want to join using it might well run after you remove the last column ", ??" 一旦您用要加入的列名称替换了on部分中的a后,就可以在删除最后一列“,??”后运行 from the select - perhaps something along these lines 从选择-也许沿着这些路线

select
  count (e.projectid) PROJECTS,
  count (u.userid) USERS

from table1 e
  join table2 u
    on ( e.a = u.a )

where e.FILTERING_COLUMN = '1'
  and e.eventdatetime >= sysdate-30

note that as sysdate is the current time on the server (depending on localisation and session settings) you can use greater than sysdate-30 instead of between which may well be give the query optimiser an easier time if the table is suitable indexed 请注意,由于sysdate是服务器上的当前时间(取决于本地化和会话设置),因此可以使用大于sysdate-30的值,而不是使用sysdate-30的值,如果表适合索引,则可以使查询优化器的时间更短

the basic rule for grouping is that to select a column you need to either be grouping by it or using an aggregate function such as COUNT() 分组的基本规则是要选择一个列,您需要按该列进行分组或使用诸如COUNT()之类的聚合函数

so you'll probably want something like 所以你可能想要像

select
  count (e.projectid) PROJECTS,
  count (u.userid) USERS,
  to_char(e.eventdatetime,'MM') MONTH

from table1 e
  join table2 u
    on ( e.a = u.a )

where e.FILTERING_COLUMN = '1'
  and e.eventdatetime >= sysdate-30

group by e.eventdatetime

though this won't be the most optimal way to do this it would be easier if you posted the schemas involved in the issue 尽管这不是实现此目的的最佳方法,但是如果发布了涉及该问题的模式,则会更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM