Hard to explain, new at mysql - mysql

Calculate each supplier's total sales quantity and get the sales person's name if the sales person supplies parts more than 1000 units in total.
Table info:
Supplier {s_num, s_name, status, city}
Spj {s_num, p_num, j_num, qty}
This is what I have:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
AND qty > 1000
GROUP BY s_name;
I think my error is in this line:
AND qty > 1000
maybe I am using the "GROUP BY" incorrectly...
No suppliers quantity is above 800, so I get a blank result.
I want to test this:
"sum of quantity for each supplier" > 1000

Use HAVING to access aggregate results after a group by clause:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
GROUP BY s_name
HAVING qty > 1000;
See this post for more info on having vs where.

Related

MySql query ignore to show if Sum of rows Quantity condition is true

I am stuck on MySql Query execution! I created one table called 'WO'. From this table, there are 3 columns
Order QTY (Actual Quantity for the order)
Receive Qty (Actual Quantity that has been received)
Pending Qty. (Quantity that will be received)
I can add some of my row results. But my target is, if Order QTY=Receive QTY, then this row will not be shown during execution. But I failed to capture this part! Need your valuable advice to solve this part.
Below is my code and output in a picture.
SELECT
id,
ind,
WONO,
PRODUCT,
SUM(QTY) as "Order_QTY",
SUM(Rec_QTY) as "Received",
SUM(QTY-Rec_QTY) as "Pending",
UNIT
FROM
wo
WHERE
Inhouse_Status!="IN-HOUSED"
Group by
ind
ORDER BY
ind DESC
You just have to add condition in you WHERE clause
SELECT
id,
ind,
WONO,
PRODUCT,
SUM(QTY) as "Order_QTY",
SUM(Rec_QTY) as "Received",
SUM(QTY-Rec_QTY) as "Pending",
UNIT
FROM
wo
WHERE
Inhouse_Status!="IN-HOUSED" AND Order_QTY != Received
Group by
ind
ORDER BY
ind DESC
You may filter rows using a where condition eg WHERE Order QTY <> Receive QTY or since you are using a group by to sum total Order QTY you may filter the total using a having clause eg WHERE SUM(QTY) <> SUM(Rec_QTY).
Your query could look like:
SELECT
ind,
WONO,
PRODUCT,
SUM(QTY) as "Order_QTY",
SUM(Rec_QTY) as "Received",
SUM(QTY-Rec_QTY) as "Pending",
UNIT
FROM
wo
WHERE
Inhouse_Status!="IN-HOUSED"
Group by
ind, WONO,PRODUCT, UNIT
HAVING
SUM(QTY) <> SUM(Rec_QTY)
ORDER BY
ind DESC

MySQL - Average number of particular product sold on date

I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID

SELECT the customer who has ordered the greatest quantity of Products in the case of two customers

I have the following ORDERS table
I know query to select the customer that has ordered the greatest quantity. However, how would it work, if say, two customers have the same quantity. What query should I write to show both the customers?
You can use a subquery which checks that the quantity for a given record matches the largest quantity observed in the table:
SELECT *
FROM yourTable
WHERE qty = (SELECT MAX(qty) FROM yourTable)
This will return multiple records if there are more than one customer sharing the maximum quantity.
If you only wanted to get back a single record, even in the presence of ties, you could use this approach:
SELECT *
FROM yourTable
ORDER BY qty DESC
LIMIT 1
I think you want sum of qty per custNum.
If so you can try like:
select custNum,
sum(qty) as qty
from Orders
group by custNum
order by sum(qty) desc;
Fiddle here:
http://sqlfiddle.com/#!9/47931b/10
SELECT custnum,sum(qty) as total
FROM orders
group by custnum
having sum(qty) = (SELECT MAX(qty) FROM orders);
This will return both values.

MS SQLServer How to multiply two columns, then add them all together..?

I'm new here. First post. I'd really appreciate some help.
I'm trying to calculate total sales for all products sold.
I have a Quantity column and a Price column.
I understand how to multiply these two columns, BUT I do not know how to add them all together in the same query.
Here is an example:
Quantity: 2, 3, 1 Price: 2, 4, 5
I could do Quantity * Price to get: 4, 12, 5
But then how would I add 4 + 12 + 5 to get the total? I need this step to be included in the same query.
EDIT: Both the Quantity and Price columns are in the same table.
SALES (Quantity, Price)
I am using Microsoft SQL-Server.
Example if you have one table:
SELECT dbo.orderid,
SUM(dbo.quantity * dbo.price) AS grand_total,
FROM ORDERITEM
If you have two tables instead of one, then:
SELECT oi.orderid,
SUM(oi.quantity * p.price) AS grand_total,
FROM ORDERITEM oi
JOIN PRODUCT p ON p.id = oi.productid
WHERE oi.orderid = #OrderId
GROUP BY oi.orderid
Adding all the rows (orderid numbers) up together to get a total, you would simply groupby and them select the sum value. Example below:
SELECT Orderid, SUM(quanity) AS Expr1, SUM(price) AS Expr2, SUM(quanity * price) AS Total
FROM dbo.mytable
GROUP BY pid
HAVING (pid = 2)
Or this in a SQL view showing the total QTY and Price:
SELECT Orderid, SUM(quanity) AS Quanity, SUM(price) AS Price, SUM(quanity * price) AS Total
FROM dbo.mytable
GROUP BY pid

Mysql SUM of SUM

Is it possible to make a sum of sum in one select query?
Something like this:
SELECT id, SUM(current_price - bought_price)*amount AS profit FROM purchase WHERE purchase_id = 1 GROUP BY id
I want the sum of all returned profit
This is what you are asking:
SELECT tmp.id, SUM(tmp.profit)
FROM (
SELECT id, SUM(current_price - bought_price)*amount AS profit
FROM purchase
WHERE purchase_id = 1
GROUP BY id
) AS tmp
but the result is the same as
SELECT SUM(current_price - bought_price)*amount AS profit
FROM purchase
WHERE purchase_id = 1
This is your query:
SELECT id, SUM(current_price - bought_price)*amount AS profit
FROM purchase
WHERE purchase_id = 1
GROUP BY id ;
If you want the total profit, just remove the group by and rephrase the sum():
SELECT SUM(current_price*amount - bought_price*amount) AS profit
FROM purchase
WHERE purchase_id = 1;
EDIT:
You can also write this as:
SELECT SUM((current_price - bought_price)*amount) AS profit
FROM purchase
WHERE purchase_id = 1;
By the way, the original formulation isn't correct. You want to use the same form with the group by:
SELECT id, SUM((current_price - bought_price)*amount) AS profit
FROM purchase
WHERE purchase_id = 1
GROUP BY id;
The problem with your version is that amount is taken from on arbitrary row for each id. If there is only one row in purchase for each id, then no problem. But if there are multiple rows for an id, then the query in the question will produce indeterminate results. (You are using a MySQL extension to group by that is documented here.)
SELECT id, SUM(current_price - bought_price)*amount
AS profit FROM purchase WHERE purchase_id = 1 GROUP BY id WITH ROLLUP;
You can use WITH ROLLUP modifier for GROUP BY (http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html). As result you will get all summary by product and TOTAL for all product groups.