Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: Tough Analytical SQL Question
michaelw436.spam_at_gmail.com wrote:
> I am wondering if anyone can find a simple solution to this SQL query.
> The end of goal of the query is to give me a count of distinct cust_id
> for each ctype and order_date, for a set of ranges. For example, I
> want an example record in the end with
>
> CTYPE = 'ECHO'
> order_date = '09-aug-2007'
> dist_1_day = 2
> dist_7_day = 2
>
> SELECT ctype_id ctype, order_date order_dt,
> dist_1_day distinct_cust_1_day,
> dist_7_day distinct_cust_7_day
> FROM (SELECT DISTINCT order_date, ctype_id,
> COUNT (cust_id) OVER
> (PARTITION BY order_date, ctype_id)
>
> dist_1_day,
> COUNT (cust_id) OVER
> (PARTITION BY ctype_id ORDER BY order_date RANGE 7 PRECEDING)
>
> dist_7_day
> FROM (SELECT DISTINCT TRUNC
>
> (order_date
> )
> order_date,
> o.ctype_id,
> o.cust_id
> FROM orders.orders
> o,
>
> webadmin.cust_details cd
> WHERE o.order_date
>> = to_date('01-aug-2007','DD-MON-YYYY') - 7> YYYY')
> and ctype_id =
> 'ECHO'
> AND order_type IN
> ('O')
> AND ostatus <>
> 'X'
> AND cd.cust_id =
> o.cust_id
> AND cd.is_guest =
> 0))
> WHERE order_date >= to_date('01-aug-2007','DD-MON-
Add the integer 1 to your inner query and then sum the integers.
-- Daniel A. Morgan University of Washington damorgan_at_x.washington.edu (replace x with u to respond) Puget Sound Oracle Users Group www.psoug.orgReceived on Fri Aug 10 2007 - 10:56:03 CDT
![]() |
![]() |