MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 20th, 2004, 01:57 PM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Extreme Noob Help needed!!!

hi ,
was wondering if anyone could help me with a few novice SQL questions.
Simply, this is my prob:

Using JOINS, I need to display 3 columns, cust_name, order_num and TOTAL order cost (need to run a SUM command here I think)(all column items come from separate tables)

also, same as above except I need to display one order per customer and order shown should be the highest numbered order.

any help would be greatly appreciated thanks!

Reply With Quote
  #2  
Old May 21st, 2004, 01:24 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
can you post the tables and their fields w/a little description of each?...we'll go from there.

Reply With Quote
  #3  
Old May 21st, 2004, 09:07 AM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
Forgot to include that info, here it is:

Table 1 - Customers:
`cust_id`, `cust_name`, `cust_address`, `cust_city`, `cust_state`, `cust_zip`, `cust_country

Table 2 - Orderitems:
`order_num`, `order_item`, `prod_id`, `quantity`, `item_price`

Table 3 - Orders:
`order_num`, `order_date`, `cust_id` FROM `Orders`

Table 4 - Products:
`prod_id`, `vend_id`, `prod_name`, `prod_price`, `prod_desc`

Table 5 - Vendors:
`vend_id`, `vend_name`, `vend_address`, `vend_city`, `vend_state`, `vend_zip`, `vend_country

So far this is what I think is what I should be doing:
(Original question -
Using joins, display three columns: Customer Name, Order Number, and Total Order Cost.)

SELECT cust_name, order_num, SUM(item_price*quantity) AS Total
FROM OrderItems, Customers, Orders
WHERE Orders.order_num = OrderItems.order_num
GROUP BY cust_name


of course this doesn't seem to work....
thanks in advance!

Reply With Quote
  #4  
Old May 21st, 2004, 11:35 AM
Gerbill's Avatar
Gerbill Gerbill is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Canada
Posts: 243 Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 6 m 57 sec
Reputation Power: 8
First of you need to specify from which table you are retrieving "order_num" value from, and also you need all values you are using except the sum inside the Group By, and as well you need another Where condition to connect the 3rd table... you can use a join but it is not necessary

<b>i.e. </b>

Select cust_name, Orders.order_num, SUM(item_price*quantity) AS Total
FROM OrderItems, Customers, Orders
WHERE (Orders.order_num = OrderItems.order_num) AND (Orders.cust_id = Customers.cust_id)
GROUP BY cust_name, Orders.order_num

hope that helps,
Gerbill

Reply With Quote
  #5  
Old May 21st, 2004, 12:24 PM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks Gerbill!!!

very much appreciated,
last thing if you could.

How would I go about and only display one order per customer, displaying the highest-numbered Order Number (per cust)?

Could I use the DISTINCT and NULL commands for this query?

thanks again!

Reply With Quote
  #6  
Old May 21st, 2004, 12:46 PM
Gerbill's Avatar
Gerbill Gerbill is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Canada
Posts: 243 Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 6 m 57 sec
Reputation Power: 8
You mean that you want the "order_num" to be sorted descending (largest # first) but you only want the first one ...the largest one

Reply With Quote
  #7  
Old May 21st, 2004, 12:52 PM
Gerbill's Avatar
Gerbill Gerbill is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Canada
Posts: 243 Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level)Gerbill User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 6 m 57 sec
Reputation Power: 8
Yeah it is possible ...I do believe good use of the DISTINCT would make a difference here.

You can select a distinct column then use...

Order By "Name of distinct column" DESC

i.e. Order By Orders.order_num DESC

Reply With Quote
  #8  
Old May 21st, 2004, 10:13 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 22 sec
Reputation Power: 870
"the highest-numbered Order Number (per cust)" is easily produced with a subquery

but centricle, what are those backticks doing around your column names? this is the microsoft sql server forum, and those backticks are only valid in mysql

if i gave you a subquery solution, will you be able to run it?
__________________
r937.com | rudy.ca

Reply With Quote
  #9  
Old May 22nd, 2004, 01:24 AM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
"the highest-numbered Order Number (per cust)" is easily produced with a subquery

but centricle, what are those backticks doing around your column names? this is the microsoft sql server forum, and those backticks are only valid in mysql

if i gave you a subquery solution, will you be able to run it?

the backticks are just the way the data was pulled off the DB (was only for display purposes).

And yes it is MS SQL, and I could run your subquery (hopefully )

Reply With Quote
  #10  
Old May 22nd, 2004, 03:01 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 22 sec
Reputation Power: 870
the backticks are just the way the data was pulled off a mysql db

this sounds suspiciously like someone's homework assignment

if you hand this in, you must be prepared to explain the use of the second LEFT OUTER
Code:
select C.cust_name
     , O.order_num
     , sum(OI.item_price*OI.quantity)
  from Customers C
left outer
  join Orders O
    on C.cust_id = O.cust_id
left outer
  join Orderitems OI
    on O.order_num = OI.order_num
 where O.order_num
     = ( select max(order_num)
           from Orders
          where cust_id = C.cust_id )
group
    by C.cust_name
     , O.order_num  

Reply With Quote
  #11  
Old May 22nd, 2004, 10:54 AM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by r937
this sounds suspiciously like someone's homework assignment


Hi r937,
well it's not exactly homework its more like questions I compiled myself for my own practise purposes for a new assignment at work.
I'm sorry for the shadiness in my description but this is my 1st week learning SQL.

Since I don't have access to the actual live DB I am practicing with a local test DB (using a SQL explorer/test utility called QTADO - thats where those "ticks" come from).
The actual DB loaded is an ACCESS file but the final server is MS SQL / ORACLE.

I will try the above and let you know, thanks again for the help.

Reply With Quote
  #12  
Old May 22nd, 2004, 11:15 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 22 sec
Reputation Power: 870
you meant this one? http://gpoulose.home.att.net/

okay, thanks

see, ya learn sumpin every day on this goldurned interweb

Reply With Quote
  #13  
Old May 22nd, 2004, 11:16 AM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yes

Reply With Quote
  #14  
Old May 25th, 2004, 03:20 PM
quadricle quadricle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 quadricle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Hi r937,
I managed to get my query to run (since your suggestion didn't execute properly on my setup but opened up many ideas) - my query resulted in this:

SELECT C.cust_name, O.order_num,
sum(OI.item_price*OI.quantity)AS Total_Order
FROM Customers AS C, Orders AS O, Orderitems AS OI
WHERE C.cust_id = O.cust_id
AND O.order_num = OI.order_num
AND O.order_num = ( select max(order_num)
FROM Orders
WHERE cust_id = C.cust_id )
GROUP BY C.cust_name, O.order_num


Thanks again for the help!

Till the next problem!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Extreme Noob Help needed!!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump