0

我正在尝试编写一个 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

现在我仍然坚持查询,我不知道该怎么做,有人可以帮忙吗?

对不起我的英语不好

4

3 回答 3

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 演示

于 2013-02-14T05:05:36.453 回答
0

Please try:

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;
于 2013-02-14T05:17:58.700 回答
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

于 2013-02-14T05:05:20.643 回答