I have a table which consists:
id country Date item_name price
ae3u2 USA 27/12/2018 budget 1.99
bf5d8 India 31/12/2018 everything 34.99
dc8a4 USA 22/01/2019 cars 25.99
and it goes on.
I have to calculate:
total revenue
total revenue made from each item broken down by country and day.
I am confused how to calculate it as i don't have "quantity" and i am also not much experienced.
You can use with rollup, but you will get additional rows:
select item_name, country, date, sum(price)
from t
group by item_name, country, date with rollup
You can filter out the intermediate rows as:
having grouping(item_name, country, date) in (0, 7)
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
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'
I feel like this is a stupid question, but I haven't been able to find a solution by searching. I have a dataset with sales data, where I want to group some of the ID groups together and show the sum of sales. I'm not concerned with individual items, I want to see only the groups of items and the sum of their sales.
A simplified version of what I'm doing now is:
SELECT year, quarter, company, segment, sum(sales) AS total_sales
FROM table
WHERE segment = 001 OR segment = 015
AND producer LIKE '%ACME Inc.%'
AND year = 2015 OR year = 2016
GROUP BY quarter, year, producer, segment
ORDER BY year, quarter, producer, segment ASC
;
My main problem is getting segments 001 and 015 into the same group, for instance 'Jelly' so that the output shows
year|quarter|company|segment|total_sales
2015|1|ACME Inc.|Jelly|100
where 'Jelly' includes both segments 001 and 015.
I'm able to list alle the individual items, but not group them up with in aggregated groups. My problem is being able to filter only the companies I want to look at, and only the segments I want to look at, at the same time as I'm creating and listing custom groups of the segments.
Thanks for any help :)
Edit: MCVE here - http://sqlfiddle.com/#!9/2414bd/3
CREATE TABLE Table1
(`company` text, `sales` int, `year` int, `quarter` int,
`segment` text)
;
INSERT INTO Table1
(`year`, `quarter`, `company`, `segment`, `sales`)
VALUES
(2015,1,'ACME',001,100),
(2015,1,'ACME',015,100),
(2015,1,'HAL',001,25),
(2015,1,'HAL',015,25),
(2015,1,'ACME',002,50),
(2015,1,'HAL',003,50)
/*many other companies and segments as well*/
;
First, I think you need to fix your where clause. Numbers that start with 0 are usually strings. Plus, you should be using in and careful about the and/or logic:
SELECT year, quarter, company, segment, sum(sales) AS total_sales
FROM table
WHERE segment IN ('001', '015') AND
producer LIKE '%ACME Inc.%' AND
year IN (2015, 2016)
GROUP BY quarter, year, producer, segment
ORDER BY year, quarter, producer, segment ASC;
Next, if you want to combine segments, use case. You seem to want:
SELECT year, quarter, company, 'Jelly' as segement,
SUM(sales) AS total_sales
FROM table
WHERE segment IN ('001', '015') AND
producer LIKE '%ACME Inc.%' AND
year IN (2015, 2016)
GROUP BY quarter, year, producer
ORDER BY year, quarter, producer ASC;
SELECT year, quarter, company,
CASE
WHEN segment IN (001,015)
THEN 'Jelly'
WHEN segment IN (002,003)
THEN 'Bread' ELSE segment END segment,
SUM(sales) AS total_sales
FROM Table1
WHERE company LIKE '%ACME%' OR company LIKE '%HAL%'
AND year IN (2015,2016)
GROUP
BY year
, quarter
, company
, CASE WHEN segment IN (001,015) THEN 'Jelly'
WHEN segment IN (002,003) THEN 'Bread'
ELSE segment
END
ORDER
BY year, quarter, company, segment ASC
;
I have been practicing SQL online and I am having doubt in one exercise. In the following database:
product (prod id, product name, product price, product manufacturer)
customer (cust id, customer name, customer address)
rating (prod id, cust id, rating date, rating stars)
sale (sale id, prod id, cust id, sale quantity, sale cost)
"For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given."
I am not entirely sure about the answer, this is what I got:
SELECT customer_name,
product_name,
min(rating_stars)
FROM customer,
product,
rating
WHERE rating_stars < SOME
(SELECT rating_stars
FROM customer,
product,
rating)
GROUP BY customer,
product,
rating
Could any of you have a check in the code and see if it is correct?