If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Query 5% of each other

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-08, 19:49
lonrot lonrot is offline
Registered User
 
Join Date: Oct 2008
Posts: 2
Query 5% of each other

Hi:

Currently i am doing an application which administers sales of certain products. I am trying to make a report that shows all pairs of salesman whose sales were 5% of each other on every product, any ideas on how to do this??

the tables look something like this
Inventory:
___________________
|ID-I | Description |
-----------------------------
|1a | Pen |
|2a | Pencil |
|3a | Eraser |
-----------------------------

Sales:
___________________
|ID-S | ID-I | Quantity|
-----------------------------
|1 | 1a | 40.00 |
|1 | 2a | 24.00 |
|1 | 3a | 30.00 |
|2 | 1a | 40.00 |
|2 | 2a | 22.00 |
|2 | 3a | 30.00 |
|3 | 2a | 30.00 |
------------------------------
Employee
__________________________
|ID-S | Name |
------------------
|1 | John |
|2 | Jane |
|3 | Moe |

So the result that i am looking would look something like
(John, Jane)
(Jane, John)

Moe wouldn't be answer since he didnt sale the same products as john and jane he still needs to sale pens and erasers even though he sold the same quantity as John and Jane for erasers

thanks
lonrot
Reply With Quote
  #2 (permalink)  
Old 10-25-08, 19:51
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
What school is this for?
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 10-26-08, 05:15
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by lonrot
... all pairs of salesman whose sales were 5% of each other on every product
Try a self-join with these two conditions (equality on product, and "sales1 > 1.05*sales2") as the join condition.
You'll probably also need some kind of "group by" (& having count(*) = 3) if by "every product" you really mean all 3 products.
Try building up the query logic step by step, by using CTEs.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 10-26-08 at 05:34.
Reply With Quote
  #4 (permalink)  
Old 10-26-08, 11:51
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by Peter.Vanroose
Try a self-join with these two conditions (equality on product, and "sales1 > 1.05*sales2") as the join condition.
You'll probably also need some kind of "group by" (& having count(*) = 3) if by "every product" you really mean all 3 products.
Try building up the query logic step by step, by using CTEs.
What makes you think he/she is using DB2?
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 10-26-08, 15:47
lonrot lonrot is offline
Registered User
 
Join Date: Oct 2008
Posts: 2
thanks Peter.Vanroose that gave me a good start and i can figure it out from there. I am going to do a subquery where i get only the salesman who sold the same products to get the for "every product"
Reply With Quote
  #6 (permalink)  
Old 10-26-08, 17:58
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Marcus_A
What makes you think he/she is using DB2?
Because this is the DB2 forum.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On