创建子查询以按日期计算当前行后面有多少行。

huangapple go评论59阅读模式
英文:

Creating sub query to count how many rows follow current row by date

问题

对于每个从数据库返回的行,我想显示该事件的日期总数以及在该事件之后剩余的日期总数。

例如,如果“Event 1”在2023年5月29日、2023年5月30日和2023年5月31日有三个不同的日期,我希望有以下数据:

Event 1 | 29/05/2023 | 总共3个日期 | 此后剩余2个
Event 1 | 30/05/2023 | 总共3个日期 | 此后剩余1个
Event 1 | 31/05/2023 | 总共3个日期 | 此后剩余0个

我相信我需要一个子查询,所以我尝试了以下查询:

SELECT
    e.id AS event_id,
    e.title AS event_title,
    ed.event_date,
    ed.event_start,
    (
      SELECT COUNT(*)
      FROM event_dates AS ed2
      WHERE ed2.event_id = e.id
    ) AS no_in_series,
    (
      SELECT COUNT(*)
      FROM event_dates AS ed3
      WHERE ed3.event_id = e.id AND ed3.event_date > ed.event_date
    ) AS no_next_in_series
FROM event_dates AS ed
LEFT JOIN events AS e ON ed.event_id = e.id
WHERE DAY(ed.event_date) = '05' AND MONTH(ed.event_date) = '07' AND YEAR(ed.event_date) = '2023'
ORDER BY ed.event_start ASC;

第一个子查询计算总的演出次数运行良好,但第二个子查询计算跟随当前日期/行的事件数量时出现问题。

请查看我在此处的SQL Fiddle中的数据和最后一次尝试,并提供帮助,如果可以的话。

我正在使用MariaDB版本10.3.25。

英文:

I have a table of events and a table of event dates.

For each row returned from the database, I would like to show the total number of dates for that event and to show the total number of dates remaining after that event.

For example, if 'Event 1' had three different dates on 29/05/2023, 30/05/2023 and 31/05/2023, I would like to have the following data:

Event 1 | 29/05/2023 | 3 shows in total | 2 remaining after this
Event 1 | 30/05/2023 | 3 shows in total | 1 remaining after this
Event 1 | 31/05/2023 | 3 shows in total | 0 remaining after this

I believe I need a sub query and so I tried the following query:

SELECT
    e.id AS event_id,
    e.title AS event_title,
    ed.event_date,
    ed.event_start,
    (
      SELECT COUNT(*)
      FROM event_dates AS ed2
      WHERE ed2.event_id = e.id
    ) AS no_in_series,
    (
      SELECT COUNT(*)
      FROM event_dates AS ed3
      WHERE ed3.event_id = e.id AND ed3.event_date > ed3.event_date
    ) AS no_next_in_series
FROM event_dates AS ed
LEFT JOIN events AS e ON ed.event_id = e.id
WHERE DAY(ed.event_date) = '05' AND MONTH(ed.event_date) = '07' AND YEAR(ed.event_date) = '2023'
ORDER BY ed.event_start ASC;

The first sub query count to count the total number of shows works just fine but the second sub query to count how many events follow the current date/row, does not.

Please see my SQL Fiddle here for data and last attempt, and help if you can.

I'm using MariaDB version 10.3.25.

答案1

得分: 2

我认为最好的方法是使用窗口函数来计算总数,并使用相关子查询来计算未来的数量。以下假设还需要包括event_time,如果不需要的话,请相应地删除。

select ed.event_id, e.title, ed.event_date, ed.event_start, 
    Count(*) over(partition by event_id) No_in_series, 
    (
      select Count(*) from event_dates ed2 
      where ed2.event_id = ed.event_id 
            and timestamp(ed2.event_date, ed2.event_start) 
              > timestamp(ed.event_date, ed.event_start)
    ) No_next_in_series
from event_dates ed
join events e on e.id = ed.event_id
where month(ed.event_date) = '07' and year(ed.event_date) = '2023'
order by ed.event_id, ed.event_date;

mariadb fiddle上查看示例。

英文:

I think you'd be best off using a combination of window function to count the totals and a correlated query for the future count. The below assumes it makes sense to also include the event_time, if that's not a requirement then simply remove accordingly.

select ed.event_id, e.title, ed.event_date, ed.event_start, 
	Count(*) over(partition by event_id) No_in_series, 
	(
	  select Count(*) from event_dates ed2 
	  where ed2.event_id = ed.event_id 
        and timestamp(ed2.event_date, ed2.event_start) 
          > timestamp(ed.event_date, ed.event_start)
	) No_next_in_series
from event_dates ed
join events e on e.id = ed.event_id
where month(ed.event_date) = '07' and year(ed.event_date) = '2023'
order by ed.event_id, ed.event_date;

See a mariadb fiddle

答案2

得分: 1

你可以使用窗口函数 count() 来计算总数,使用 rank() 来获取 no_next_in_series

SELECT
    e.id AS event_id,
    e.title AS event_title,
    ed.event_date,
    ed.event_start,
    count(*) over (partition by event_id) AS no_in_series,
    rank() over (partition by event_id order by timestamp(event_date, event_start) desc) - 1 AS no_next_in_series
FROM event_dates AS ed
LEFT JOIN events AS e ON ed.event_id = e.id
WHERE MONTH(ed.event_date) = '07' AND YEAR(ed.event_date) = '2023'
ORDER BY ed.event_date ASC;

演示链接

英文:

You can do it by using the window functions count() to count total and rank() to get no_next_in_series :

SELECT
    e.id AS event_id,
    e.title AS event_title,
    ed.event_date,
    ed.event_start,
    count(*) over (partition by event_id) AS no_in_series,
    rank() over (partition by event_id order by timestamp(event_date, event_start) desc) - 1 AS no_next_in_series
FROM event_dates AS ed
LEFT JOIN events AS e ON ed.event_id = e.id
WHERE MONTH(ed.event_date) = '07' AND YEAR(ed.event_date) = '2023'
ORDER BY ed.event_date ASC;

Demo here

huangapple
  • 本文由 发表于 2023年7月24日 18:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定