简体   繁体   English

Informix DB - 按年,月,周,日,小时分组的SQL组

[英]Informix DB - SQL Group By Year, Month, Week, Day, Hour

I want to write sql queries in Informix db to return results that are grouped by Year/Month/Week/Day/Hr (I will write 5 sql queries for each one). 我想在Informix数据库中编写sql查询以返回按年/月/周/日/小时分组的结果(我将为每个编写5个sql查询)。

The scenario I have is as follow : 我的情景如下:

  • I have two tables, Contest table (contains information about contest name, description, start date, submission date, review date, result date), the other table is Submission table (contains submissions made to contests, things like submission id, submitter id, submission date ..etc). 我有两个表,竞赛表(包含竞赛名称,描述,开始日期,提交日期,审查日期,结果日期的信息),另一个表是提交表(包含提交竞赛的提交,提交ID,提交者ID,提交日期..等)。
  • What I want to achieve is get progress of a contest by getting submissions made grouped by day (sql returns stats about how many submissions submitted for each day), or by week, or by month, or by year ..etc. 我想要实现的是通过按天分组提交的提交来获得竞赛的进展(sql返回有关每天提交的提交数量的统计数据),或按周,按月或按年...等。

I am familiar with writing sql queries in Informix DB but such scenario turns out to be very complicated to me and I could not figure out how to do it. 我熟悉在Informix DB中编写SQL查询,但这种情况对我来说非常复杂,我无法弄清楚如何去做。

Can you please provide me a sample query that accomplish the scenario described above? 能否请您提供一个完成上述方案的示例查询?

If this isn't exactly what you need, it might get you some way along, or give you some ideas: 如果这不是您所需要的,它可能会让您有所帮助,或者给您一些想法:

SELECT contest_id, YEAR(submission_date) AS submission_period, COUNT(*),
       "Y" AS sub_type
  FROM submissions
  GROUP BY 1, 2
UNION ALL
SELECT contest_id, MONTH(submission_date) AS submission_period, COUNT(*),
       "M" AS sub_type
  FROM submissions
  GROUP BY 1, 2
UNION ALL
SELECT contest_id, DAY(submission_date) AS submission_period, COUNT(*),
       "D" AS sub_type
  FROM submissions
  GROUP BY 1, 2
ORDER BY 1, 4, 2

However, if you're looking at Month by Year instead of Months in isolation, or Week in Year, then I highly recommend you look at creating a "Time Dimension" such as is used in Data Warehousing applications. 但是,如果你看的是逐月而不是隔离的月份,或者一年中的周,那么我强烈建议你看看创建一个“时间维度”,例如在数据仓库应用程序中使用的。 This is a table with a record for every day that looks a bit like this: 这是一张包含每天记录的表,看起来有点像这样:

Date         Year   Month     Week      Quarter  Day    MthName
2013-01-01 | 2013 | 2013-01 | 2013W01 | 2013Q1 | Tues | January
2013-01-02 | 2013 | 2013-01 | 2013W01 | 2013Q1 | Wed  | January
..
2013-03-20 | 2013 | 2013-03 | 2013W12 | 2013Q1 | Wed  | March
..
2013-05-01 | 2013 | 2013-05 | 2013W18 | 2013Q2 | Wed  | May

By joining the submission date to this table, you can group by whatever column suits your requirement. 通过将提交日期加入此表,您可以按适合您要求的列进行分组。

Pro Tip: don't call them what I've labelled those headers, using reserved words will cause you pain :-) 专业提示:不要把它们称为我标记的那些标题,使用保留字会让你痛苦:-)

According to the documentation, informix has the functionality to do this. 根据文档,informix具有执行此操作的功能。 You just don't use the same function for each date component. 您只是不为每个日期组件使用相同的功能。

Ironically, to get the hour, you use a date function - to_char(). 具有讽刺意味的是,要获得小时,您可以使用日期函数 - to_char()。 There might be an appropriate mask for the hour. 小时可能有适当的面具。 If not, you'll have to use a substring. 如果没有,你将不得不使用子字符串。

For the date components, you would use time components year(), month(), and either day() or weekday(). 对于日期组件,您将使用时间组件year(),month()以及day()或weekday()。 You might be out of luck for the week. 本周你可能运气不好。

See these reference pages: 请参阅以下参考页面:

Edit 编辑

Here is a simple example, using the year function. 这是一个使用年份函数的简单示例。

select year(resultdate) resultyear, count(*) results
from etc
group by year(resultdate)

When I run this query, i get an error: "Error: A syntax error has occurred. (State:37000, Native Code: FFFFFF37)" You have to change the group by to the column number, ie 当我运行此查询时,我收到一个错误:“错误:发生了语法错误。(状态:37000,本机代码:FFFFFF37)”您必须将组更改为列号,即

select year(resultdate) as resultYear, count(*) as resultCount from caa44340 group by 1 选择年份(resultdate)作为resultYear,count(*)as resultCount from caa44340 group by 1

for it to run correctly! 让它正确运行!

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

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