Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 13th, 2006, 08:51 AM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default Select from Two Tables Problem

Hello Friend.
There are two tables as follow:
Table name --> Emp
   CustID EmpName EmpCity
     001 Joy Agra
     002 Sim Delhi
     003 Kim Goa

Table name --> Order
    ItemID CustId Quantity Price
     1001 001 2 $5
     1001 001 4 $10
     1003 003 5 $20

Now, I want to display the names of those customers who have ordered atleast one item(example:joy and Kim).Also If any customer has ordered same item twice, I want to display the Total_Quantity and Total_Price.The required output is as follow:
     EmpName ItemID Total_Quantity Total_Price
      Joy 1001 6 $15
      Kim 1003 5 $20

I also have another problem. I want to display the names of those customers who have placed a single order(example: Kim).

Please tell me the queries for achieving the specified result.
Thank you, Waiting for some quick responses.

Gaurav Jain.
__________________
Gaurav
Reply With Quote
  #2 (permalink)  
Old May 15th, 2006, 05:52 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

At least one item is solved with this code
----------------------------------------------------
SELECT Emp.EmpName,
        Order.ItemID,
        SUM(Order.Quantity) Total_Quantity,
        SUM(Order.Price) Total_Price
FROM Emp
INNER JOIN Order ON Order.CustID = Emp.CustID
GROUP BY Emp.EmpName,
        Order.ItemID
----------------------------------------------------

A single order is solved with adding HAVING clause
----------------------------------------------------
SELECT Emp.EmpName,
        Order.ItemID,
        SUM(Order.Quantity) Quantity,
        SUM(Order.Price) Price
FROM Emp
INNER JOIN Order ON Order.CustID = Emp.CustID
GROUP BY Emp.EmpName,
        Order.ItemID
HAVING COUNT(*) = 1

Reply With Quote
  #3 (permalink)  
Old May 18th, 2006, 07:18 AM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

PESO,Thank you very much.
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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Count from multiple tables aspless SQL Language 1 January 30th, 2008 10:32 AM
list all tables using SELECT Statement rylemer Access 5 March 7th, 2005 08:58 PM
Select to all user tables in db Moueg SQL Server 2000 5 December 13th, 2004 10:56 AM
Select lot tables... vieritlc Classic ASP Databases 3 May 18th, 2004 07:07 AM
Grant SELECT on multiple tables oidhche SQL Server 2000 3 June 16th, 2003 06:26 PM



All times are GMT -4. The time now is 07:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.