Well I have a table(MYSQL) 'product', with one of the columns named 'category_id' that stores category id from another table 'category' . So there are multiple categories in 'category_id' column. eg table below. Now I want to select fixed number of product from each category(say 2), for eg i want to select 2 latest products from each category from the product table. What mysql query should i use, any idea?
Product:
+-------------+------+---------------------+
| category_id | name | timestamp |
+-------------+------+---------------------+
| 1 | abc | 2016-02-27 16:04:00 |
| 1 | def | 2016-02-28 16:10:00 |
| 1 | ghi | 2016-02-29 16:11:00 |
| 2 | pqr | 2016-02-27 16:04:00 |
| 2 | stu | 2016-02-27 16:05:00 |
| 3 | vwx | 2016-02-28 16:04:00 |
+-------------+------+---------------------+
Expected Result:
Product:
+-------------+------+---------------------+
| category_id | name | timestamp |
+-------------+------+---------------------+
| 1 | def | 2016-02-28 16:10:00 |
| 1 | ghi | 2016-02-29 16:11:00 |
| 2 | pqr | 2016-02-27 16:04:00 |
| 2 | stu | 2016-02-27 16:05:00 |
| 3 | vwx | 2016-02-28 16:04:00 |
+-------------+------+---------------------+
It may help you...
Check the Output in SQL Fiddle Link: http://sqlfiddle.com/#!9/3942d2/6
SELECT
category_id,
name,
tsDate
FROM
(
SELECT
category_id,
name,
tsDate,
#rn := IF(#prev = category_id, #rn + 1, 1) AS rn,
#prev := category_id
FROM Product
JOIN (SELECT #prev := NULL, #rn := 0) AS vars
ORDER BY category_id, tsDate DESC, name
) AS T1
WHERE rn <= 2
Try this :
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.category_id = x.category_id
AND y.timestamp >= x.timestamp
GROUP
BY x.category_id,x.timestamp
HAVING COUNT(*) <= 2;
Related
I want to get the categories pricing, Say if there is data in customer_category_pricing table then fetch that pricing for that specific customer. Otherwise fetch default prices from categories table.
i have tried achieving this desired result using mysql case it is working fine, but the problem is, it is returning two rows
hourly_amount_final column returns updated price then per_day_amount_final returns default price
Then in next row, hourly_amount_final column return default price and then per_day_amount_final returns updated price.
table: pr_categories
---------------------------------------------------------------------------------
| id | title | hourly_amount | per_day_amount | created_at| updated_at
---------------------------------------------------------------------------------
| 1 | Power Generation | 100.00 | 200.00 |
| 2 | Local Government | 300.00 | 400.00 |
----------------------------------------------------------
table: pr_customer_categories_pricing
------------------------------------------------------------------------------------
| id | customer_id | category_id | billing_type_id | amount | created_at | updated_at
------------------------------------------------------------------------------------
| 1 | 1 | 1 | 1 | 109 |
| 2 | 1 | 1 | 2 | 600 |
----------------------------------------------------------
table: pr_billing_types
----------------
| id | title |
--------------
| 1 | Hourly |
| 2 | Per Day |
----------------
This is the query i am working with at the moment:
SELECT c.id,c.title,
CASE
WHEN (c.hourly_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 1))
THEN (SELECT ccp.amount WHERE ccp.billing_type_id = 1)
ELSE c.hourly_amount
END
AS hourly_amount_final,
CASE
WHEN (c.per_day_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 2))
THEN (SELECT ccp.amount WHERE ccp.billing_type_id = 2)
ELSE c.per_day_amount
END
AS per_day_amount_final
FROM pr_customer_category_pricing AS ccp
RIGHT JOIN pr_categories AS c
ON c.id = ccp.category_id AND ccp.customer_id = 1
Expected result when there is no data in pr_customer_category_pricing
----------------------------------------------------------------------
| id | title | hourly_amount_final | per_day_amount_final |
----------------------------------------------------------------------
| 1 | Power Generation | 100.00 | 200.00 |
| 2 | Local Government | 300.00 | 600.00 |
----------------------------------------------------------------------
Expected result when there is data in pr_customer_category_pricing
----------------------------------------------------------------------
| id | title | hourly_amount_final | per_day_amount_final |
----------------------------------------------------------------------
| 1 | Power Generation | 109.00 | 600.00 |
| 2 | Local Government | 300.00 | 400.00 |
----------------------------------------------------------------------
Actual result what i'm getting:
----------------------------------------------------------------------
| id | title | hourly_amount_final | per_day_amount_final |
----------------------------------------------------------------------
| 1 | Power Generation | 109.00 | 200.00 |
| 1 | Power Generation | 100.00 | 600.00 |
| 2 | Local Government | 300.00 | 400.00 |
----------------------------------------------------------------------
What am i doing wrong? Any suggestions! Help a brother out. :S
Since there can only be one entry in the pr_customer_category_pricing table per billing type, you can simplify things by creating a derived pivot table from the pr_customer_category_pricing with values for each of the billing_type_id in separate columns. You can then simply COALESCE the value from the derived table with the value from the pr_categories for each billing_type_id:
SELECT c.id,c.title,
COALESCE(ccp.hourly_amount, c.hourly_amount) AS hourly_amount_final,
COALESCE(ccp.per_day_amount, c.per_day_amount) AS per_day_amount_final
FROM (SELECT
customer_id,
category_id,
MAX(CASE WHEN billing_type_id = 1 THEN amount END) AS hourly_amount,
MAX(CASE WHEN billing_type_id = 2 THEN amount END) AS per_day_amount
FROM pr_customer_category_pricing
GROUP BY customer_id, category_id) AS ccp
RIGHT JOIN pr_categories AS c
ON c.id = ccp.category_id AND ccp.customer_id = 1
Output:
id title hourly_amount_final per_day_amount_final
1 Power Generation 109 600
2 Local Government 300 400
Demo on dbfiddle
You can use max() aggregation with group by
SELECT c.id,c.title,
max(CASE
WHEN (c.hourly_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 1))
THEN (SELECT ccp.amount WHERE ccp.billing_type_id = 1)
ELSE c.hourly_amount
END)
AS hourly_amount_final,
max(CASE
WHEN (c.per_day_amount <> (SELECT ccp.amount WHERE ccp.billing_type_id = 2))
THEN (SELECT ccp.amount WHERE ccp.billing_type_id = 2)
ELSE c.per_day_amount
END)
AS per_day_amount_final
FROM pr_customer_category_pricing AS ccp
RIGHT JOIN pr_categories AS c
ON c.id = ccp.category_id AND ccp.customer_id = 1
group by c.id,c.title
Category Table
mysql> SELECT * FROM cats;
+------+------+-----------+
| c_id | p_id | c_name |
+------+------+-----------+
| 1 | 1 | cats 1 |
| 2 | 1 | cats 2 |
| 3 | 1 | cats 3 |
+------+------+-----------+
Meta Table
mysql> SELECT * FROM meta;
+------+------+------+---------+-------------+-------+
| m_id | p_id | c_id | name | description | costs |
+------+------+------+---------+-------------+-------+
| 1 | 1 | 1 | Abhijit | description | 100 |
| 2 | 1 | 1 | Abhijit | description | 200 |
| 3 | 1 | 2 | Abhiji2 | description | 500 |
+------+------+------+---------+-------------+-------+
Transaction Table
mysql> SELECT * FROM transactions;
+------+------+------+---------------------+--------+
| t_id | p_id | m_id | date | amount |
+------+------+------+---------------------+--------+
| 1 | 1 | 1 | 2016-02-16 11:17:06 | 50 |
| 2 | 1 | 1 | 2016-02-16 11:17:06 | 50 |
| 3 | 1 | 2 | 2016-02-16 11:17:06 | 50 |
| 4 | 1 | 2 | 2016-02-16 11:17:06 | 150 |
+------+------+------+---------------------+--------+
I want to sum() for each category costs (from meta table) and amount( from transaction table).
I use:
mysql> SELECT c.*, SUM(t.amount), SUM(m.costs)
FROM cats c
LEFT JOIN meta m ON m.c_id=c.c_id
LEFT JOIN transactions t ON t.m_id=m.m_id
GROUP BY c.c_id;
+------+------+-----------+--------+---------------+--------------+
| c_id | p_id | c_name | add_by | SUM(t.amount) | SUM(m.costs) |
+------+------+-----------+--------+---------------+--------------+
| 1 | 1 | Abhijit | 1 | 100 | 400 |
| 2 | 1 | Abhiji2 | 1 | 200 | 500 |
+------+------+-----------+--------+---------------+--------------+
It's wrong. The Costs of cats id 1 is 300 but here I got 400
I Want Get Return From Query Like This:
+------+------+-----------+--------+---------------+--------------+
| c_id | p_id | c_name | add_by | SUM(t.amount) | SUM(m.costs) |
+------+------+-----------+--------+---------------+--------------+
| 1 | 1 | Abhijit | 1 | 100 | 300 |
| 2 | 1 | Abhiji2 | 1 | 200 | 500 |
+------+------+-----------+--------+---------------+--------------+
I think you had a typo (or error) in one of your JOIN conditions. I think you intended your original query to be this:
SELECT c.*, SUM(t.amount), SUM(m.costs)
FROM cats c
LEFT JOIN meta m ON m.c_id = c.c_id
LEFT JOIN transactions t ON t.m_id = m.c_id
GROUP BY c.c_id;
Note carefully ON t.m_id = m.c_id, which agrees with your expected output. In any case, I reworked your query as follows:
SELECT c.c_id, c.p_id, c.c_name, t2.transactionCosts, t1.metaCosts
FROM cats c
LEFT JOIN
(
SELECT c_id, SUM(costs) AS metaCosts
FROM meta
GROUP BY c_id
) t1
ON c.c_id = t1.c_id
LEFT JOIN
(
SELECT m_id, SUM(amount) AS transactionCosts
FROM transactions
GROUP BY m_id
) t2
ON c.c_id = t2.m_id
WHERE t2.transactionCosts IS NOT NULL OR t1.metaCosts IS NOT NULL;
The first subquery computes the meta total for each c_id, and the second subquery computes the transaction total for each m_id. These results are then both joined together with the cats table to get your final result.
Follow the link below for a running demo:
SQLFiddle
the problem is you select c.* but only group by c_id, in this case you have 2 options. window function or subquery.
via over(partition by):
SELECT c.*,
SUM(t.amount)over(partition by c.c_id) as amount,
SUM(m.costs)over(partition by c.c_id) as cost
FROM con_cats c
LEFT JOIN meta m ON m.c_id=c.c_id
LEFT JOIN transactions t ON t.m_id=m.m_id;
via subquery:
select a.*,b.amount,b.costs from con_cats a
inner join
(SELECT c.c_id, SUM(t.amount) as amount, SUM(m.costs) as costs
FROM con_cats c
LEFT JOIN meta m ON m.c_id=c.c_id
LEFT JOIN transactions t ON t.m_id=m.m_id
GROUP BY c.c_id) b
on a.c_id = b.c_id;
I have this table below that I need to get the number of student that get A in their exam. This is what I want to achieve from table below:
3 A's = 0 student. 2 A's = 3 student.
+--------------+------------+------+
| student_ID | kod_subjek | gred |
+--------------+------------+------+
| 746123096687 | 02 | A |
| 746123096687 | 12 | B |
| 746123096687 | 21 | A |
| 860206145454 | 12 | A |
| 860206145454 | 02 | A |
| 881012085535 | 02 | A |
| 881012085535 | 21 | A |
+--------------+------------+------+
I try with:
mysql> SELECT student_ID, COUNT(gred) FROM data_exam GROUP BY student_ID;
The output is:
+--------------+-------------+
| student_ID | COUNT(gred) |
+--------------+-------------+
| 746123096687 | 3 |
| 860206145454 | 2 |
| 881012085535 | 2 |
+--------------+-------------+
It not work. It will just count all the grade for specific student. Please help me solve this. Thank you.
SELECT a_count, COUNT(*) AS cnt
FROM
(
SELECT COUNT(*) AS a_count
FROM data_exam
WHERE gred = 'A'
GROUP BY student_id
) x
GROUP BY a_count
ORDER BY a_count
Example on SQL-Fiddle returns:
a_count cnt
2 3
You can use a subquery:
SELECT num_as, COUNT(1) AS num_students
FROM (
SELECT student_ID, COUNT(gred) AS num_as
FROM data_exam
WHERE gred = 'A'
GROUP BY student_ID
) counts_by_student
GROUP BY num_as
My table has 4 tags linked to the to each id. I want to select the position(rank) of that value(tag_name or tag_id) and the number of times the value(tag_name or tag_id) displays in that same position(rank).
Here's what it would look like in mysql:
> +--------+------------+--------+------+
> | id | tag_name | tag_id | rank |
> +--------+------------+--------+------+
> | 2345 | cookie | 2 | 1 |
> | 2345 | bar | 1 | 2 |
> | 2345 | cereal | 3 | 3 |
> | 2345 | milk | 4 | 4 |
> | 2346 | cereal | 3 | 1 |
> | 2346 | milk | 4 | 2 |
> | 2346 | cookie | 2 | 3 |
> | 2346 | hot dogs | 5 | 4 |
> | 2347 | chocolate | 6 | 1 |
> | 2347 | bar | 1 | 2 |
> +--------+------------+--------+------+
Here's what my current code looks like:
SELECT m.*, tr.tag_id, t.tag_name, #rownum:=#rownum + 1 AS rank
FROM meals AS m
RIGHT JOIN tags_rel AS tr ON tr.meal_id = m.id
JOIN tags AS t ON tr.tag_id = t.id
JOIN (SELECT #rownum:=0) AS r
ORDER BY m.id DESC
Please keep in mind that I'm actually using a pagination so my ORDER BY actually looks like this:
ORDER BY id DESC LIMIT $start_from, 12
If I select either tag_name=bar or tag_id=1 and rank=2 I should get the bellow results. Plus I would like the count of rows returned for that value.
> +--------+------------+--------+------+
> | id | tag_name | tag_id | rank |
> +--------+------------+--------+------+
> | 2345 | bar | 1 | 2 |
> | 2347 | bar | 1 | 2 |
> +--------+------------+--------+------+
Thanks!
SELECT tag_name, tag_id, rank, COUNT(tag_id) AS rankcount
FROM table
GROUP BY tag_id, rank;
results in:
+-----------+--------+------+-----------+
| tag_name | tag_id | rank | rankcount |
+-----------+--------+------+-----------+
| bar | 1 | 2 | 2 |
| cookie | 2 | 1 | 1 |
| cookie | 2 | 3 | 1 |
| cereal | 3 | 1 | 1 |
| cereal | 3 | 3 | 1 |
| milk | 4 | 2 | 1 |
| milk | 4 | 4 | 1 |
| hot dogs | 5 | 4 | 1 |
| chocolate | 6 | 1 | 1 |
+-----------+--------+------+-----------+
9 rows in set (0.00 sec)
If you want your original table enhanced by a count of lines (although that seems to be quite senseless, as every sql result returns the number of rows) you'll have to do a subquery:
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table;
+------+-----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+-----------+------+------+---------------+
| 2345 | cookie | 2 | 1 | 2 |
| 2345 | bar | 1 | 2 | 2 |
| 2345 | cereal | 3 | 3 | 2 |
| 2345 | milk | 4 | 4 | 2 |
| 2346 | cereal | 3 | 1 | 2 |
| 2346 | milk | 4 | 2 | 2 |
| 2346 | cookie | 2 | 3 | 2 |
| 2346 | hot dogs | 5 | 4 | 1 |
| 2347 | chocolate | 6 | 1 | 1 |
| 2347 | bar | 1 | 2 | 2 |
+------+-----------+------+------+---------------+
10 rows in set (0.00 sec)
Extending that query would result in your desired result, extended by a column where the count of rows is inserted (which is IMO still rather senseless).
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table
WHERE tag_id = 1;
results in
+------+----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+----------+------+------+---------------+
| 2345 | bar | 1 | 2 | 2 |
| 2347 | bar | 1 | 2 | 2 |
+------+----------+------+------+---------------+
2 rows in set (0.00 sec)
how about this:
SELECT DISTINCT meal_id as MealId, t2name as TagName, t2id as TagId, Rank
FROM
(SELECT t1id as t2id, t1name as t2name, rnk as rnk2, count(*) as Rank
FROM
tags_rel AS r,
(SELECT m1id, t1id, t1name,
#rnk := CASE WHEN #id <> m1id THEN 1 ELSE #rnk + 1 END AS rnk, #id := m1id as idx
FROM
tags_rel AS r,
(SELECT #id := 0) idx,
(SELECT #rnk := 0) rxx,
(SELECT m.id AS m1id, t.id AS t1id, name AS t1name
FROM tags_rel r
JOIN (meals AS m, tags AS t ) ON ( m.id = r.meal_id AND t.id = r.tag_id )
) AS rn
WHERE r.tag_id = t1id AND r.meal_id = m1id
ORDER BY m1id, t1name
) AS rno
WHERE r.tag_id = t1id AND r.meal_id = m1id
group BY t1id, t1name, rnk
) as rn2
join (meals, tags_rel) on (meals.id = tags_rel.meal_id and tags_rel.tag_id = t2id)
ORDER BY meal_id, t2id
which results in:
2345 bar 1 2
2345 cookie 2 1
2345 cereal 3 1
2345 milk 4 2
2346 cookie 2 1
2346 cereal 3 1
2346 milk 4 2
2346 hotdog 5 1
2347 bar 1 2
2347 chocolad 6 1
I am making a website where users can vote on which category a page is. They can vote that the page is in category a, b, c, or d.
I need to find the most commonly occurring category in the MySQL row out of all the votes.
Each time a user submits their vote, it submits the "category" that they voted for, and the "page_id".
I have this so far:
SELECT page_id, category
FROM categories
GROUP BY page_id
I cannot use a COUNT(*) WHERE category = 'a' then repeat it for each category because there is many more categories in the actual project.
If your table looks something like this:
SELECT * from categories;
+---------+----------+
| page_id | category |
+---------+----------+
| 1 | a |
| 1 | b |
| 1 | a |
| 1 | c |
| 1 | a |
| 1 | b |
| 1 | a |
| 2 | d |
| 2 | d |
| 2 | c |
| 2 | d |
| 3 | a |
| 3 | b |
| 3 | c |
| 4 | c |
| 4 | d |
| 4 | c |
+---------+----------+
17 rows in set (0.00 sec)
Then you may want to try this query:
SELECT c1.page_id, MAX(freq.total),
(
SELECT c2.category
FROM categories c2
WHERE c2.page_id = c1.page_id
GROUP BY c2.category
HAVING COUNT(*) = MAX(freq.total)
LIMIT 1
) AS category
FROM categories c1
JOIN (
SELECT page_id, category, count(*) total
FROM categories
GROUP BY page_id, category
) freq ON (freq.page_id = c1.page_id)
GROUP BY c1.page_id;
Which returns this:
+---------+-----------------+----------+
| page_id | MAX(freq.total) | category |
+---------+-----------------+----------+
| 1 | 4 | a |
| 2 | 3 | d |
| 3 | 1 | a |
| 4 | 2 | c |
+---------+-----------------+----------+
4 rows in set (0.00 sec)
Compare the results with the actual frequency distribution:
SELECT page_id, category, COUNT(*) FROM categories GROUP BY page_id, category;
+---------+----------+----------+
| page_id | category | COUNT(*) |
+---------+----------+----------+
| 1 | a | 4 |
| 1 | b | 2 |
| 1 | c | 1 |
| 2 | c | 1 |
| 2 | d | 3 |
| 3 | a | 1 |
| 3 | b | 1 |
| 3 | c | 1 |
| 4 | c | 2 |
| 4 | d | 1 |
+---------+----------+----------+
10 rows in set (0.00 sec)
Note that for page_id = 3, there is no leading frequency, in which case this query makes no guarantee on which category will be chosen in such a case.
something like
SELECT category, page_id, count(vote_id)
FROM categories
WHERE category in ('a', 'b', 'c', 'd')
GROUP BY category, page_id
ORDER BY count(vote_id) DESC
LIMIT 1
should do the trick. I assume here the votes are individually stored in a separate row per vote.
It only looks in the cqtegory you're interested in, sorts with the most votes first and only returns the first one.