let me sketch the situation a bit:
i have 2 tables: producten and reviews,
in producten i have multiple products stored, and in reviews i have stored all the reviews now while attemption to get the average of the ratings in the reviews table mysql only returns 1 row while i expect 2 rows back since i have 2 products.
the query im trying to use is:
SELECT p.*, CAST(AVG(r.rating) AS DECIMAL(2,1)) as waardering FROM `producten` as p INNER JOIN `reviews` as r ON p.id=r.product_id
i have also tried:
SELECT p.*, CAST(AVG(r.rating) AS DECIMAL(2,1)) as waardering FROM `producten` as p INNER JOIN `reviews` as r ON r.product_id=p.id
but this diddnt returned more then 1 row either.
could anyone please tell me why the query is only returning 1 row and not all the rows it finds inside the producten table?
also please feel free to tell me how i could increase this question if needed.
You want to GROUP BY p.id at the end of your query so that you get the average per product, instead of the values in the "first" product's field with the average for all products.
It's because you are using AVG. It is going to group all the rows together as one row. You probably want to group by avg rating instead.
So if I had a table with two columns.. product name / rating... I would do like so:
select productname, avg(rating) from ratings group by productname
Related
I'm trying to join 2 tables and count the number of entries for unique variables in one of the columns. In this case I'm trying to join 2 tables - patients and trials (patients has a FK to trials) and count the number of patients that show up in each trial. This is the code i have so far:
SELECT patients.trial_id, trials.title
FROM trials
JOIN(SELECT patients, COUNT(id) AS Num_Enrolled
FROM patients
GROUP BY trials) AS Trial_Name;
The Outcome I'm trying to acheive is:
Trial_Name Num_Patients
Bushtucker 5
Tribulations 7
I'm completely new to sql and have been struggling with the syntax compared to scripting languages.
It's not 100% clear from your question of the names of your columns however you are after a basic aggregation. Adjust the names of the columns if necessary:
select t.title Trial_Name, Count(*) Num_Patients
from Trials t
join Patients p on p.Trial_Id = t.Id
group by t.title;
Based on Stu-'s answer, I want to say that your column naming is wrong.But you can write query based on logic like this.
SELECT trial.title AS Trial_Name, COUNT(p.id) AS Num_Patients
FROM trial
INNER JOIN patients AS p
ON trial.patient_fk_id = p.id
GROUP BY trial.title,p.id;
I need to calculate difference not sum, is there any way to do this. Here is desired example if sub aggreagate function exists.
SELECT p.*,SUB(p.orders) AS diff FROM products AS p GROUP BY p.id HAVING p.orders<0;
This is just example, not real table, just for example. The idea is to compare if first value is bigger than next values. Real example is if query returns two rows compare two values (orders count) of this tow rows in one query.
I would be grateful for any help.
What you want is only applicable when there are always 2 rows with the same p.id and different orders. If you have more than 2 rows which you would subtract from which?
So in the special case that you always have 2 rows per p.id then what you need is not a group function but join table products with itself like:
Select p1.id, (p1.orders - p2.orders) as diff
from products p1 inner join products p2 on p1.id = p2.id
where p1.orderid <> p2.orderid
This will only work if you always have 2 different orders per product.
I am trying to write a mysql query for an app I'm developing for android.
I have a database that has a bill_content table and a products table
I want to select top 10 most sold products.
This is a minimal version of what I have, but it's all I need to get an answer here.
bill_content table has the columns: id, id_product, quantity (id_product and quantity here can be duplicate because this table is larger, containing id_bill and other information)
products table has the columns: id, name
SELECT products.name AS Product,
bill_content.quantity AS Quantity
FROM bill_content, products
WHERE bill_content.id = products.id
ORDER BY bill_content.quantity DESC
LIMIT 10
Of course this returns a table of 2 rows containing all the products and their quantity in the bill_content table, but there are duplicates and I need to make sum of their quantity and display them as a single row.
Thank you in advance.
ANSWERED
This could be done using GROUP BY as Gordon Linoff said.
You want a group by. You should also learn to use proper explicit join syntax:
SELECT p.name AS Product,
SUM(bc.quantity) AS Quantity
FROM bill_content bc JOIN
products p
ON bc.id = p.id
GROUP BY p.name
ORDER BY SUM(bc.quantity) DESC
LIMIT 10;
I have a product table and a price table where a product can have several price rows. Is it possible to do a JOIN statement that returns one row per product with the price column from each price row where product is equal to the product ID?
Right now I have this:
SELECT PT.*, PR.*
FROM `products` AS PT
RIGHT JOIN `prices` PR
ON PR.`product` = PT.`ID`
And if there are 4 price rows for one product, I get 4 rows in response.
Look up the wizzbang GROUP_CONCAT function, which does what you want quite nicely (making a delimiter-separated string).
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
SELECT pt.`id`,
group_concat(pr.`price` separator ',')
FROM `products` AS PT
LEFT JOIN `prices` PR ON PR.`product` = PT.`ID`
GROUP BY pt.`id`
I have 3 tables:
Orders
- id
- customer_id
Details
- id
- order_id
- product_id
- ordered_qty
Parcels
- id
- detail_id
- batch_code
- picked_qty
Orders have multiple Details rows, a detail row per product.
A detail row has multiple parcels, as 10'000 ordered qty may come from 6 different batches, so goods from batches are packed and shipped separately. The picked quantity put in each parcel for a detail row should then be the same as the ordered_qty.
... hope that makes sense.
Im struggling to write a query to provide summary information of all of this.
I need to Group By customer_id to provide a row of data per customer.
That row should contain
Their total number of orders
Their total ordered_qty of goods across all orders
Their total picked_qty of goods across all orders
I can get the first one with:
SELECT customer_id, COUNT(*) as number_of_orders
FROM Orders
GROUP BY Orders.customer_id
But when I LEFT JOIN the other two tables and add the
SELECT ..... SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
.. then I get results that dont seem to add up for the quantities, and the COUNT(*) seems to include the additional lines from the JOIN which obviously then isn't giving me the number of Orders anymore.
Not sure what to try next.
===== EDIT =======
Here's the query I tried:
SELECT
customer_id,
COUNT(*) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
LEFT JOIN Parcels ON Parcels.detail_id=Detail.id
GROUP BY Orders.customer_id
try COUNT(distinct Orders.order_id) as number_of_orders,
as in
SELECT
customer_id,
COUNT(distinct Orders.order_id) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
(select SUM(Parcels.picked_qty)
FROM Parcels WHERE Parcels.detail_id=Detail.id ) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
GROUP BY Orders.customer_id
EDIT: added an other select with subselect
Is there any particular reason you feel the need to combine all these in one query? Simplify by breaking it up in to separate queries, and if you want a single call to get the results, put the queries in a stored procedure, using temp tables.