Sum over rows to result in columns in a table - sql

I have a table like this:
Product
Type
Price
M&Ms
Peanut
2.50
M&Ms
No_Peanut
2.40
Assuming multiple products and types, I want to summarize price (Total price) over each product name but have it placed in a separate table like this:
Product
Total_price
M&Ms
4.9
I know there's analytical SQL functions like OVER but I wasn't sure how to make a separate table.

TO make seprate table use this
SELECT Product, SUM(Price) AS Total_price
INTO schema.newtable FROM schema.ProductTable
Group by Product;

This is a simple aggregation:
select prodct, sum(price) as total_price
from products
group by product
order by product;
And the result is a table. Only it is not a table stored in your database. It is usually not desired to store such an additional table, because then you'd have redundant information in your database, which can lead to inconsistencies later.
But if you want to, you can use above query to create a table:
create new_table as
select prodct, sum(price) as total_price
from products
group by product;
Update: SQL Server does not support the standard SQL syntax for creating a table from a query. The syntax in SQL Server is:
select prodct, sum(price) as total_price
into new_table
from products
group by product;

The query is just a simple GROUP BY:
SELECT Product, SUM(Price) AS Total_price
FROM Table1
GROUP BY Product;
If you want to insert it into a separate table that already exists, just use an INSERT combined with that query:
INSERT Table2 (Product, Total_price)
SELECT Product, SUM(Price) AS Total_price
FROM Table1
GROUP BY Product;
Or, to create a new table on the fly:
SELECT Product, SUM(Price) AS Total_price
INTO Table2
FROM Table1
GROUP BY Product;

Related

Finding the maximum price for a given customer id

I need to write a hive query. I am working on a data set that has three columns : Customer ID, Product ID and the Price. I need to write a query which outputs the columns Customer ID and Product ID for the maximum item bought by the customer.
SELECT [customer], [product] FROM table WHERE [price] = (SELECT MAX(t.[price]) AS price
FROM table as t WHERE t.[customer] = [customer])
Could be something like this if you're wanting to find the most expensive item that a customer has purchased? I'm unsure if the syntax is 100% correct but it should give you something to go from. I've added a cheat sheet below for Hive just incase.
Hive Cheat Sheet
Using row_number():
select Customer_ID, Product_ID
from
(select Customer_ID,
Product_ID,
row_number () over ( partition by Customer_ID order by Price desc) rn
from table
where customer_id=given_customer_id --add filter if necessary
)s
where rn=1;

How to get the minimum sales of a product without using GROUP BY and ROWNUM

I would like to get the minimum sales of each product without using any GROUP BY and ROWNUM function. Just wondering how can I accomplish that. Any inputs would be appreciated.
If I understand your question correctly, you want something like this:
SELECT DISTINCT ON (product_id) product_id, sales
FROM mytable
ORDER BY product_id, sales;
Try this,
SELECT value,productid FROM
table T1
WHERE value=(select min(value)
from table t2
where t1.productid=t2.productid)
Without knowing the table structure, this is impossible to answer correctly. But assuming your orders table has an order_id (PK), a customer_id and sales column, this should do it:
select o1.product_id, o1.sales
from orders o1
where o1.sales <= all (select o2.sales
from orders o2
where o2.product_id = o1.product_id
and o2.order_id <> o1.order_id);

How to get the products that all Warehouse exist?

I have a question as the title,I wanna get the products which appeared in every Warehouse,
I have no idea when i thought long time,i found i am so beetleheaded,
Thare are three tables in my sql server database:
Product
(
productID,
name,model,
size,
color
)
Warehouse
(
warehouseID,
name,
address
)
Inventory
(
warehouseID,
productID,
quantity
)
i hope someone help me to have a look and can write the sql that get the result
Thank you.
Use a GROUP BY to count the number of warehouses each product is in. Accept those rows for which this count is equal to the total number of warehouses.
SELECT productID
FROM Inventory
WHERE quantity > 0
GROUP BY productID
HAVING COUNT(*) = (SELECT COUNT(*) FROM Warehouse)
This assumes that (productID, warehouseID) is unique in the inventory table. If it is not then use COUNT(DISTINCT warehouseID) instead of COUNT(*).

Query a column and a calculation of columns at the same time PostgreSQL

I have two tables, Products and BundleProducts that have o2o relation with BaseProducts.
A BundleProduct is a collection of Products using a m2m relation to the Products table.
Products has a price column and the price of a BundleProduct is calculated as the sum of the prices of its Products.
BaseProducts have columns like name and description so I can query it to get both Products and BundleProducts.
Is it possible to query and sort by price both for the price column of the Products and calculated price of the BundleProducts?
Try something like this:
SELECT name, description, price
FROM (
SELECT name, description, price FROM products
UNION
SELECT bundle_products.name, bundle_products.description, sum(products.price)
FROM bundle_products
JOIN products on (<your join condition)
GROUP BY bundle_products.name, bundle_products.description
) AS combined
ORDER BY price

Find Number of Unique Values in Table

I have a table with four columns:
PartNumber, ValvePartNumber, ActuatorPartNumber, Price
I want to find the number of distinct prices for each combination of ValvePartNumber and ActuatorPartNumber.
This is using SQL Server 2005
You can combine COUNT(DISINTCT) and GROUP BY to accomplish this.
SELECT ValuePartNumber, ActuatorPartNumber, COUNT(DISTINCT Price) AS Prices
FROM [Table]
GROUP BY ValuePartNumber, ActuatorPartNumber