Formula expanded field and rounding cannot figure out - mysql

I have the following formula to put into mySQL
SELECT Order_Number,
Order_Date,
Product,
Quantity,
Price,
Commission,
Employee_Sold,
(Quantity*Price)*(Commission*.01) AS Commision_Paid
FROM Orders
WHERE Order_Date BETWEEN '2017-12-01' AND '2017-12-31'
The commission paid amount comes out with 2.992500 as one example.....
The commission field is setup as decimal(5,2).
In the expanded field (Commission_Paid) it would be nice for it to show up as 2.99 in this case.
I have tried the ROUND function in many places but I am not sure as I keep getting error messages.
Thanks in advance. Student here and deadline on my paper. Appreciated.
MySQL 5.5X running.

Use Round(x, 2)
SELECT Order_Number,
Order_Date,
Product,
Quantity,
Price,
Commission,
Employee_Sold,
round((Quantity*Price)*(Commission*.01), 2) AS Commision_Paid
FROM Orders
WHERE Order_Date BETWEEN '2017-12-01' AND '2017-12-31'

Related

MySql - Pulling two different sums from the same column

I am working on a game with a MySql database. In the game we have a system where characters have a debt that can be paid. We track each debt singularly and flag whether its paid or not with a Boolean 0,1.
I am trying to understand how in a single query I can sum the total paid amount as well as the total owed amount from a single character ID. The tables are
charid, amount, paid (the boolean)
Currently if I just want to find out how much they owe I simply place
SELECT sum(amount) FROM debts WHERE paid='0';
So how would I modify this to create a resulting column for both paid='0' and paid='1' ?
Thanks all.
Use conditional aggregation. Since the paid flag holds 0/1 values, you can just do:
select
sum(paid * amount) amount_paid,
sum( (1 - paid) * amount) amount_owed
from debts
you can use conditional aggregation using select case.
SELECT
sum(case when paid = 0 then amount else 0 end) as not_paid
,sum(case when paid > 0 then amount else 0 end) as paid
FROM debts
SELECT user, paid, SUM(amount) total FROM debts GROUP BY user, paid

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

Selecting customers who have spent more than a certain amount in a date range

I have a sales table which has one row for each sales tranaction. This table has date of sale and customer id as well.
I am looking for a way to select all those customers who have total spending in the specified range with in a specified date range. For example, get all customers who spent between 100 and 1000, between 2016-07-01 and 2016-08-15. This then has to become part of a larger query.
This query
select
customer_id,
sum(sale_amount)
from
sales_receipt
where
DATE(sales_receipt.sale_date) BETWEEN '2016-07-01' AND '2016-08-29'
group by
customer_id;
gives me all customers and their total spending in the specified date range but I need only those customers for whom sum(sale_amount) is between 100 and 1000.
Can any one help.
Try to use
select customer_id, sum(sale_amount) from sales_receipt where
DATE(sales_receipt.sale_date) BETWEEN '2016-07-01' AND '2016-08-29'
group by customer_id having sum(sale_amount)>=100 and sum(sale_amount)<=100
You'd use the HAVING clause here because you want to filter on the aggregated result:
SELECT
customer_id,
SUM(sale_amount) AS total_amount
FROM sales_receipt
WHERE DATE(sales_receipt.sale_date) BETWEEN '2016-07-01' AND '2016-08-29'
HAVING total_amount BETWEEN 100 AND 1000
GROUP BY customer_id;

need to get data from multiple mySQL tables grouping by date

I have 2 tables
1st: order
columns: id, date, price
2nd: paypal
columns: id, posted_date, amount
the columns date and posted_date contains the full date & time details; day/month/year hrs:minute:seconds
I need to get the data by grouping by the day from the both tables
order.date (day by day)
count all orders from order table for each day
sum of all price records from order table for each day
sum of all amount records from the another table paypal table for the same days
I can't imagine if I should use join, union, union all, or just merge by comma
SELECT DATE(O.`dater`) AS Dates,
COUNT(O.orders) AS Order_count,
SUM(O.price) as Total_Price,
(SELECT SUM(amount) FROM paypal WHERE DATE(O.`dater`)=`posted_date`) AS Total_Amount
FROM orders O
GROUP BY DATE(O.`dater`)
Note:(I have used column dater instead of column date)
Hope this helps.
This one working for me :)
SELECT DATE(O.`date`) AS Dates,
COUNT(O.order) AS Order_count,
SUM(O.price) as Total_Price,
(SELECT SUM(amount) FROM paypal WHERE DATE(O.`date`)=`posted_date`) AS Total_Amount
FROM order O
GROUP BY DATE(O.`date`)

mySQL query issue -- homework

I need some help with one of the questions from my homework, Ive been trying for about an hour now and cant get it to run.
List the customers that bought more items than the average number of items per customer
The tables are as follows:
Customer(Cnum, CustomerName, Address)
Item(Inum, ItemName, Manufacturer, Year)
Bought(Cnum, Inum, Date, Quantity)
Prefer(Inum, Cnum)
The best i could figure out was that it needs to be the total Quantity per Customer compared to the overall average of the Quantity. I've tried various forms of this query:
SELECT Cnum
FROM Bought
WHERE
(
SELECT Cnum, SUM(Quantity)
FROM Bought
GROUP BY Cnum;
) >
(
SELECT AVG(Quantity)
FROM Bought
);
But it returns an error -- (phpMyAdmin isnt telling me what the problem is, just failing to execute and going to no connection page, which means error in my query)
I have also tried to return the higher SUM with:
SELECT SUM(Quantity)
FROM Bought
WHERE SUM(Quantity) > AVG(Quantity);
And same issue.
Any help would be appreciated, even an explanation as to why the second one fails.
You might want to take a look at the HAVING clause of SQL.
Note: I'm intentionally not giving you the full answer since this is homework.
why Don't you try this.
SELECT `Cnum` , Sum( Quantity )
FROM `bought`
GROUP BY `Cnum`
HAVING Sum( Quantity ) > ( SELECT AVG( Quantity ) FROM Bought )
Try below:
SELECT Cnum
FROM Bought
having SUM(Quantity) > (SELECT avg(Quantity) FROM Bought)
order by SUM(Quantity) desc
Try this maybe it helps
SELECT Cnum, SUM(Quantity)
FROM Bought
GROUP BY Cnum
HAVING SUM(OrderQuantity) > avg(Quantity)
Try this
SELECT Cnum, Inum, SUM(Quantity) sum, AVG(QUANTITY) average
from bought group by cnum,inum having sum > average