Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 13th, 2006, 08:51 AM
Friend of Wrox
 
Join Date: Feb 2006
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
 
Old May 15th, 2006, 05:52 AM
Friend of Wrox
 
Join Date: May 2006
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

 
Old May 18th, 2006, 07:18 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

PESO,Thank you very much.





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





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