Wrox Programmer Forums
|
BOOK: Beginning SQL Server 2005 Programming ISBN: 978-0-7645-8433-6
This is the forum to discuss the Wrox book Beginning SQL Server 2005 Programming by Robert Vieira; ISBN: 9780764584336
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning SQL Server 2005 Programming ISBN: 978-0-7645-8433-6 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 June 13th, 2007, 11:14 AM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch 7 Question 3 order totals in results

I'm trying to get the total order amounts to appear along with the top 5 customers who spent over $25k.

SELECT TOP 5 o1.CustomerID, o1.OrderDate,
(SELECT SUM(od1.UnitPrice * od1.Quantity *(1-od1.Discount))
FROM [Order Details] od1
WHERE o1.OrderID = od1.OrderID) AS "Totals"
FROM Orders o1
WHERE o1.CustomerID IN
(SELECT c.CustomerID
FROM Customers c
JOIN Orders o
    ON c.CustomerID = o.CustomerID
JOIN [Order Details] od
    ON o.OrderID = od.OrderID
GROUP BY c.CustomerID
HAVING SUM(UnitPrice * (1-Discount) * Quantity) > 25000
)
ORDER BY o1.OrderDate

I keep getting weird numbers that aren't over 25,000. Have I done something wrong? Is there a better way to do this (without going beyond the scope of the book thus far)?

Thanks!
 
Old September 26th, 2008, 07:42 PM
Registered User
 
Join Date: Sep 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi fordpickup,

I have a solution but it is quite a lenghty code involving Views and derived tables. I imagine that there may be a much shorter way out. You can Select as many columns as you want to see at the last batch. Pls send a feed back and let's know how it goes.


USE Northwind
GO

CREATE VIEW LatestOrders
AS

SELECT TOP 5 *
FROM Orders oo

WHERE CustomerID IN
(SELECT c.CustomerID
FROM Customers c
JOIN Orders o
    ON c.CustomerID = o.CustomerID
JOIN [Order Details] od
    ON o.OrderID = od.OrderID
GROUP BY c.CustomerID
HAVING SUM(UnitPrice * (1-Discount) * Quantity) > 25000
)
ORDER BY OrderDate

GO

CREATE VIEW TopOrders
AS

SELECT CustomerID,Total FROM
(SELECT Orders.CustomerID,SUM(([Order Details].Quantity * [Order Details].UnitPrice) *
 (1 - [Order Details].Discount))
                      AS Total
FROM [Order Details] INNER JOIN
                      Orders ON [Order Details].OrderID = Orders.OrderID INNER JOIN
                      Customers ON Orders.CustomerID = Customers.CustomerID
GROUP BY Orders.CustomerID)AS dt1

WHERE Total >25000

GO

SELECT lo.OrderDate,tpo.CustomerID,tpo.Total
FROM TopOrders tpo
JOIN
LatestOrders lo
ON lo.CustomerID = tpo.CustomerID

GO

--Sylva.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 2 Try It Out Example 2-4. Different results. VictorVictor BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 5 March 27th, 2006 12:12 PM
new ch. 6 question Dennis Mollet BOOK: Beginning VB.NET Databases 2 August 16th, 2005 05:27 PM
Sort order Question Tere Crystal Reports 1 February 14th, 2005 03:18 PM
GROUP BY and ORDER BY question savoym SQL Language 6 June 24th, 2003 08:24 AM





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