How to add values of the same item in a column? - sql

I have the following columns in a table:
Product Name
Quantity
Description
I want to add the quantities of the product if the name of the product is same. For example if I have product with the same name twice, I want the total quantities of the products to be added and get a result.
This is the table and I want to add the quantities of the same item:
Name Quantity Description
Pen 3
Pencil 2
Pen 6
Eraser 7
Eraser 6
For exmaple:
I have pen twice and so want to add (3+6) and the display the total as 9, and ...
I have eraser twice (7+6), so the total should be 13.

The solution is GROUP BY clause:
SELECT Name, SUM(Quantity)
FROM Table
GROUP BY Name
GROUP BY is used in an SQL query to group rows together for aggregation purposes.
For example:
If your input table had rows:
ItemName Qty
Pen 4
Pencil 7
Pen 6
SQL Code:
SELECT ItemName, SUM(Qty)
FROM Table
GROUP BY ItemName
Using GROUP BY ItemName in your query would give you the output:
ItemName Qty
Pen 10
Pencil 7

SELECT SUM(column_name) FROM table_name
This is the best example!

Related

SQL how to return column values with same name only once

I have a table containing 3 columns consisting of a product (varchar), quantity(integer), price(integer). I want to get the total price of the products and return the product and the total price and ordered by descending alphabetical order. But if the product is repeated I only want to return the product once with its total price.
Heres the table:
product
quantity
price
Pencil
2
4
Notebook
3
5
Notebook
5
5
In this case desired output would be :
product
totalprice
Notebook
40
Pencil
8
My current output is this:
product
totalprice
Notebook
15
Notebook
25
Pencil
8
My query is the following:
SELECT product, quantity * price AS totalprice
FROM schoolproducts
ORDER BY product DESC
This is a case for aggregation:
SELECT product, sum(quantity * price) AS totalprice
FROM schoolproducts
GROUP BY product
ORDER BY product DESC

How to merge two rows and sum the columns

product
quantity
price
milk
3
10
bread
7
3
bread
5
2
And my output table should be
product
total_price
milk
30
bread
31
I can't seem to get my code to work. Here is my code
SELECT product, (SELECT (quantity*unit_price)
FROM shopping_history AS sh ) AS total_price
FROM shopping_history
GROUP BY product
You are looking for the aggregate function SUM (which doesn't require a sub-query) e.g.
SELECT product, SUM(quantity*unit_price) AS Total_Price
FROM shopping_history
GROUP BY product

Count all of a column where value is 2 and sum this value with price

I'm doing with Northwind database where I use the Products table. I need to count all of the rows where Category_Id is 2 and sum the amount with the prices.
Here's the example of a table shortly:
Category_ID | Unit Price
1 | 2,90
2 | 3,70
3 | 4,90
2 | 1,90
5 | 0,90
2 | 2,90
There are 3 rows where category_Id is 2. How to sum this 3 with that rows Unit price?
3,70 + 1,90 + 2,90 = 8,50
So the answer I need is 8,50 but I have no idea how to get that amount with a SQL query.
Does someone know?
you can get the aggregated values for all Ids using
Select Categeory_Id, sum([Unit Price]) Total, count(*) Qty
from Products
group by Category_Id
or just a specific total such as
select sum([Unit Price]) total
from products
where category_Id=2

COUNT OF in SQL Query?

I only want the count of Brands (Blue, White, Red) if their corresponding Customer value is listed more than once.
*customer* *productid* *brand*
1 A Red
2 B Blue
1 A Red
2 C Blue
3 B White
1 A Red
2 B Blue
Desired result: I want a single dataset that has the Brands with their tallied count, only for Customers that are repeat purchasers.
*brands* *repeat_purchase*
Red 3
Blue 2
select customer, productid, count(productid) as repeat_purchase
from Public."CustomerData"
group by customer, productid
having count(productid) > 1;
Above is what I have so far, but I can't figure out how to get just two columns: One with the name of each Brand, and one with the total number of times a that each brand is included in a repeat purchase.
Your question seems to want two levels of aggregation:
select brand, sum(cnt)
from (select customer, product, brand, count(*) as cnt
from Public."CustomerData"
group by customer, product, brand
having count(*) >= 2
) t
group by brand;

SQL: How to display items from one table according to categories from another table, and counting the total items in each category

I have two tables, a category table, and a items table. The category table have the following fields:
project_No
cat_ID
cat_Description
The items table has the following fields:
project_No
cat_ID
item_Id
item_description
item_Qty
item_cost
Now I need to write a query that displays all the items for each category, but I also want to count the amount of items in each category
Now the output must first list the category before the items in that catgegory and then list all the items in that category, and giving a total for the amount of items in that category, then another category and the items, for example
Beverages
Coffee $1.50 4
Tea $2.50 4
Total Items 2
Tin Food
Peas $0.50 10
Meatballs $1 20
total items 2
I need to write this SQL and place it inside my TADOQuery component in Delphi
Please can anyone assist me
Select category_table.Cat_Description, item_table.Item_Desctiption, SUM(Item_table.Item_Cost) as Total_Cost, SUM(item_Table.Item_Qty) as Total_Qty
From Items_table join Category_Table
on items_Table.Cat_Id = Category_Table.Cat_Id and items_Table.Project_no = Category_table.Project_no
This would get you the result:
Beverages Coffee $1.50 4
Beverages Tea $2.50 4
Tin Food Peas $0.50 10
Tin Food Meatballs $1.00 20
I summed your cost, but if you just want cost listed, remove the sum from the option.