Question

I am still a noobie when it comes to the SQL server. I know something but other still seem so confusing. I know the more you practice with it the better you get. Well I am working on this part that kinda has my mind confused. It is using northwind database show the most recent five orders that were purchased from a customer who has spent more than $25,000. Ok I know I will use the customer table, and the order table. What is putting my mind crazy is how do you get it where the 5 customers who spent 25,000 up. I am sure I probably know it but my mind is not thinking it does.

The reason why I think this one is hard for me is because I have a order detail which has the OrderID, ProductID, UnitPrice,Quantity, and discounts in it.

then you have Orders which don't have any information with it that deals with the money issue. Then you have the customers which I know I will need to use that to show the 5 customers who spent more than 25,000.

With the Order Detail I don't know how to use it that well because they are split into two names. I am woundering can I put a bracket around it since there is a space between the two words.

I am sure people are going to vote this down or say i seen this on SQL dummies but I am just trying to understand this. I know you can not see my database to see what I am looking at but I will also try in my best to explain why I am so confused. I am just trying to understand this. Thanks to all who looked at this topic.

This is what I have so far I know I am missing to add the 5 in there I am just not understanding where too.

  SELECT ContactName FROM Customers
    INNER JOIN [Order Details]ON OrderId = 
CustomerID
INNER JOIN Orders ON Product.ID = Orders.ID
WHERE UnitPrice >= 25000
Was it helpful?

Solution

You need to join on a subquery from order details so you can get the order total.

select Top 5 [Customer].ContactName,[Order].OrderDate,detail.total
from [Customer] inner join
[Order] on [Customer].CustomerID = [Order].CustomerID inner join
(SELECT [OrderID],SUM(([UnitPrice]*[Quantity])-[Discount]) as total FROM [OrderDetail] GROUP BY [OrderID]) as detail on [Order].OrderID = detail.OrderID
WHERE detail.total > 25000
ORDER BY [Order].OrderDate DESC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top