| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.server -> Re: Tough Analytical SQL Question
michaelw436_at_gmail.com wrote:
> On Aug 10, 12:21 pm, DA Morgan <damor..._at_psoug.org> wrote: >> michaelw..._at_gmail.com wrote: >>> On Aug 10, 11:56 am, DA Morgan <damor..._at_psoug.org> wrote: >>>> michaelw436.s..._at_gmail.com wrote:
>>>>>> = to_date('01-aug-2007','DD-MON-YYYY') - 7
>>>>> 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-
>>>>> YYYY')
>>>> Add the integer 1 to your inner query and then sum the integers.
>>>> --
>>>> Daniel A. Morgan
>>>> University of Washington
>>>> damor..._at_x.washington.edu (replace x with u to respond)
>>>> Puget Sound Oracle Users Groupwww.psoug.org
>>> I like the idea, but I still get the same results because when I sum
>>> the integer column that I create, I am still adding 1 for each day in
>>> the the 7 day range. I only want to count the distinct cust_id's
>>> inside of that 7 day range. Does that make sense?
>> Then add it after doing the grouping.
>> --
>> Daniel A. Morgan
>> University of Washington
>> damor..._at_x.washington.edu (replace x with u to respond)
>> Puget Sound Oracle Users Groupwww.psoug.org
>
> Thanks for your help Daniel by the way, I still don't follow exactly
> where to do this summing of the integer that I create. I tried to add
> an integer to the inner query, but that still ends up with my original
> results. I think that you are going the right direction here, but I
> have spent so many hours here that I needed a fresh perspective. Here
> is the SQL that I currently have...
>
> 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
> 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-YYYY')
I'm really pressed for time today so I can't write it for you but this may give you an idea:
http://www.psoug.org/reference/explain_plan.html
Look at Test Statement #9.
-- 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 - 19:13:38 CDT
![]() |
![]() |