Sql statement groupby with limiting condition - mysql

What am trying to do is to return all the records where the total count of truck owners is more than the specified limit
that is in the case below suppose the total limit is 3 i would like to fetch all the owners who have 3 or more trucks registered
so the query if the limit is 3 should only return metro trans as they have 3 or more trucks
This is my db structure
tbl_trucks
id owner_id .....
1 12
2 12
3 13
4 12
tbl_truck_owners
id owner
12 metro trans
13 mush elec
How do i add the limit to my sql statement
THis is what i have tried
SELECT
COUNT(tbl_trucks.owner_id), tbl_truck_owners.owner
FROM tbl_trucks
LEFT JOIN tbl_truck_owners ON tbl_trucks.owner_id = tbl_truck_owners.id
WHERE //stuck at adding the limit
GROUP BY owner_id"
How do i add the limit

Use HAVING statement:
SELECT COUNT(tbl_trucks.owner_id), tbl_truck_owners.owner
FROM tbl_trucks
LEFT JOIN tbl_truck_owners ON tbl_trucks.owner_id = tbl_truck_owners.id
GROUP BY owner_id
HAVING COUNT(tbl_trucks.owner_id) >= 3

Related

Use COALESCE within SUM in MySQL SELECT statement

I have the following tables:
Table users
id name base_discount
1 jack 10
2 michael 20
3 richard 30
Table item
id name category_id price
1 hammer 1 10
2 knife 2 15
3 spoon 2 12
4 plate 3 20
5 tree 4 400
Table category
id name
1 tools
2 kitchen
3 dishes
4 garden
Table discount_category
id user_id category_id discount
1 1 1 20
2 1 3 25
3 3 3 10
4 1 2 15
Table discount_item
id user_id item_id discount
1 2 1 50
2 1 2 50
Now what I want to achieve. I want to attach the discount per item that a user has to the correct item. If that is not available (NULL) I want to attach the discount per category that a user has. And if that is not available (NULL), I want to attach the base discount that a user has. With the discount I then calculate the new price of the item. However, when I try using COALESCE() within SUM() I get a syntax error. What am I doing wrong?
Below is my current query:
SELECT item.id, item.name, category.id,
category.name AS category_name, item.price, SUM((100 -
COALESCE(
(
SELECT discount_item.discount
FROM discount_item
INNER JOIN users ON discount_item.user_id = users.id
WHERE users.id = '1' AND discount_item.item_id = item.id
),
(
SELECT discount_category.discount
FROM discount_category
INNER JOIN users ON discount_category.user_id = users.id
WHERE users.id = '1' AND discount_category.category_id = item.category_id
),
(
SELECT users.base_discount
FROM users
WHERE users.id = '1'
)
)) / 100 * item.price) AS new_price,
FROM item
INNER JOIN category ON item.category_id = category.id;
Please also see the below link for an SQL Fiddle (couldn't do it on sqlfiddle.com as it wouldn't load). In the example I have appended a suffix to each table name.
http://rextester.com/LCCKSD59098
You have an extra comma after new_price and before FROM ITEM, hence the error.
Rextester Demo
Do not select other columns in select if you are not using them in group by, as in other relational RDBMS, you will get error.
Also use alias for table names for better readibily and to avoid confusion.

Max & AVG MYSQL INNER JOIN for 2 tables

I have 2 tables (example):
users:
ID company_ID
1 7
2 6
3 7
activity_rewards:
user_ID points activity_type_ID
1 1 7
1 2 7
1 1 7
1 1 8
2 1 7
2 1 7
2 2 8
2 1 7
3 2 7
3 1 7
3 2 8
3 1 8
(There are also tables for company and activity_types, but they shouldn't be relevant here)
I need a MYSQL query that will sum the total points for each user WHERE all users have a certain company_ID and for a certain activity_type_ID AND it will return the MAX and the AVG of the sum of all the users points
I have for example:
SELECT SUM(activity_rewards.points) AS totalpoints,
MAX(activity_rewards.points) AS maxpoints,
AVG(activity_rewards.points) AS avgpoints
FROM activity_rewards
INNER JOIN users
ON activity_rewards.user_ID = users.ID
WHERE ( (users.company_ID = "7") && (activity_rewards.activity_type_ID LIKE '8') )
In the example query only 3 results are involved. They are:
user_ID points activity_type_ID
1 1 8
3 2 8
3 1 8
I want to get:
user 1 has 1 point
user 3 has 3 points
Max is 3 average is 2
instead I'm getting Max is 2 and average is 1.33
The results are in line with your query, but your query is not in line with your requirements.
Your query calculates the max and the average of points across the relevant records. But you seem to want the max and the average of the points summed by user id.
This means that you need to calculate the sum of points per user in a subquery and then calculate the max and average in the outer query.
SELECT SUM(sumpoints) as totalpoints, max(sumpoints) as maxpoints, avg(sumpoints) as avgpoints
FROM
(SELECT users.ID, SUM(activity_rewards.points) AS sumpoints
FROM activity_rewards
INNER JOIN users ON activity_rewards.user_ID = users.ID
WHERE users.company_ID = 7 and activity_rewards.activity_type_ID = 8
GROUP BY users.ID) t
Do it like this:
SELECT MAX(sum_points) max, AVG(sum_points) avg, SUM(sum_points) sum_all FROM (SELECT SUM(t2.points) sum_points FROM users t1 JOIN activity_rewards t2 ON (t1.ID = t2.user_ID) WHERE ( (t1.company_ID = "7") AND (t2.activity_type_ID = '8') ) GROUP by t1.ID) as summation

SQL limit SELECT but not JOIN

I'm implementing pagination on my BD. My problem is when I want limit the SELECT statement but not the JOIN. Example, a product can got many prices:
SELECT * FROM product
LEFT JOIN price ON product.id == price.id_product
LIMIT 20
But I want to get 20 products with each one with their prices. How I can limit the statement SELECT, but not LEFT JOIN.
Example:
product price.id price.id_pruct price.price
1 1 1 50
2 2 1 30
3 3 1 40
4 1 20
5 2 30
SELECT * FROM product
LEFT JOIN price ON product.id == price.id_product
LIMIT 3
Return:
product price.id id_prodcut price
1 1 1 50
1 2 1 30
1 3 1 40
But I Want
product price.id id_prodcut price
1 1 1 50
1 2 1 30
1 3 1 40
1 4 1 20
2 5 2 30
3 . . .
Three products (limit 3)
Thanks. I hope you can help me.
Modify your query to limit the number of product rows before joining it to the price table. This means we want to to join the results of a query to a table, or in other words, we write a query containing a subquery:
SELECT *
FROM (
SELECT *
FROM product
ORDER BY id_product
LIMIT 3
) p
LEFT JOIN price ON p.id = price.id_product
Hope that helps.
I would write a subquery to get the three first products (or whatever condition you choose) like this:
SELECT id
FROM product
ORDER BY id
LIMIT 3;
Once I have that, I can select everything from the price table as long as the id is in that subquery. You can do this using a join:
SELECT p.*
FROM price p
JOIN(
SELECT id
FROM product
ORDER BY id
LIMIT 3) tmp ON tmp.id = p.product_id;
Here is an SQL Fiddle example using your sample data, and I also added a row that won't be returned so you can see that it works.

Find MIN value based on two columns in MySQL

I've table in MySQL with athletic performs.
Structure:
id (PRIMARY KEY)
athlete_id (FOREIGN KEY)
perform
category_id
discipline_id
If I want to select the best performs from this table (each athlete can be in results max once), I use this query:
SELECT
*
FROM
performs NATURAL
JOIN athletes
JOIN
(SELECT
athlete_id,
MIN(perform) AS perform,
category_id,
discipline_id
FROM
zaznamy
WHERE discipline_id = 4
AND category_id = 3
GROUP BY athlete_id) rec
ON performs.athlete_id = rec.athlete_id
AND performs.perform = rec.perform
AND performs.category_id = rec.category_id
AND performs.discipline_id = rec.discipline_id
ORDER BY performs.perform
LIMIT 25
I get right results. But I want to select the best performs from one discipline and more category together (e.g. Mens with Juniors etc). How do I use clause GROUP BY for two columns?
Maybe I don't understand something, but you can add two columns in group by, like the following example. Just add ,(comma) between the two or more columns.
SELECT
*
FROM
performs NATURAL
JOIN athletes
JOIN
(SELECT
athlete_id,
MIN(perform) AS perform,
category_id,
discipline_id
FROM
zaznamy
WHERE discipline_id = 4
GROUP BY athlete_id,category_id) rec
ON performs.athlete_id = rec.athlete_id
AND performs.perform = rec.perform
AND performs.category_id = rec.category_id
AND performs.discipline_id = rec.discipline_id
ORDER BY performs.perform
LIMIT 25 enter code here
EDIT - UPDATE
Ok, now I understand your problem. This is quite common, actually, where you want first to find the max value of a group and then request additional info on that value.
One way to achieve this is with a nested query as follow
SELECT ss.athlete_id,ss.perform,category_id
FROM performs ss
inner join
(SELECT
athlete_id, MIN(perform) AS perform
FROM
performs
WHERE
discipline_id = 4 AND category_id IN (1,3,5,7,9)
GROUP BY
athlete_id) tt
on tt.athlete_id = ss.athlete_id and ss.perform = tt.perform
The result is the one you described above.
If I use this query:
SELECT
athlete_id, MIN(perform) AS perform, category_id
FROM
performs
WHERE
discipline_id = 4 AND category_id IN (1,3,5,7,9)
GROUP BY
athlete_id, category_id
I get this results:
athlete_id perform category_id
1 11,14 1
1 11,54 3
1 11,54 5
1 10,71 7
2 11,04 1
2 11,24 3
2 11,54 5
2 12,14 7
3 10,94 1
4 10,94 1
4 11,34 3
But I need this result:
athlete_id perform category_id
1 10,71 7
2 11,04 1
3 10,94 1
4 10,94 1
For each athlete_id the best perform at once category_id.
This result will be JOIN with full table "perform".
SQL:
SELECT ss.athlete_id,ss.perform,category_id
FROM performs ss
INNER JOIN
(SELECT
athlete_id, MIN(perform) AS perform
FROM
performs
WHERE
discipline_id = 4 AND category_id IN (1,3,5,7,9)
GROUP BY
athlete_id) tt
ON tt.athlete_id = ss.athlete_id and ss.perform = tt.perform
Results:
athlete_id perform category_id
1 7,04 1
2 7,14 1
3 7,14 1
4 7,24 1
5 7,14 1
6 7,24 1
6 7,24 3
7 7,02 1
8 7,07 1
9 7,34 1
10 7,34 1
11 7,18 1
12 7,04 3
13 7,44 1
13 7,44 5
14 7,44 1
15 7,27 1
I´ve tried clause DISTINCT but results were same.

Count Category products with mysql

I have to count products inside each category like I have a category A that have two childs B and C.
In B category have 10 products and C category have 5 Products.
I want to write a query that count product like A = 15 B = 10 C = 5.
I have wrote a query that count only for child categories.
My query is given below
SELECT
c.`id`,
c.`parent_id`,
c.`name`,
c.`slug`,
c.`route_id`,
c.`description`,
c.`excerpt`,
c.`sequence`,
c.`image`,
c.`seo_title`,
c.`meta`,
c.`enabled`,
(SELECT
COUNT(`product_id`)
FROM
HM_category_products
WHERE
HM_category_products.category_id IN (SELECT
HM_categories.`id`
FROM
HM_categories
WHERE
(id = c.`id` OR id = c.`parent_id`))) AS count
FROM
`HM_categories` AS c
EDITED :- Below is my tables structure. This is just an example.
Category Table
id parent_id name slug enabled
1 0 Mobiles & Tablets mobiles-tablets 1
2 1 Mobile Phones mobile-phones 1
3 1 Tablets tablets 1
4 1 Mobile Accessories mobile-accessories 1
5 0 Electronics & Computers electronics-computers 1
6 5 Cameras & Accessories cameras-accessories 1
7 5 TV - Video - Audio tv-video-audio 1
Category Product Table
product_id category_id
1 2
2 2
3 2
4 2
5 3
6 2
7 3
8 3
9 3
10 2
11 3
12 2
13 2
14 2
15 2
You hav to use the "Group by" with something like:
SELECT
category_id, COUNT(*)
FROM
HM_category_products
GROUP BY category_id
This would get your table HM_category_products Grouped by category_id so will know how mane rows of each category its in the table, whats mean you will know the number of product of each category. Then you can join this resulting table with category one to get the category info. (Sorry for my english)
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns. SQL GROUP BY Statement
for more info abaut "Group by" read
12.16.2 GROUP BY Modifiers and
12.16.1 GROUP BY (Aggregate) Functions
EDIT: I see now what you whant. I already give you the direction but here its a step furder: DISCLAIMER: THIS QUERY ITS JUST EXAMPLE, IT DOSNT WORK
CREATE VIEW view_2 AS
SELECT
category_id, COUNT(*) AS product_sum
FROM
HM_category_products
GROUP BY category_id
CREATE VIEW view_1 AS
SELECT
*
FROM
HM_categories
LEFT JOIN
view_2 AS a ON HM_categories.id = a.category_id;
SELECT
id,
name,
(SELECT
SUM(product_sum)
FROM
view_1
WHERE
parent_id = final.id) as product_count
FROM
HM_categories AS final;
that way you will get the product of a parent category, your only missing a CASE on the last select "product_count" to the child categorys, but that sum its easy because you already hav it on the view_1 (again sorry for my english)