在 SQL 中计算每年的事件数

发布于 2024-12-16 13:44:27 字数 142 浏览 1 评论 0原文

我有一个有日期的事件列表。我试图在单个语句中使用简单的联接、选择等(无子查询)来计算 mySQL 中今年发生的事件数量,以及两侧 5 年发生的事件数量(无论是否发生任何事件)。

我有一张表,可以生成年份和当年的事件数量,但当该年没有发生事件时就会遇到问题

I have a list of events that have a date. I'm trying to count how many events take place in the current year, and 5 years on either side (regardless of whether any events took place) in mySQL using simple joins, selects, etc (no subqueries) in a single statement.

I have a table that produces the years and the number of events in that year, but am having problems when the year has no events taking place

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画▽骨i 2024-12-23 13:44:27

查看 mysql 上的日期函数 http:// /dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff

您可以使用 datediff 这会给您带来天数差异。前任;

其中abs(datediff(now(), event_date)) < 365*5

或 dateadd(),如果您的事件日期是时间戳,请使用 timestampdiff()

示例查询

SELECT count(*) FROM mytable
WHERE abs(datediff(now(), event_date)) < 365*5

UPDATE

基于我在这里读到的一些评论,这里有一个查询供您

SELECT year(event_date) as event_year, count(event_date)
FROM mytable
WHERE
abs(datediff(now(), event_date)) < 365*5
GROUP by year(event_date)

随意调整5 in (365 * 5) for different range

UPDATE 2

这不是很漂亮,但你可以用纯mysql尝试这个。如果需要,您还可以将其修改为存储过程:

SET @y6 = year(now());
SET @y5 = @y6-1;
SET @y4 = @y5-1;
SET @y3 = @y4-1;
SET @y2 = @y3-1;
SET @y1 = @y2-1;

SET @y7 = @y6+1;
SET @y8 = @y7+1;
SET @y9 = @y8+1;
SET @y10 = @y9+1;
SET @y11 = @y10+1;

CREATE TEMPORARY TABLE event_years (event_year int not null);
INSERT INTO event_years SELECT @y1;
INSERT INTO event_years SELECT @y2;
INSERT INTO event_years SELECT @y3;
INSERT INTO event_years SELECT @y4;
INSERT INTO event_years SELECT @y5;
INSERT INTO event_years SELECT @y6;
INSERT INTO event_years SELECT @y7;
INSERT INTO event_years SELECT @y8;
INSERT INTO event_years SELECT @y9;
INSERT INTO event_years SELECT @y10;
INSERT INTO event_years SELECT @y11;

SELECT ey.event_year , (SELECT count(event_date) from mytable where year(event_date) = ey.event_year)
from event_years ey;

连接关闭后临时表将自行删除。如果在 SELECT 之后添加 DROP TABLE,则可能无法取回结果。

Look into date functions on mysql http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff

You can use datediff which will give you difference in days. Ex;

WHERE abs(datediff(now(), event_date)) < 365*5

or dateadd(), if your event dates are timestamps, use timestampdiff()

Sample query

SELECT count(*) FROM mytable
WHERE abs(datediff(now(), event_date)) < 365*5

UPDATE

based on some of the comments I've read here, here's a query for you

SELECT year(event_date) as event_year, count(event_date)
FROM mytable
WHERE
abs(datediff(now(), event_date)) < 365*5
GROUP by year(event_date)

Feel free to adjust 5 in (365 * 5) for different range

UPDATE 2

This is NOT very pretty but you can try this with pure mysql. You can also modify this to be a stored proc if necessary:

SET @y6 = year(now());
SET @y5 = @y6-1;
SET @y4 = @y5-1;
SET @y3 = @y4-1;
SET @y2 = @y3-1;
SET @y1 = @y2-1;

SET @y7 = @y6+1;
SET @y8 = @y7+1;
SET @y9 = @y8+1;
SET @y10 = @y9+1;
SET @y11 = @y10+1;

CREATE TEMPORARY TABLE event_years (event_year int not null);
INSERT INTO event_years SELECT @y1;
INSERT INTO event_years SELECT @y2;
INSERT INTO event_years SELECT @y3;
INSERT INTO event_years SELECT @y4;
INSERT INTO event_years SELECT @y5;
INSERT INTO event_years SELECT @y6;
INSERT INTO event_years SELECT @y7;
INSERT INTO event_years SELECT @y8;
INSERT INTO event_years SELECT @y9;
INSERT INTO event_years SELECT @y10;
INSERT INTO event_years SELECT @y11;

SELECT ey.event_year , (SELECT count(event_date) from mytable where year(event_date) = ey.event_year)
from event_years ey;

temporary table will get dropped by itself after your connection is closed. If you add DROP TABLE after SELECT, you might not get your results back.

黯然#的苍凉 2024-12-23 13:44:27

您尝试过使用左连接吗?

修改:

SELECT tleft.YEARS, COUNT(tright.EVENTS) 
FROM ONLY_YEARS tleft LEFT JOIN TABLE1 tright 
ON (tleft.YEARS = tright.YEARS)
GROUP BY tleft.YEARS;

通过此修改,您需要指向一个包含所有年份 (ONLY_YEARS) 的表,可能是一个包含从 1990 年到 2020 年的一列的虚拟表...

MySQL左连接优化链接

did you try to use join left?

MODIFIED:

SELECT tleft.YEARS, COUNT(tright.EVENTS) 
FROM ONLY_YEARS tleft LEFT JOIN TABLE1 tright 
ON (tleft.YEARS = tright.YEARS)
GROUP BY tleft.YEARS;

With that modification, you need to point to a table that holds all the years (ONLY_YEARS), maybe a dummy table with one column that goes from 1990 to 2020...

Left join optimization for MySQL link

一花一树开 2024-12-23 13:44:27

要选择两年之间发生的事件的计数(按年份分组),以下 sql 应该足够了:

select year(event.date), count(*) from event where event.date >= '2006' and event.date <= '2016' group by year(event.date);

但是,如果一年内没有发生任何事件,则不会返回任何结果。
数据库并不是真正为这种动态事物而设计的,我建议这种逻辑应该放在业务(或可能是数据访问)层中。

To select a count of events that happened between two years, grouped by years, the following sql should suffice:

select year(event.date), count(*) from event where event.date >= '2006' and event.date <= '2016' group by year(event.date);

However, if no events occurred in a year, no result will be returned for it.
Databases are not really designed for such dynamic things and I'd suggest such logic should be put in a business (or possibly data-access) layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文