mysql adding total price of certain products - mysql

hi i'm having problems querying a products table at the moment:
i need to display the total cost of all HP and toshiba products..
this is what i have tried so far
SELECT * FROM products
WHERE prod_id LIKE '__hp%'
AND SELECT SUM(price) AS total FROM products;
any help would be appreciated
this is a pic of the products table -->>
Thank you;

You could help yourself a lot if it is clear that the 3rd and 4th characters of Prod_ID are a manufacturer code. HP and TA for Toshiba.
SELECT SUBSTRING(prod_id,3,2)
,SUM(price * on_hand)
WHERE SUBSTRING(prod_id,2,1) IN ('TA','HP')
GROUP BY SUBSTRING(prod_id,3,2)

Seems like you're looking for something like this:
SELECT SUM(price) AS total_price
FROM products
WHERE prod_name LIKE 'HP%' or prod_name LIKE 'Toshiba%';

Related

Multiple the amount of sold products with the price of the product

My tables look like this:
tblartikel(artikelid primary key, price)
tblbestallning(artikelid, amount)
I saw someone do this:
select artikelid, sum(amount) as AmountSold from tblbestallning group by artikelID
this correctly showed the artikelid's and thee amount it has ever been bought
how do i multiply the times it has been bought with the price?
This depends on the relationship between the two tables but assuming there is only one price per artikel, this would be a good starting point
SELECT tblbestallning.artikelid,
SUM(amount) as AmountSold,
SUM(amount*price) AS 'TotalSold'
FROM tblbestallning
JOIN tblartikel ON tblartikel.artikelid = tblbestallning.artikelid
GROUP by tblbestallning.artikelid

Possibly advanced SQL query UNION of 3 Tables

Goal. To make a query that displays Product Name(Products), Product Type(ProductTypes), and total number of sales of each product(Sales)
Here are my tables:
I am having real difficulty figuring out how I am meant to do this. I am trying to do a UNION and a few other things but cannot get it to work.
I can get the total number of sales by using this SELECT ProductID, count(*) as NumSales from Sales group by ProductID but really struggling to do the rest and format it correctly. Any help would be appreciated.
EDIT:
Select Products.ProductName, ProductTypes.ProductType
From Products
INNER JOIN ProductTypes ON Products.ProductTypeID=ProductTypes.ProductTypeID
I have this to display this right now, just need to join the sales count somehow.
try:
select prod.ProductName, ptyp.ProductType, count(SaleID) count_sale
from Products prod
join ProductTypes ptyp on ( ptyp.ProductTypeID= prod.ProductTypeID)
join Sales sal on ( sal.ProductID = prod.ProductID)
group by prod.ProductName, ptyp.ProductType

SUM and GROUP BY Using a Subquery

I been working on a question given to me to find the total room price of a hotel chain using a subquery and grouping them by Hotel. I currently have the following which seems to not work at all and get an error.
SELECT MAX(roomPrice) AS 'Total Room Price'
FROM room
WHERE IN
(SELECT roomPrice, SUM(roomPrice) AS 'TotalRoomCost' FROM room GROUP BY hotel);
Any help would be appreciated
Screenshot of the Tables
Simple fix is to select from the sub query.
I think from your description you want to sum all the room prices for each hotel, and the get the max total room price from all the individual hotels. If so something like this:-
SELECT MAX(TotalRoomCost ) AS 'Total Room Price'
FROM
(
SELECT hotel, SUM(roomPrice) AS TotalRoomCost FROM room GROUP BY hotel
) sub0;
Please change column names as these are not shown in attached screenshot.
SELECT
H.Name AS HotelName,
SUM(R.roomPrice) AS 'Total Room Price'
FROM room AS R
INNER JOIN hotel AS H
ON R.HotelID=H.HotelID
GROUP BY H.Name

sum of products in stock

i need to show the total valvue of all the all-in-one printers in stock from the products table
the all-in-one printers can be identified by having 'A1' in positions one and two of the product id
here is what ive tried so far
select sum(price) AS total_price
from products
where prod_id like 'A1%'
here is a picture of the products table
so if i understand you correctly you want the price per item times the number on hand all summed up if thats the case this should do the trick
SELECT SUM(adjusted_price)
FROM
( SELECT price * on_hand adjusted_price
FROM products
WHERE prod_id like 'A1%'
)t
DEMO

How to compare fields from different tables

I want to select rows from a table (products), but that table is linked to another one (stock) using JOIN statement.
In products table there are 2 important fields, SKU and minimum_stock.
In stock table there are SKU (relation with products) and qty
Now, I want to select rows from products only if in table stock exists SKU, and qty is > minimum_stock from products.
Like this:
SELECT *
FROM products
INNER JOIN stock ON products.SKU = stock.SKU
WHERE products.minimum_stock.value > stock.qty.value;
Obviously this code not works.
Can somebody help me?
Thanks in advance.
If your tables look like:
products:
SKU
minimum_stock
(... other columns ...)
stock
SKU
qty
(... other columns ...)
Your query doesn't have to have the .value at the end of the columns. In fact, that's what would make this not work.
Do something like:
SELECT *
FROM products
INNER JOIN stock ON products.SKU = stock.SKU
WHERE products.minimum_stock > stock.qty;
SQL Fiddle Demo