 |
|
 |
|
Next: Domain Logins required if member of Domain Group?
|
| Author |
Message |
External

Since: Sep 29, 2008 Posts: 2
|
(Msg. 1) Posted: Mon Sep 29, 2008 1:33 pm
Post subject: Help with some ordering Archived from groups: comp>databases>oracle>misc (more info?)
|
|
|
I have this query:
SELECT customer_id, co.order_id, p.product_id, ol.status, p.code,
p.subproduct_id,
FIRST_VALUE(ol.status)
OVER (PARTITION BY customer_id, subproduct_id ORDER BY order_date
DESC) LAST_STATUS
FROM engine.customer_order co, engine.order_line ol, engine.product p
WHERE co.order_id = ol.order_id
AND ol.product_id = p.product_id
AND p.subproduct_id IN (209, 211, 216, 217, 220, 222, 223, 224, 226)
ORDER BY customer_id;
I want to get the status of the most recent record for that customer.
There can be more than 1 record for that customer with the same
subproduct ID.
A customer may have ordered a product, canceled it and re-ordered it
at a later date.....I would want the status of the most recent, along
with the other information above.
This query is giving me all the records. I thought this query would
partition by customer ID / subproduct ID, order by the order date, and
give me the first record (FIRST_VALUE).
Any idea why I am not getting this?? >> Stay informed about: Help with some ordering |
|
| Back to top |
|
 |  |
External

Since: Feb 13, 2008 Posts: 11
|
(Msg. 2) Posted: Mon Sep 29, 2008 9:42 pm
Post subject: Re: Help with some ordering [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
a... RemoveThis @mickeyschicago.com wrote:
> I have this query:
>
> SELECT customer_id, co.order_id, p.product_id, ol.status,
> p.code, p.subproduct_id,
> FIRST_VALUE(ol.status)
> OVER (PARTITION BY customer_id, subproduct_id
> ORDER BY order_date
> DESC) LAST_STATUS
> FROM engine.customer_order co, engine.order_line ol,
> engine.product p
> WHERE co.order_id = ol.order_id
> AND ol.product_id = p.product_id
> AND p.subproduct_id IN (209, 211, 216, 217, 220, 222,
> 223, 224, 226)
> ORDER BY customer_id;
>
> I want to get the status of the most recent record for
> that customer.
> There can be more than 1 record for that customer with
> the same subproduct ID.
>
> A customer may have ordered a product, canceled it and
> re-ordered it at a later date.....I would want the
> status of the most recent, along with the other
> information above.
>
> This query is giving me all the records. I thought
> this query would partition by customer ID / subproduct
> ID, order by the order date, and give me the first
> record (FIRST_VALUE).
The analytical function _is_ partitioning and giving you
the first status within that partition by the the
order date.
> Any idea why I am not getting this??
Analytic functions don't reduce rows. Indeed they
give you group information for _every_ row.
Select the status as normal, but use row_number()
instead of first_value. Then do an outer select
on your query looking for row numbers of 1.
Note that this might still give you multiple rows
if a customer orders the same product multiple
times on the same order_date. In which case it's
generally best to refine the order by in the
row_number(), e.g. by order_id desc.
--
Peter >> Stay informed about: Help with some ordering |
|
| Back to top |
|
 |  |
External

Since: Dec 10, 2003 Posts: 46
|
(Msg. 3) Posted: Tue Sep 30, 2008 1:25 am
Post subject: Re: Help with some ordering [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
<art.DeleteThis@mickeyschicago.com> a écrit dans le message de news: 0f59e550-e2e5-4c9f-b873-3fc2d7b5846f.DeleteThis@y71g2000hsa.googlegroups.com...
|
| I have this query:
|
| SELECT customer_id, co.order_id, p.product_id, ol.status, p.code,
| p.subproduct_id,
| FIRST_VALUE(ol.status)
| OVER (PARTITION BY customer_id, subproduct_id ORDER BY order_date
| DESC) LAST_STATUS
| FROM engine.customer_order co, engine.order_line ol, engine.product p
| WHERE co.order_id = ol.order_id
| AND ol.product_id = p.product_id
| AND p.subproduct_id IN (209, 211, 216, 217, 220, 222, 223, 224, 226)
| ORDER BY customer_id;
|
| I want to get the status of the most recent record for that customer.
| There can be more than 1 record for that customer with the same
| subproduct ID.
|
| A customer may have ordered a product, canceled it and re-ordered it
| at a later date.....I would want the status of the most recent, along
| with the other information above.
|
| This query is giving me all the records. I thought this query would
| partition by customer ID / subproduct ID, order by the order date, and
| give me the first record (FIRST_VALUE).
|
| Any idea why I am not getting this??
Have a look at ROW_NUMBER/RANK/DENSE_RANK functions.
Regards
Michel >> Stay informed about: Help with some ordering |
|
| Back to top |
|
 |  |
External

Since: Mar 07, 2008 Posts: 197
|
(Msg. 4) Posted: Tue Sep 30, 2008 5:25 am
Post subject: Re: Help with some ordering [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
<art.DeleteThis@mickeyschicago.com> schreef in bericht
news:0f59e550-e2e5-4c9f-b873-3fc2d7b5846f@y71g2000hsa.googlegroups.com...
>
> I have this query:
>
> SELECT customer_id, co.order_id, p.product_id, ol.status, p.code,
> p.subproduct_id,
> FIRST_VALUE(ol.status)
> OVER (PARTITION BY customer_id, subproduct_id ORDER BY order_date
> DESC) LAST_STATUS
> FROM engine.customer_order co, engine.order_line ol, engine.product p
> WHERE co.order_id = ol.order_id
> AND ol.product_id = p.product_id
> AND p.subproduct_id IN (209, 211, 216, 217, 220, 222, 223, 224, 226)
> ORDER BY customer_id;
>
> I want to get the status of the most recent record for that customer.
> There can be more than 1 record for that customer with the same
> subproduct ID.
>
> A customer may have ordered a product, canceled it and re-ordered it
> at a later date.....I would want the status of the most recent, along
> with the other information above.
>
> This query is giving me all the records. I thought this query would
> partition by customer ID / subproduct ID, order by the order date, and
> give me the first record (FIRST_VALUE).
>
> Any idea why I am not getting this??
No, and I don't have any idea why you keep posting questions without
following up to most of the responses you get. And why you keep changing
your address with every n-th post, unless this is related to the
none-responsiveness.
Shakespeare >> Stay informed about: Help with some ordering |
|
| Back to top |
|
 |  |
External

Since: Mar 07, 2008 Posts: 197
|
(Msg. 5) Posted: Tue Sep 30, 2008 5:25 am
Post subject: Re: Help with some ordering [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Shakespeare" <whatsin RemoveThis @xs4all.nl> schreef in bericht
news:48e1ea06$0$201$e4fe514c@news.xs4all.nl...
>
> <art RemoveThis @mickeyschicago.com> schreef in bericht
> news:0f59e550-e2e5-4c9f-b873-3fc2d7b5846f@y71g2000hsa.googlegroups.com...
>>
>> I have this query:
>>
>> SELECT customer_id, co.order_id, p.product_id, ol.status, p.code,
>> p.subproduct_id,
>> FIRST_VALUE(ol.status)
>> OVER (PARTITION BY customer_id, subproduct_id ORDER BY order_date
>> DESC) LAST_STATUS
>> FROM engine.customer_order co, engine.order_line ol, engine.product p
>> WHERE co.order_id = ol.order_id
>> AND ol.product_id = p.product_id
>> AND p.subproduct_id IN (209, 211, 216, 217, 220, 222, 223, 224, 226)
>> ORDER BY customer_id;
>>
>> I want to get the status of the most recent record for that customer.
>> There can be more than 1 record for that customer with the same
>> subproduct ID.
>>
>> A customer may have ordered a product, canceled it and re-ordered it
>> at a later date.....I would want the status of the most recent, along
>> with the other information above.
>>
>> This query is giving me all the records. I thought this query would
>> partition by customer ID / subproduct ID, order by the order date, and
>> give me the first record (FIRST_VALUE).
>>
>> Any idea why I am not getting this??
>
> No, and I don't have any idea why you keep posting questions without
> following up to most of the responses you get. And why you keep changing
> your address with every n-th post, unless this is related to the
> none-responsiveness.
>
> Shakespeare
>
No, and I don't have any idea why you keep posting questions without
following up to most of the responses you get. And why you keep changing
your address with every n-th post, unless this is related to the
none-responsiveness.
Shakespeare
And why you still post replies to your own post with the same question. >> Stay informed about: Help with some ordering |
|
| Back to top |
|
 |  |
External

Since: Sep 29, 2008 Posts: 2
|
(Msg. 6) Posted: Tue Sep 30, 2008 5:47 am
Post subject: Re: Help with some ordering [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Sep 29, 11:44 pm, "Michel Cadot" <micadot{at}altern{dot}org> wrote:
> <a... DeleteThis @mickeyschicago.com> a écrit dans le message de news: 0f59e550-e2e5-4c9f-b873-3fc2d7b58... DeleteThis @y71g2000hsa.googlegroups.com...
> |
> | I have this query:
> |
> | SELECT customer_id, co.order_id, p.product_id, ol.status, p.code,
> | p.subproduct_id,
> | FIRST_VALUE(ol.status)
> | OVER (PARTITION BY customer_id, subproduct_id ORDER BY order_date
> | DESC) LAST_STATUS
> | FROM engine.customer_order co, engine.order_line ol, engine.product p
> | WHERE co.order_id = ol.order_id
> | AND ol.product_id = p.product_id
> | AND p.subproduct_id IN (209, 211, 216, 217, 220, 222, 223, 224, 226)
> | ORDER BY customer_id;
> |
> | I want to get the status of the most recent record for that customer.
> | There can be more than 1 record for that customer with the same
> | subproduct ID.
> |
> | A customer may have ordered a product, canceled it and re-ordered it
> | at a later date.....I would want the status of the most recent, along
> | with the other information above.
> |
> | This query is giving me all the records. I thought this query would
> | partition by customer ID / subproduct ID, order by the order date, and
> | give me the first record (FIRST_VALUE).
> |
> | Any idea why I am not getting this??
>
> Have a look at ROW_NUMBER/RANK/DENSE_RANK functions.
>
> Regards
> Michel
Thanks for all your info, the DENSE_RANK did the job..... >> Stay informed about: Help with some ordering |
|
| Back to top |
|
 |  |
| Related Topics: | Replacing a subselect with Collections - Hi all oracle users, I have a cursor which fetches a few thousand records. Inside it I have a subselect which is used for decoding one field. CURSOR snap_crs IS SELECT decode (snap.id, 1, (select code_ptr from anag_ptr where desc_ptr =..
how to refresh a sequence - How could I refresh a sequence in Oracle using an SQL statement? I've got the problem, that the current number of a sequence is less the highest index in a table, where I want to use the sequence. Regards, Robert
Using NonUnique Index to Enforce Uniqueness - Oracle manual describes using non unique index to enforce unique constraint so that the index will not be dropped with constraint is diabled. But I dont understand why non unique index can enforce uniqueness?
Triggers and Window Service - Hi, Is there a way to create an oracle Trigger that will start a window service? I am trying to get the service away from running a timer. Running Oracle 9i and windows 2K or XP Thanks
aggregation over two hierarchies - In my application there are two given tables: JOB table: job_id open closed splitfrom mergedto ================================================= AAAA 1/1/90 1/1/00 BBBB 1/1/80 1/1/00 CCCC 1/1/00 1/1/01 AAAA .. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|