![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I am using this query. *It returns information on existing orders the customer has, and ranks them based on the status. We have 25 products defined in a separate table. *Rather than having to keep doing a COUNT to check for a given product, I am hoping to somehow modify this query such that it returns a record for ALL products. *If the customer does not have that product, it will give NULL values for the status column. *right now I cannot find any way to do that join, I know it is some outer join. It may be a WITH query, which I always have trouble creating, but maybe someone can help, I've been at this for a few days. So, for a customer with 4 orders, this query returns 4 records. *I'd like it to return 25 (total orders in products table) and just a NULL or 'N' for products the customer does not have. SELECT email, customer_id, subproduct_id, description, status, rnum, status_rank FROM (SELECT email, customer_id, subproduct_id, status, rnum, status_rank, description * * * * * *FROM (SELECT email, c.customer_id, subproduct_id, status, description, * * * * * * * * * * * *CASE status * * * * * * * * * * * * WHEN 'Active' THEN 1 * * * * * * * * * * * * WHEN 'Pending' THEN 2 * * * * * * * * * * * * WHEN 'Complete' THEN 3 * * * * * * * * * * * * WHEN 'Cancelled' THEN 4 * * * * * * * * * * * END status_rank, * * * * * * * * * * * * *ROW_NUMBER() OVER (PARTITION BY subproduct_id ORDER BY subproduct_id) rnum * * * * * * * * * * *FROM customer c, customer_account ca, customer_address cd, customer_order_vw co * * * * * * * * * * *WHERE c.customer_id = ca.customer_id(+) * * * * * * * * * * * * * AND c.customer_id =cd.customer_id(+) * * * * * * * * * * * * * AND c.customer_id =co.customer_id(+) * * * * * * * * * * * * * AND c.customer_id =312039939) * * * * * * *ORDER BY status_rank, rnum) WHERE rnum = 1; |
#3
| |||
| |||
|
|
Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'. * That is where I am stuck. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. I'm hoping this makes sense. |
#4
| |||
| |||
|
|
On Mar 29, 11:28*am, ExecMan <artme... (AT) yahoo (DOT) com> wrote: Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'. * That is where I am stuck.. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. I'm hoping this makes sense. I'm not exactly sure I understand the bit about the delimited string, but can you union two queries, one against editor? jg -- @home.com is bogus.http://www.utsandiego.com/news/2012/...-charged-bilki... |
#5
| |||
| |||
|
|
Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'.* That is where I am stuck. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. |
#6
| |||
| |||
|
|
ExecMan <artme... (AT) yahoo (DOT) com> wrote: Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'.* That is where I am stuck. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. The traditional way is a conditional sum... with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) * , * the_people as * ( * * select 1 as person_id from dual union all * * select 2 as person_id from dual union all * * select 3 as person_id from dual union all * * select 4 as person_id from dual * ) select p.person_id, * * * *count(decode(p.person_id, t.author_id, t.article_id)) as author_ct, * * * *count(decode(p.person_id, t.editor_id, t.article_id)) as editor_ct * from the_people p * left join the_table t on p.person_id in (t.author_id, t.editor_id) *where p.person_id in (1, 2, 3, 4) *group by p.person_id *order by p.person_id; Or you can unpivot and repivot the 'Table': with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) select person_id, author_ct, editor_ct * from ( * * * * select type_cd, person_id, article_id * * * * * from the_table unpivot * * * * * * * *(person_id for * * * * * * * * type_cd in (author_id as 'A', editor_id as 'E')) * * * *) pivot (count(article_id) for * * * * * * * * type_cd in ('A' as author_ct, 'E' as editor_ct)) *where person_id in (1, 2, 3, 4) *order by person_id; -- Peter |
#7
| |||
| |||
|
|
ExecMan <artme... (AT) yahoo (DOT) com> wrote: Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'.* That is where I am stuck. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. The traditional way is a conditional sum... with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) * , * the_people as * ( * * select 1 as person_id from dual union all * * select 2 as person_id from dual union all * * select 3 as person_id from dual union all * * select 4 as person_id from dual * ) select p.person_id, * * * *count(decode(p.person_id, t.author_id, t.article_id)) as author_ct, * * * *count(decode(p.person_id, t.editor_id, t.article_id)) as editor_ct * from the_people p * left join the_table t on p.person_id in (t.author_id, t.editor_id) *where p.person_id in (1, 2, 3, 4) *group by p.person_id *order by p.person_id; Or you can unpivot and repivot the 'Table': with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) select person_id, author_ct, editor_ct * from ( * * * * select type_cd, person_id, article_id * * * * * from the_table unpivot * * * * * * * *(person_id for * * * * * * * * type_cd in (author_id as 'A', editor_id as 'E')) * * * *) pivot (count(article_id) for * * * * * * * * type_cd in ('A' as author_ct, 'E' as editor_ct)) *where person_id in (1, 2, 3, 4) *order by person_id; -- Peter |
#8
| |||
| |||
|
|
ExecMan <artme... (AT) yahoo (DOT) com> wrote: Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'.* That is where I am stuck. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. The traditional way is a conditional sum... with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) * , * the_people as * ( * * select 1 as person_id from dual union all * * select 2 as person_id from dual union all * * select 3 as person_id from dual union all * * select 4 as person_id from dual * ) select p.person_id, * * * *count(decode(p.person_id, t.author_id, t.article_id)) as author_ct, * * * *count(decode(p.person_id, t.editor_id, t.article_id)) as editor_ct * from the_people p * left join the_table t on p.person_id in (t.author_id, t.editor_id) *where p.person_id in (1, 2, 3, 4) *group by p.person_id *order by p.person_id; Or you can unpivot and repivot the 'Table': with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) select person_id, author_ct, editor_ct * from ( * * * * select type_cd, person_id, article_id * * * * * from the_table unpivot * * * * * * * *(person_id for * * * * * * * * type_cd in (author_id as 'A', editor_id as 'E')) * * * *) pivot (count(article_id) for * * * * * * * * type_cd in ('A' as author_ct, 'E' as editor_ct)) *where person_id in (1, 2, 3, 4) *order by person_id; -- Peter |
#9
| |||
| |||
|
|
ExecMan <artme... (AT) yahoo (DOT) com> wrote: Hi, I'm trying to write a single query for this and I'm kind of stuck. *I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. *I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: *'12,457,976'.* That is where I am stuck. If I query and use an OR *( *author_id IN (...) OR editor_id IN (...) *), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. The traditional way is a conditional sum... with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) * , * the_people as * ( * * select 1 as person_id from dual union all * * select 2 as person_id from dual union all * * select 3 as person_id from dual union all * * select 4 as person_id from dual * ) select p.person_id, * * * *count(decode(p.person_id, t.author_id, t.article_id)) as author_ct, * * * *count(decode(p.person_id, t.editor_id, t.article_id)) as editor_ct * from the_people p * left join the_table t on p.person_id in (t.author_id, t.editor_id) *where p.person_id in (1, 2, 3, 4) *group by p.person_id *order by p.person_id; Or you can unpivot and repivot the 'Table': with * the_table as * ( * * select 1 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 2 as article_id, 1 as author_id, 1 as editor_id from dual union all * * select 3 as article_id, 1 as author_id, 2 as editor_id from dual union all * * select 4 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 5 as article_id, 2 as author_id, 4 as editor_id from dual union all * * select 6 as article_id, 2 as author_id, 1 as editor_id from dual union all * * select 7 as article_id, 3 as author_id, 2 as editor_id from dual union all * * select 8 as article_id, 3 as author_id, 1 as editor_id from dual * ) select person_id, author_ct, editor_ct * from ( * * * * select type_cd, person_id, article_id * * * * * from the_table unpivot * * * * * * * *(person_id for * * * * * * * * type_cd in (author_id as 'A', editor_id as 'E')) * * * *) pivot (count(article_id) for * * * * * * * * type_cd in ('A' as author_ct, 'E' as editor_ct)) *where person_id in (1, 2, 3, 4) *order by person_id; -- Peter |
#10
| |||
| |||
|
|
I'm trying to write a single query for this and I'm kind of stuck. I have a table with 3 columns: ARTICLE_ID AUTHOR_ID EDITOR_ID The AUTHOR_ID and EDITOR_ID need not be the same. I would like to get a count from that table, providing a user ID, of how many articles where they are the Author, and how many where they are the Editor. It is easy with a single ID, but the application can pass in a delimited string of ID's: '12,457,976'. That is where I am stuck. If I query and use an OR ( author_id IN (...) OR editor_id IN (...) ), I will get records where the AUTHOR_ID may be in my list, but the EDITOR_ID is not. I'm hoping this makes sense. |
![]() |
| Thread Tools | |
| Display Modes | |
| |