How to show summ of several variables. PostgreSQL - sql

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;

Related

Using a COUNT in a WHERE Clause to find Products Purchased By One Customer

I have a PostgreSQL query similar to the following:
SELECT product, customer, COUNT(customer)
FROM table
WHERE COUNT (customer) = 1
I want the query to return a list of products that have only been bought by one customer. How can I do this without using a COUNT in there WHERE?
Many Thanks.
I see. You want products purchased by only one customer. The idea is:
SELECT product, MAX(customer) as customer, COUNT(*)
FROM table
GROUP BY product
HAVING MIN(customer) = MAX(customer);
Because there is only one customer, MAX(customer) is that customer. COUNT(*) is the number of products the customer purchased.
You must group by product and use a HAVING clause where you check if the product has been bought by only 1 customer:
SELECT product,
MAX(customer) AS customer -- you may remove this column if you don't need the customer also
FROM table
GROUP BY product
HAVING COUNT(DISTINCT customer) = 1

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 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, 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.