SQL Using sum command with "in" clause - sql

I have sql tables listed as picture below.
And my Query is :
Select * from tblOrderDetails where OrderID in
(Select OrderID from tblOrders where CustomerID = 523456)
Which returns values from tblOrderDetails like
Query brings all sale details for selected customer as expceted. But I want to use SUM on rows which has same ProductID. Expected output should look like this :
Rows with same productID, quantity and price gets summed and also rows are merged. But it must be done within selected CustomerID.
I've tried SUM command with many different ways but can't get it to work. Please suggest. Thanks.

It sounds like you don't know about the GROUP BY clause.
Perhaps this is what you are trying to do?
Select CustomerID,ProductId,SUM(Quantity) Quantity,SUM(TotalPrice) TotalPrice from tblOrderDetails where OrderID in (Select OrderID from tblOrders where CustomerID = 523456)
GROUP BY CustomerID,ProductId
This groups everything by same customer then by same products, allowing the SUMs only on those merged subsets

You'll be wanting to use SUM and GROUP BY to attain the results you're looking for.
SELECT ProductID, SUM(Quantity) AS Quantity, SUM(TotalPrice) AS TotalPrice
FROM tblOrderDetails
WHERE OrderID IN
(
SELECT OrderID
FROM tblOrders
WHERE CustomerID = 523456
)
GROUP BY ProductID

Related

How to show summ of several variables. PostgreSQL

Good day.
I have 2 tables in psql db.
1st orders: orderid, status
2nd goods: orderid, price
Orderid goods = orderid from orders.
I can have one order (orderid not replies in orders table) but i can have several goods with one orderid.
I want to see with SELECT next view:
Orderid, allprice (if we have several goods, their prices must be summaruzed to one).
How i can do it?
Use SUM aggregate function and GROUP BY clause:
SELECT
orderid,
SUM(price)
FROM
orders
JOIN goods
USING (orderid)
GROUP BY
orderid;

List orderId, Number of Order Items, and Total Order Price - not a GROUP BY expression

I know the "not a GROUP BY expression" has been asked, but mine seems to be very different.
I'm trying to get a list of data from a view but I am getting the "not a GROUP BY expression" error.
Code:
Select orderid, count(orderid) as Number_Of_Order_Items , count(orderid) * orderprice as Total_Order_Price
from orderdetail
Group by orderid
order by orderid
Basically I am expecting to get the orderID, how many times that order ID appears and then multiply the number of times the orderID exists by the OrderPrice.
I have all of the non-aggregate clauses in my Group By, so what is happening here?
Just use sum() on the price:
Select orderid, count(orderid) as Number_Of_Order_Items,
sum(orderprice) as Total_Order_Price
from orderdetail
Group by orderid
order by orderid;
Note: This assume that orderprice is really a separate price on each detail record that needs to be summed up. In some cases, there might be a quantity column as well -- but then I would expect the column to be called something like productprice or unitprice.

Grab ordered records grouped by ID where the first record of that group == 10

Using W3 Schools SQL Server Query Tool I remove the contents and enter the following:
SELECT * FROM OrderDetails
ORDER BY ProductID, Quantity;
And click Run SQL. It nicely orders by the ProductID and then for tie breakers on ProductID It orders by Quantity.
Here is what I want to do. With the table ordered as above: I want to group together the ProductID's on this table, and then return only those ProductID groups where the first record of each group's Quantity amount is == 10.
I attempted something like this but it doesn't work:
SELECT * FROM OrderDetails
ORDER BY ProductID, Quantity
Group By ProductID
Having first(Quantity == 10);
If I read this correctly, you want all the products that have 10 as the smallest quantity that has been ordered in productId order.
SELECT productId, min(quantity) as first FROM OrderDetails
group by productId
having first = 10
ORDER BY ProductID
EDIT to answer comment below.
The same logic will work with a string column. In this example, I'm showing all suppliers where the first product (with products listed alphabetically) starts with a C (try it in the W3 query tool). If I had data that any repeating string data, I could have done an =.
SELECT supplierId, min(productName) as first from products
group by supplierId
having first like 'C%'
order by supplierId

Query for total number of orders with the same ID

Apologies if this is simple but I have just started learning SQL. In my Access database, I have a table that displays a list of orders:
I can filter the table down to just display the OrderID next to the Quantity of items like this:
SELECT OrderID, Quantity FROM OrderItems;
However in the table there are multiple OrderID's that are the same. How do I tell the database to total up the quantity of orders that relate to each OrderID?
Is is almost how you say it. You have to group the records by id and sum up the quantities. With SQL you do the following
SELECT
OrderID, <-- you get the OrderID that is grouped
SUM(Quantity) <-- you sum the quantities
FROM
OrderItems
GROUP BY
OrderId <-- you group the records by OrderId

SQL, DISTINCT product_id, orderQuantity

Here is the given question:
Show me a list of the product codes along with the number of times that each code has been ordered (note, order quantity does affect the number of total times ordered); only include product codes that have been ordered at least once?
I have tried the following
SELECT DISTINCT productId, orderQuantity
FROM Order
WHERE (orderQuantity > 0);
The table comes up with duplicate product ids. How do I get a table of distinct product ids with the SUM of their order quantities?
I think this is what you're looking for:
SELECT productID, SUM(orderQuantity)
FROM Order
WHERE orderQuantity > 0
GROUP BY productID
You have to use an aggregate function, in this case SUM(), to have the sum of your quantities and you want the sums to be grouped by each product, in your case productID.
To understand how this works, just try removing the GROUP BY statement and the productID from the SELECT statement and the query will return the SUM of all quantities.
SELECT SUM(orderQuantity)
FROM Order
WHERE orderQuantity > 0
Then, if you add just the GROUP BY productID you will get the sum of quantities ordered for each product.
SELECT SUM(orderQuantity)
FROM Order
WHERE orderQuantity > 0
GROUP BY productID
Then just add the productID back in the SELECT statement to understand the information displayed.