How to calculating SUM of (quantity*price) from two different tables? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this two following tables:
Order Table
Product Table
I'm trying to calculate the subtotal price for each product (quantity*price) then SUM the TOTAL value for the entire order.
Thanks, Any help would be very appreciated.

Check if table names are correct and try this:
SELECT o.order_number, o.item_quantity, p.price,(o.item_quantity * p.price) AS subtotal, SUM( o.item_quantity * p.price) AS total
FROM order AS o
LEFT JOIN product AS p ON product_number = idproduct_detail;
With this I am connecting each order with the relative product then selecting item_quantity and price and SUM the product of them.

Related

sql - mysql - tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I need to collect the name and total order of customers who had an order of more than 56000. there are three tables, customers, orders, and orderdetails.
This is the output that I need:
this is the code that im trying:
select a.customername, sum(c.priceeach*c.quantityordered) as "ORDERTOTAL"
from customers a
join orders b on b.customernumber = a.customernumber
join orderdetails c on c.ordernumber = b.ordernumber
having sum(c.priceeach*c.quantityordered) > 58000
group by a.customername;
but it is giving me 76 rows.
when I only try with the table order details i can get exact 7 rows but the table orderdetails doesnt contain customername, I can't undrestand where am I doing it wrong.

Two different columns need criteria with SUM condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table with two columns in a mysql database.
For example: Item_name, qty in table1
I need to retrieve all items by Item_name where sum(qty) <> 0
I tried the following but it didn't work
select item_name from table1 where SUM(QTY) <> 0 group by item_name
Thank you.
When you want to add a condition on an aggregate, HAVING is used rather than WHERE:
SELECT item_name
FROM table1
GROUP BY item_name
HAVING SUM(QTY) <> 0

Updating a column with a Count of data from another column in the same table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
As the title suggests, trying to take the count or possibly distinct count of a column, we call it year, to count the number of a years for an individual or populated ID and place it another column in the same table. Here is an idea of what I have so far and we want to update the table. Thank you.
Join group table to updated table directly:
UPDATE outputtable O JOIN
(SELECT personID, COUNT(DISTINCT year) AS countYear
FROM outputtable GROUP BY personID) temp ON O.personID=temp.personID
SET O.N=temp.countYear

Joining on BETWEEN values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How do I do lookup or joining two tables based on the values of date?
In the first table I have Item_ID and Entry_Date as columns.
On the second one I have Shift_ID, Personnel, Begin_Date, and End_Date as columns.
I want to create a result that displays Item_ID and Personnel, where Personnel and Shift is determined by whether or not an Item's Entry_Date is between a shift's Begin_Date and End_Date.
I'm sorry for utilizing image, I meant to write the table within this post itself but I don't know how yet.
Try a basic join:
SELECT
i.Item_ID,
COALESCE(s.Personnel, 'NA') AS Personnel
FROM Item i
LEFT JOIN Shift s
ON i.Entry_Date BETWEEN s.Begin_Date AND s.End_Date;

Can i use alias in where clause? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
SELECT sum(st.stock_in) - sum(st.stock_out) as'stock_qty',
p.product_name,p.product_limit
FROM stock as st
join products as p
on p.product_id=st.product_id
where 'stock_qty' < 'p.product_limit'
GROUP BY st.product_id
I Have two Tables Products and Stock.
Products Table: Product_id, Product_name and Product_limit
Stock: stock_id, Product_id, stock_in, stock_out
From above query i get all the records, but I want to get only those records who product limit is reached or below.
if product 1 has 50 product_limit and stock reached the 50 limit or below the 50 so get me those records.
Suggest me best solution.
You cannot:
Use an alias in a where clause.
Use aggregation functions in the where clause.
Although comparing two strings is pretty much not ever recommended, it is allowed.
But, you want a having clause anyway, so the question in your title is irrelevant. Try this:
select sum(st.stock_in) - sum(st.stock_out) as stock_qty,
p.product_name, p.product_limit
from stock st join
products p
on p.product_id = st.product_id
group by p.product_name, p.product_limit
having stock_qty < p.product_limit ;
Note that I changed the group by to match the columns being selected in the select. That is also a good practice.