Oracle query help

Thread Solved

Join Date: Feb 2008
Posts: 6
Reputation: mamtha is an unknown quantity at this point 
Solved Threads: 0
mamtha mamtha is offline Offline
Newbie Poster

Oracle query help

 
0
  #1
Jul 23rd, 2009
I have to create a report based on the customer for particular order.
For example find all the customers who ordered an item ‘ABC’ for current month
and look back 11 months to find out if the same customers ordered the same item
and list all the customers who ordered ‘ABC’ in last 11 months

If 1000 customers ordered an item ‘ABC’ in June 09.
Check if any of the 1000 customers ordered ABC from July 08 to May 09.
If any list the customer.

Can you plz help me write the query for this? Thanks,
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Oracle query help

 
0
  #2
Jul 23rd, 2009
I think we would need a little more details to what you are asking.

Do you want a list of all customers that purchased a specific product in the current month only IF they had purchased the same product in the past 11 months?

The product will be a provided parameter?

It also sounds like this information would be held in multiple tables, are you not interested in how to pull these together; just how to make this comparison?

If you need to know how to join the tables a schema description of the tables would be required.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: mamtha is an unknown quantity at this point 
Solved Threads: 0
mamtha mamtha is offline Offline
Newbie Poster

Re: Oracle query help

 
0
  #3
Jul 23rd, 2009
Right, list the customers if they purchased the same product in june as well as in previous 11 months.
The data is only in one table and don't need to join other tables.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Oracle query help

 
0
  #4
Jul 23rd, 2009
Try something like this

  1. SELECT customer FROM Table
  2. WHERE purchasedate BETWEEN trunc(SYSDATE,'MM') AND LAST_DAY (TO_DATE (trunc(SYSDATE,'MM')))
  3. AND product = 'ABC'
  4. INTERSECT
  5. SELECT customer FROM table
  6. WHERE purchasedate BETWEEN add_months(trunc(SYSDATE,'MM'),-11) AND trunc(SYSDATE,'MM')-1
  7. AND product = 'ABC'

This will only show a customer that has purchased a particular product in the current month as well as in the last 11 months.
Last edited by cgyrob; Jul 23rd, 2009 at 12:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: mamtha is an unknown quantity at this point 
Solved Threads: 0
mamtha mamtha is offline Offline
Newbie Poster

Re: Oracle query help

 
0
  #5
Jul 23rd, 2009
Thanks!
I was trying the sub query..
This looks like a faster version.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Oracle query help

 
0
  #6
Jul 23rd, 2009
Yes intersects work well and are much more readable.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Oracle query help

 
0
  #7
Jul 23rd, 2009
Mamtha,

If this solved your question can you please mark the thread solved.

thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 517 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Oracle
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC