I have written the following query which is design the multiply the total quantity of products, against quantity price and put this into a new column. What I am wondering and a bit stuck with. Is there then a way to also total this new column. So I as well as getting a total for each row ordered, I can also get a total for the order itself.
SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
This add a record with a total.
Simulates GROUP BY with ROLLUP
SELECT
Product_Quantity
, ProductPrice
, (Product_Quantity*ProductPrice) AS Total
FROM
Order_Info
WHERE
Order_Number = '1'
UNION ALL
SELECT
NULL
, NULL
, SUM((Product_Quantity*ProductPrice)) AS Total
FROM
Order_Info
WHERE
Order_Number = '1'
To get a summary row, you can use sql's union and the sum aggregating function. Try this:
SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
union all
SELECT sum(Product_Quantity), sum(ProductPrice), sum(Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
Related
I want to count data with unique ID buat same as pn.This is my sample data:
id_form id_purchase pn
PUR3-20190515022552 PUR-20190515022552 02N64073
PUR2-20190515022552 PUR-20190515022552 02N64073
PUR1-20190515022552 PUR-20190515022552 02N64073
This is my code :
SELECT COUNT(*) as Total FROM pur_supp WHERE pn = '02N64073' GROUP BY id_purchase
When I run the code, that total still 3 but I want that total is 1.
Try this
SELECT COUNT(DISTINCT id_purchase) as Total FROM pur_supp WHERE pn = '02N64073'
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.
I've to get all the orders from one customer, then get the sum of the orders and then get the max order.
I can't use order by and limit.
I got the result but i can't get the make sum and max work properly
here is my current query:
SELECT SUM(Qty * UnitPrice) AS Total FROM `Details`
WHERE ONo IN (
SELECT Orders.Ono
FROM Orders, Customers
WHERE Customers.FName = 'Charles' AND Customers.LName = 'Xavier' AND Customers.CNo = Orders.CNo
GROUP BY Orders.ONo
)
GROUP BY ONo
Total
7.50
20.99
54.47
49.98
8.00
Please try:
SELECT MAX(Total) as MaxTotal FROM (<your query comes here>) AS T
I have a table of items some are second hand and some are new,
I would like a query giving me the minimum price for the new version of the item as well as the second hand version.
I have a boolean field called "new" who tell if the item is second hand or not
for the result I have to do two query but I think its possible to do only one
query for the second hand :
SELECT ref_product, MIN( price )
FROM item_list
WHERE new = 0
GROUP BY ref_produit
query for the new :
SELECT ref_product, MIN( price )
FROM item_list
WHERE new = 1
GROUP BY ref_produit
I would like that kind of result :
ref_product | min_price_for _secondhand | min_price_for_new
SELECT ref_product,
MIN(case when new = 1 then price else null end) as new_price,
MIN(case when new = 0 then price else null end) as used_price
FROM item_list
GROUP BY ref_product
Try something like:
SELECT ref_product, MIN( price )
FROM item_list
WHERE new in (0,1)
GROUP BY new, ref_product
Drop the condition on new (get all products) and group by ref_produit' then bynew`:
SELECT ref_product, new, MIN(price) AS min_price
FROM item_list
GROUP BY ref_produit, new
I have a table in MySql related to purchase of Inventory.
The table format is as below :
itemname (varchar)
vendor (varchar)
pdate (datetime)
qty (decimal)
rate (decimal)
total (decimal)
id (mediumint) (autoincrement)
A report is to be generated that must have the following columns
Item HighestRate HighestRateDate LowestRate LowestRateDate % difference
--
Item is the itemname
HighestRate and HighestRateDate are rates at maximum
LowestRate and Date are rates at minimum
%difference is a basic difference percentage between highestrate and lowestrate of a row
I have prepared the following query
SELECT itemname,rate,pdate from purchases
group by itemname
having rate = max(rate)
order by itemname
which does generate one half of the report.
However since it requires both the lowest and highest rate. This report is incomplete and printing two reports makes comparison difficult.
Any help would be appreciated.
Thankyou
Here goes. It's not pretty but it works.
select min.itemname,
min.rate,
min.pdate,
max.rate,
max.pdate,
(max.rate - min.rate)/max.rate as diff
from (SELECT p.itemname,
p.rate,
pdate
from purchases p,
(select itemname,
max(rate) as rate
from purchases
group by itemname) max_values
where p.itemname = max_values.itemname
and p.rate = max_values.rate
) max,
(SELECT p.itemname,
p.rate,
pdate
from purchases p,
(select itemname,
min(rate) as rate
from purchases
group by itemname) min_values
where p.itemname = min_values.itemname
and p.rate = min_values.rate
) min
where max.itemname = min.itemname;
You'll need an index on (itemname, rate) for this query not to be awfully slow.
If there are two or more dates with same maximum (or minimum) rate, then a (more or less) random date will be selected. If you want to control that, change the ORDER BY rate ASC (to ORDER BY rate ASC, pdate DESC for example):
SELECT
di.itemname AS Item
, maxr.rate AS HighestRate
, maxr.pdate AS HighestRateDate
, minr.rate AS LowestRate
, minr.pdate AS LowestRateDate
, (maxr.rate - minr.rate)/maxr.rate --- calculations
AS PrecentDifference
FROM
( SELECT DISTINCT itemname
FROM purchases
) AS di
JOIN
purchases AS minr
ON minr.id = --- I guess this is the PK
( SELECT id
FROM purchases p
WHERE p.itemname = di.itemname
ORDER BY rate ASC
LIMIT 1
)
JOIN
purchases AS maxr
ON maxr.id =
( SELECT id
FROM purchases p
WHERE p.itemname = di.itemname
ORDER BY rate DESC
LIMIT 1
)