Calculate Amount of Product from Northwind - sql

How can I calculate the total amount for product after discount of each EmployeeId in Northwind database of SQL Server?
The tables used from Northwind database are:
Employees
Order details
Products

If you are calculating amount for each EmployeeId
you have to use one more table for join along with these tables
Orders
Products
Employees
Order Details
and your query should be
select O.EmployeeID,OD.ProductID,Sum(OD.UnitPrice*Quantity *(1-Discount)) as TotalAmount from Orders as o
inner join [Order Details] as OD on OD.OrderID=o.OrderID
inner join Products as p on p.ProductID=OD.ProductID
group by OD.ProductID,o.EmployeeID
order by OD.ProductID

Write the code (Country, City, TotalCustomer) to show how many customers there are in each city in the country from the Northwind database

Related

How to get all Customers records in Northwind (with and without orders)

With this code I will get only customers records with total of orders more than zero, but I need get the customers with zero total of orders also.
How to get all Customers record in Northwind with and without orders?
thanks for the help.
Changing INNER JOIN to LEFT JOIN will return customers who do not have orders.
SELECT Customers.CustomerID,
Customers.CompanyName,
COUNT(Orders.OrderID) AS Total
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerID,
Customers.CompanyName
This query returns all customers (91 in the Northwind DB) and the total displays 0 for those who do not have orders.
Is this what you were after?

Select all orders by one customer

I have three tables, orders, orders_details and customers. I need to select orders by one customer for the orders table so I did this
orders columns:
id
customer_id
created
vat
discount
amount
paid
orders_details columns:
id
order_id
cost
qty
product
The SQL I used
SELECT
orders.*,
SUM(orders_details.qty*orders_details.cost) as amount,
SUM(orders_details.qty) AS qty
FROM
orders,
orders_details,
customers
WHERE
orders.customer_id = customers.id
AND orders_details.order_id = orders.id
AND orders.customer_id = 1
but I am getting a wrong qty of 30 instead of 20 and the amount is wrong
If you want to aggregate per order you need a GROUP BY clause. Also you should use proper JOIN syntax, and might consider using aliases to make the query more compact.
SELECT
o.*,
SUM(od.qty * od.cost) AS amount,
SUM(od.qty) AS qty
FROM orders o
INNER JOIN orders_details od ON od.order_id = o.id
INNER JOIN customers c ON o.customer_id = c.id -- not used, might be excluded
WHERE o.customer_id =1
GROUP BY o.id
Depending on what database system you are using you might need to include all columns referenced in o.* in the GROUP BY:
GROUP BY o.id, o.customer_id, o.created, o.vat, o.discount, o.amount, o.paid
Last note: as you don't seem to use any data from the customers table you probably could exclude that table altogether.
You're missing a GROUP BY clause which I'm guessing should be on orders.id.

I want to find that how much number of products sold for particular category in northwind database?

I have three main Tables
categories:
categoryID(primary Key)
CategoryName
Description.
OrderDetails
OrderID (primary Key)
ProductID
Quantity
Products
productId (Primary Key)
ProductName
CategoryID
Now I have to write a query to get number of products sold for particular category?
here is my attempt
SELECT Products.ProductID,
Products.CategoryID,
SUM(OrderDetails.Quantity)
FROM Products
LEFT INNER JOIN OrderDetails
ON (SELECT OrderDetails.Quantity,
orderDetails.ProductID,
categories.categoryName
FROM orderDetails
LEFT INNER JOIN Categories
ON categories.categoryID=products.CategoryId
)
ON Products.ProductId = OrderDetaqils.ProductId
i am not getting any answer close to my requirement.its not correct
please give me the solution if possible
It would be great help if you can give me the solution. If you required any additional details please let me know
here is the SQL query
SELECT Product.ProductID,
sum(Quantity) as TotalSoldAmount,
categories.CategoryID,
CategoryName
FROM OrderDetails
-- INNER JOIN over all 3 Tables
INNER JOIN Product ON Product.ProductID = OrderDetails.ProductID
INNER JOIN categories ON categories.CategoryID = Product.CategoryID
-- we need to group our result because we used sum()
GROUP BY categories.CategoryID,
CategoryName,
Product.ProductID
and her the sqlfiddle as prove

How do I use SQL to select rows that have > 50 related rows in another table?

I've been trying to find out how to write this query in sql.
What I need is to find the productnames (in the products table) that have 50 or more orders (which are in the order table).
only one orderid is matched up to a productname at a time so when I try to count the orderid's it counts all of them.
I can get distinct productnames but once i add in the orderid's then it goes back to having multiple productnames.
I also need to count the number of customers (in the order table) that have ordered those products.
I need some serious help ASAP! if anyone could help me figure out how to figure this out that would be awesome!
Table: Products
`productname` in the form of a text like 'GrannySmith'
Table: Orders
`orderid` in the form of '10222'..etc
`custid` in the form of something like 'SMITH'
Assuming the orders table has a field that relates back to the products table named ProductId. The SQL would translate to:
SELECT p.ProductName, Count(*)
FROM Orders o
JOIN Products p
on o.ProductId = p.ProductId
GROUP BY p.ProductName HAVING COUNT(*) >= 50
The key is in the having component of the Group By clause. I hope this helps.
You might be missing an "Order Details" table - typically, an order has several order details, and each of the order details then maps to a product - something like the sample in Northwind:
In that case, your SQL query would be something like this: join the [Order Details] table to both the [Orders] and [Products] tables, group by the product ID and name, and count the OrderID's:
select
p.ProductID, p.ProductName, count(o.OrderID)
from
[order details] od
inner join
orders o on od.OrderID = o.OrderID
inner join
products p ON od.productID = p.ProductID
group by
p.ProductID, p.ProductName
having
count(o.OrderID) > 50

SQL Query to find the maximum of a set of averages

This is a query based on the Northwind Database in MS SQL Server 2005.
First I have to get the average of the UnitPrice from OrderDetails table, and group it by ProductID for that particular column alone and alias it as AveragePrice.
Then I need to find the maximum(AveragePrice) which is nothing but the max of previous column, how can I do it??? This is a kind of very tricky for me its taking me ages to think on it.
select
O.CustomerID,
E.EmployeeID,
E.FirstName+space(1)+E.LastName FullName,
OD.OrderID,
OD.ProductID,
(select avg(DO.UnitPrice) from OrderDetails
DO where OD.ProductID = DO.ProductID
group by DO.ProductID) AveragePrice ,
from OrderDetails OD
join Orders O
on OD.OrderID = O.OrderID
join Customers C
on C.CustomerID = O.CustomerID
join Employees E
on E.EmployeeID = O.EmployeeID
This is not a Homework question, am learning SQL, but am really stuck at this point, please help me.
It's 2 steps: "the ungrouped maximum of the grouped averages"
You can expand this as needed which shows how to apply an aggregate on top of an aggregate
SELECT
MAX(AveragePrice) AS MaxAveragePrice
FROM
(
select
avg(UnitPrice) AS AveragePrice, ProductID
from
OrderDetails
group by
ProductID
) foo
Or with CTE
;WITH AvgStuff AS
(
select
avg(UnitPrice) AS AveragePrice
from
OrderDetails
group by
ProductID
)
SELECT
MAX(AveragePrice) AS MaxAveragePrice
FROM
AvgStuff