Query for total number of orders with the same ID - sql

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

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;

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

SQL Using sum command with "in" clause

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

SQL Server query to further summarize grouped data

Assume a table named transactions with two columns: invoiceNumber and itemNumber. Multiple quantities of an item on a single invoice are reflected by multiple records in the table. (I know this isn't an appropriate design, but I'm simplifying a more complex structure to get at the root question.)
I can determine the average number of unique items for each invoice with a query like:
SELECT invoiceNumber, COUNT(DISTINCT itemNumber)
FROM transactions
GROUP BY invoiceNumber
This query effectively ignores the quantity of an item, counting each one only once per invoice and shows the result for each invoice.
Instead of all this detailed information, however, all I really want is to determine the average number of unique items across all invoices. That is, I just want to summarize the per-invoice information. How do I do that?
You can aggregate the result you've already figured out how to obtain.
WITH DistinctCounts AS (
SELECT invoiceNumber, COUNT(DISTINCT itemNumber) AS distinctItems
FROM transactions
GROUP BY invoiceNumber
)
SELECT AVG(distinctItems)
FROM DistinctCounts
Select avg(numberininvoice)
From
(
Select invoicenumber, count(itemnumber) as numberininvoie
From
(Select distinct invoicenumber, itemnumber
From transactions) a
Group by invoicenumber
) b

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.