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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Cant think how to do....

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-04, 03:54
mbrindley mbrindley is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Cant think how to do....

Hi guys..if anyone has the time, could some1 help me with a querie i am trying to do on my DB..i attached the tables..and well,

i am trying to do...given the start and end dates, produce report showing the performance of each sales rep. over that period. should begin with the rep who has most orders by value and include total units sold and total order value

geez, just writing what i wanna do sounds complicated.
if anyone can gimme a hand it would be much appreciated!
thanks
Attached Files
File Type: sql cw2tables.sql (1.6 KB, 80 views)
Reply With Quote
  #2 (permalink)  
Old 03-24-04, 07:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh, this sounds so much like a school assignment

the only way i help people with homework is if they've already written the sql and shown their thinking process, so that i can offer corrections or suggestions for improvement
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-24-04, 10:28
mbrindley mbrindley is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
Hi, yes it is a question sheet i have to do...and i understand why you said what you said...so ill try to explain what i understand from the question and hopefully you can help me

well from what i can see, i am going to have to join the salesRep table with ShopOrder. and use the SalesRepID as the link. but, its what comes next that is just too mixed up for me....i am guessing i have to do a sum of the salesrep...that then mean i have to link to the orderline table to see the quantity right??
hmmmm, ok....i am gonna have another think..i am talking rubbish!
ill edit this in a bit.....tbc
Reply With Quote
  #4 (permalink)  
Old 03-24-04, 11:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you're on the right track

join SalesRep to ShopOrder

can't stop there, must also join to OrderLine, to get the value of Quantity*UnitSellingPrice

now sum those up, so we need a GROUP BY

how about by SalesRep.Name

don't forget to sum the number of units sold, too
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-25-04, 19:27
mbrindley mbrindley is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
ok, this is where i am at...thanks for the help


SELECT salesRep.salesRepId, salesRep.Name, SUM(Quantity*UnitSellingPrice) AS "units sold"
WHERE salesRep.salesRepID = ShopOrder.salesRepID, orderLine.ShopOrderID = ShopOrder.ShopOrderID
FROM salesRep, ShopOrder, OrderLine
GROUP BY salesRep.Name;

something is wrong there right??
thanks for the guidance
Reply With Quote
  #6 (permalink)  
Old 03-25-04, 19:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
WHERE comes after FROM

clauses in the WHERE must be separated by ANDs, not commas

alternative: use JOIN syntax, and replace your FROM and WHERE with this:
PHP Code:
select ...
  
from salesRep
inner
  join ShopOrder
    on salesRep
.salesRepID ShopOrder.salesRepID
inner
  join OrderLine
    on ShopOrder
.ShopOrderID orderLine.ShopOrderID 
group
    by 
... 
the GROUP BY must have all the non-aggregate columns in the SELECT list
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-25-04, 20:08
mbrindley mbrindley is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
i think i understand that way of joining......this way is better?
i am getting a error msg:

PHP Code:
mysqlselect salesRep.salesRepIdsalesRep.Name
    
-> from salesRep inner join ShopOrder on salesRep.salesRepID ShopOrder.sal
esRepID
    
-> inner join OrderLine on ShopOrder.ShopOrderID orderLine.ShopOrderID
    
-> group by salesRep.salesRepIdsalesRep.Name;
ERROR 1109Unknown table 'orderLine' in on clause 
i didnt put the quantity yet because i want to check that it works...am i way off?
Reply With Quote
  #8 (permalink)  
Old 03-25-04, 23:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
case sensitive table name

sorry, that was my bad

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 03-25-04 at 23:29.
Reply With Quote
  #9 (permalink)  
Old 03-26-04, 00:06
mbrindley mbrindley is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
thats ok, i cant believe i didnt spot it!!!! must cos my brain has turned into jello!!!.....

select salesRep.salesRepId, salesRep.Name, SUM(Quantity*UnitSellingPrice) AS "Total"
from salesRep inner join ShopOrder on salesRep.salesRepID = ShopOrder.salesRepID
inner join OrderLine on ShopOrder.ShopOrderID = OrderLine.ShopOrderID
group by salesRep.salesRepId, salesRep.Name;

i gota that working....but i am having trouble understanding, or breaking down the question..ie what it wants, its confusing me!! this bit..

" the rep who has most orders by value and include total units sold and total order value "

thanks for all the help...ur a star
Reply With Quote
  #10 (permalink)  
Old 03-26-04, 00:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
"begin with the rep who has most orders by value"

i.e sort the results by Total descending

don't forget your other sum for number of units
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 03-26-04, 01:12
mbrindley mbrindley is offline
Registered User
 
Join Date: Mar 2004
Posts: 16
select salesRep.salesRepId, salesRep.Name, SUM(Quantity*UnitSellingPrice) AS "Total", SUM(Quantity) AS "Units", SUM(OrderLine.ShopOrderID) AS "Total Orders"
from salesRep inner join ShopOrder on salesRep.salesRepID = ShopOrder.salesRepID
inner join OrderLine on ShopOrder.ShopOrderID = OrderLine.ShopOrderID
group by salesRep.salesRepId, salesRep.Name
ORDER by "Total" DESC;

i think i got it ??? right...seems to work....

now to do the bit about given start and end dates?
i tried putting a WHEN and giving dates but it didnt work
honestly i do try before bugging you, i feel useless having to ask so much......but thanks, if you have time to show me, brilliant, thanks...
Reply With Quote
  #12 (permalink)  
Old 03-26-04, 06:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
not WHEN, WHERE

and if fred dobbs sold an order with ShopOrderID = 21, if there were 5 items in that order, his "Total Orders" would be 105, and would be wrong
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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