SQL查询定期获取付款日期

时间:2013-02-14 04:48:03

标签: sql oracle

我正在尝试编写一个SQL查询,以便让哪些客户定期支付账单,哪些客户没有。 该表位于ORACLE。

CUSTOMERS table.
customers_id   customers_name   bill_value    payment_date
----------------------------------------------------------
001            ary              30000         1/14/2012
001            ary              30000         2/14/2012
001            ary              30000         3/14/2012
001            ary              30000         4/14/2012
002            budhy            25000         1/21/2012
002            budhy            25000         2/25/2012
002            budhy            25000         3/21/2012
002            budhy            25000         4/23/2012
003            chanadh          51000         1/17/2012
003            chanadh          51000         2/17/2012
003            chanadh          51000         3/17/2012
003            chanadh          51000         4/17/2012    

问题是......

首先,我想展示定期支付账单的客户,这是我期望的结果......

customers_id   customers_name   bill_value    payment_date
----------------------------------------------------------
001            ary              30000         1/14/2012
001            ary              30000         2/14/2012
001            ary              30000         3/14/2012
001            ary              30000         4/14/2012
003            chanadh          51000         1/17/2012
003            chanadh          51000         2/17/2012
003            chanadh          51000         3/17/2012
003            chanadh          51000         4/17/2012

第二,我想显示那些没有定期支付账单的客户,这是我期望的结果......

customers_id   customers_name   bill_value    payment_date
----------------------------------------------------------
002            budhy            25000         1/21/2012
002            budhy            25000         2/25/2012
002            budhy            25000         3/21/2012
002            budhy            25000         4/23/2012

现在我仍然坚持查询,我无法弄清楚如何做到这一点, 任何人都可以帮忙吗?

抱歉我的英文

3 个答案:

答案 0 :(得分:3)

试试这个

定期支付账单的客户

WITH CTE AS
(
  SELECT A.*,COUNT(DISTINCT EXTRACT(day from to_date(payment_date,'mm/dd/yyyy'))) 
              OVER (PARTITION BY customers_id) cnt
  FROM TEST A
)
SELECT * FROM CTE WHERE CNT = 1;

未定期支付账单的客户

WITH CTE AS
(
  SELECT A.*,COUNT(DISTINCT EXTRACT(day from to_date(payment_date,'mm/dd/yyyy'))) 
              OVER (PARTITION BY customers_id) cnt
  FROM TEST A
)
SELECT * FROM CTE WHERE CNT > 1; 

SQL DEMO

答案 1 :(得分:0)

select this.*, next.payment_date next_payment_date
from CUSTOMERS this
join CUSTOMERS next on next.customers_id = this.customers_id 
and next.payment_date = (select min(payment_date) from CUSTOMERS where customers_id = this.customers_id and payment_date>this.payment_date)
where trunc(add_months(this.payment_date,1)) < trunc(next.payment_date)

SQLFiddle:http://sqlfiddle.com/#!4/007e0/4

答案 2 :(得分:0)

请尝试:

SELECT
  y.*, 
  (CASE WHEN PD=CN THEN 'Regular' ELSE 'Irregular' END) Status
FROM (
    SELECT
      x.*,
      count(*) over (partition by CUSTOMERS_ID, EXTRACT(day FROM payment_date)) PD,
      count(*) over (partition by CUSTOMERS_ID) CN
    FROM CUSTOMERS x
)y;