简体   繁体   English

计算日期列中的日期是否在开始日期和结束日期之间[Oracle SQL]

[英]Count if date in date column is between start and end date [ Oracle SQL ]

This is my first post, so I hope I've posted this one correctly. 这是我的第一篇文章,所以我希望我已经正确地张贴了这篇文章。

My problem: 我的问题:

I want to count the number of active customers per day, the last 30 days. 我想计算最近30天每天的活跃客户数量。

What I have so far: 到目前为止,我有:

In the first column I want to print today, and the last 29 days. 在第一列中,我想今天打印,最后29天。 This I have done with 我已经完成了

select distinct trunc(sysdate-dayincrement, 'DD') AS DATES
from   (
  select level as dayincrement
  from   dual
  connect by level <= 30
)

I've picked it up here at stackoverflow, and it works perfectly. 我是在stackoverflow上拾取它的,它的工作原理很完美。 I can even extend the number of days returned to ex. 我什至可以延长退还给ex的天数。 365 days. 365天。 Perfect! 完善!

I also have a table that looks like this 我也有一张像这样的桌子

|Cust# | Start date | End date  |
| 1000 | 01.01.2015 | 31.12.2015|
| 1001 | 02.01.2015 | 31.12.2016|
| 1002 | 02.01.2015 | 31.03.2015|
| 1003 | 03.01.2015 | 31.08.2015|

This is where I feel the problem starts 这是我感到问题开始的地方

I would like to get this result: 我想得到这个结果:

| Dates    | # of cust |
|04.01.2015|     4     |
|03.01.2015|     4     |
|02.01.2015|     3     |
|01.01.2015|     1     |

Here the query would count 1 if: 如果满足以下条件,查询将在此处计数为1:

  • Start date <= DATES 开始日期<=日期
  • End date >= DATES 结束日期> =日期

Else count 0. 其他计数为0。

I just don't know how to structure the query. 我只是不知道如何构造查询。

I tried this, but it didn't work. 我试过了,但是没有用。

count(
  IF ENDDATE <= DATES THEN
    IF STARTDATE >= DATES THEN 1 ELSE 0 END IF
  ELSE
    0
  END IF
) AS CUST

Any ideas? 有任何想法吗?

You could write a CASE expression equivalent to your IF-ELSE construct. 您可以编写一个与IF-ELSE构造等效的CASE表达式。

For example, 例如,

SQL> SELECT COUNT(
  2    CASE
  3      WHEN hiredate <= sysdate
  4      THEN 1
  5      ELSE 0
  6    END ) AS CUST
  7  FROM emp;

      CUST
----------
        14

SQL>

However, looking at your desired output, it seems, you just need to use COUNT and GROUP BY . 但是,看您想要的输出,看来,您只需要使用COUNTGROUP BY即可 The date conditions should be in the filter predicate . 日期条件应在过滤谓词中

For example, 例如,

SELECT dates, COUNT(*) 
FROM   table_name
WHERE  dates BETWEEN start_date AND end_date
GROUP BY dates;

The following produces the results you're looking for. 以下产生您要寻找的结果。 I had change the date generator to start on 04-JAN- 2015 instead of SYSDATE (which is, of course, in the year 2016 ), and to use LEVEL-1 to include 'current' day: 我将日期生成器更改为从2015年1月4 开始,而不是SYSDATE(当然是在2016年 ),并使用LEVEL-1包括“当前”日期:

WITH CUSTS AS (SELECT 1000 AS CUST_NO, TO_DATE('01-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-DEC-2015', 'DD-MON-YYYY') AS END_DATE FROM DUAL UNION ALL
               SELECT 1001 AS CUST_NO, TO_DATE('02-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-DEC-2016', 'DD-MON-YYYY') AS END_DATE FROM DUAL UNION ALL
               SELECT 1002 AS CUST_NO, TO_DATE('02-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-MAR-2015', 'DD-MON-YYYY') AS END_DATE FROM DUAL UNION ALL
               SELECT 1003 AS CUST_NO, TO_DATE('03-JAN-2015', 'DD-MON-YYYY') AS START_DATE, TO_DATE('31-AUG-2015', 'DD-MON-YYYY') AS END_DATE FROM DUAL ),
     DATES AS (SELECT DISTINCT TRUNC(TO_DATE('04-JAN-2015', 'DD-MON-YYYY') - DAYINCREMENT, 'DD') AS DT
                FROM (SELECT LEVEL-1 AS DAYINCREMENT
                        FROM DUAL
                        CONNECT BY LEVEL <= 30))
SELECT d.DT, COUNT(*)
  FROM CUSTS c
  CROSS JOIN DATES d
  WHERE d.DT BETWEEN c.START_DATE AND c.END_DATE
  GROUP BY d.DT
  ORDER BY DT DESC

Best of luck. 祝你好运。

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

相关问题 Teradata SQL 代码,用于查找 eff 开始日期和结束日期之间的计数 - Teradata SQL code to find count between eff start and end date Oracle SQL查询中的开始/结束日期和时间 - start/end date and time in oracle sql query Oracle SQL 动态开始和结束日期 - Oracle SQL Dynamic Start and End Date 如何从单个日期列获取开始/结束日期-oracle - How to get start/end date from single date column -oracle Oracle SQL Cursor for循环,用于在START_DATE和END_DATE之间检查并分配行程值 - Oracle SQL Cursor for loop to check between the START_DATE and END_DATE and assign a trip value 在MS Access中计算开始日期和结束日期之间的不同项目 - Count distinct items between start date and end date in MS Access 查找开始日期和结束日期之间的日期 - Finding date between start date and end date SQL - 基于另一列的开始和结束日期 - SQL - Start and End date based on another column 如果输入日期不可用,如何在 oracle 中获取开始日期和结束日期之间的输入日期,然后返回最大日期记录 - How to get the input date between start and end date in oracle if input date not available then return max date record SQL - 将日期列转换为开始和结束日期 - SQL - convert date column to start and end dates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM