Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 分解并计算“2个日期”列中的所有项目_Sql_Date_Count - Fatal编程技术网

Sql 分解并计算“2个日期”列中的所有项目

Sql 分解并计算“2个日期”列中的所有项目,sql,date,count,Sql,Date,Count,我想得到所有可能的日期(在本例中为:event_day)以及在start_date和end_date之间发生的事件数。请看下表 --------------------------------- start_date | end_date | event --------------------------------- 2019-01-01 | 2019-01-04 | A 2019-01-02 | 2019-01-03 | B 2019-01-01 | 2019-

我想得到所有可能的日期(在本例中为:event_day)以及在start_date和end_date之间发生的事件数。请看下表

 ---------------------------------
 start_date |  end_date   |  event
 ---------------------------------
 2019-01-01 |  2019-01-04 | A 
 2019-01-02 |  2019-01-03 | B
 2019-01-01 |  2019-01-06 | C
我想查询所有日期中的事件数。请看下面的结果

----------------------------
event_day |  event_count
----------------------------
2019-01-01 |   2
2019-01-02 |   3
2019-01-03 |   3
2019-01-04 |   2
2019-01-05 |   1
2019-01-06 |   1
我阅读了其他来源,但只能找到如何从2个日期分解日期。这里有什么帮助吗?谢谢

您可以使用解决方案:

SELECT date_value AS event_day, COUNT(*) AS event_count
FROM (
    SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS date_value
    FROM
        (SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
        (SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
        (SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
        (SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
        (SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) calendar INNER JOIN events ON calendar.date_value BETWEEN events.start_date AND events.end_date
WHERE calendar.date_value BETWEEN '2019-01-01' AND '2019-01-04' -- to filter for a specific date range.
GROUP BY date_value

如果您使用postgres,您可以使用generate_series生成日历表,基本上您需要一个日历表来分解日期

WITH a AS(
  Select '2019-01-01'::date as start_date  ,'2019-01-04'::date as  end_date union all
  Select '2019-01-02'::date , '2019-01-03'::date union all
  Select '2019-01-01'::date, '2019-01-06'::date 
  )
 Select t.date_generated,count(*) as event
 from a
 JOIN(Select date_generated
  from generate_series(date '2019-01-01',
                   date '2019-12-31',
                   interval '1 day') as t(date_generated)
   ) t
   ON t.date_generated between a.start_date and a.end_date
   group by t.date_generated
   order by t.date_generated
如果有任何问题,请讨论。
请创建日历表并插入日历数据。

请用您使用的DBMS标记您的问题。
 select Calendar.Calndr_date , count(Calendar.Calndr_date) count_events
 from event_table
 join Calendar on
 Calendar.Calndr_date between  event_table.start_date and event_table.end_date
 group by Calendar.Calndr_date