Get the count of group by results - mysql

I am getting count of some column by using group by. It returns results of the form,
col1 count
date1 1
date2 2
I would like to further count on this result so that it returns 3.
How would I go about accomplishing this?
SELECT t.p_date
,count(t.p_date) AS saturday
FROM t_p_booking t
WHERE t.p_id IN (
220
,221
)
AND dayofweek(t.p_date) = 7
AND date_format(t.p_date, '%Y%m') = : ccyymm
GROUP BY t.p_date

Add WITH ROLLUP to the end of your GROUP BY clause

Would that not just be:
SELECT count(*)
FROM t_p_booking t
WHERE t.p_id IN (
220
,221
)
AND dayofweek(t.p_date) = 7
AND date_format(t.p_date, '%Y%m') = : ccyymm
GROUP BY t.p_date

Related

achieve "order by" before "group by" in mysql

i have a DB with this structure
id, UserID, price, Date
,1, 10.00, 2000-01-01
,1, 25.00 ,2022-02-01
,2, 12.00 ,2000-01-05
,2, 13.00 ,2001-01-05
,2, 24.00 ,2022-01-01
,3, 23.00 ,2022-01-01
i want to show the price for each user based on newest date.
So just 1 row per UserID (latest date)
if we query above table. results need to be like this:
,1, 25.00 ,2022-02-01
,2, 24.00 ,2022-01-01
,3, 23.00 ,2022-01-01
i have tried those 2 commands but they are not working
SELECT UserID,price,Datee FROM (SELECT UserID,price,Datee FROM tbl
ORDER BY UserID ASC,datee DESC) as tb_temp GROUP BY UserID
also this
SELECT UserID,price,max(Datee) FROM tbl Group by UserID ORDER BY UserID ASC,datee DESC
this command show latest date but price is not based on that
so i need something like ORDER BY datee then group by userID or LIMIT 1 per userID
If you're on MySQL 8, the most common way to achieve this is with a window function:
select userid, price, date
from (select *
, row_number() over (partition by userid
order by date desc) as row_priority
from tbl
) subq
where subq.row_priority = 1
Here's a working demo on dbfiddle
You could use a window function to good effect as MarcinJ has done--another approach would be to use a correlated subquery:
SELECT
S1.UserID,
S1.price,
S1.Date
FROM
SampleDetails S1
WHERE
S1.Date = (SELECT MAX(S2.Date) FROM SampleDetails S2 WHERE S2.UserID = S1.UserID GROUP BY S2.UserID);
You can try it out on DB Fiddle.
You could try this, it's not the most efficient because it uses a sub-query:
SELECT t.UserID, t.price, t.Datee
FROM tbl t
JOIN tbl on t.id = (SELECT id FROM tbl WHERE UserID = t.UserID ORDER BY Datee DESC LIMIT 1)
GROUP BY UserID
The idea is to join the table to itself by finding the latest row for the user id.
I made this sql fiddle to test it: http://sqlfiddle.com/#!9/63d495/2

Add limit in group_concat

My mysql query like :
SELECT GROUP_CONCAT(scorecard_id ORDER BY scorecard_id DESC) as scorecard_id
FROM scorecard
WHERE partner_id = 121 AND client_id = 15
It returns me data like :
65,64,63,62,61,60,39,38,37,36,35,34,33,32,31,30,29...
I want to add limit in group_concat so that it returns first 2 values i.e:
65,64
Maybe enclosing by a subquery would do:
SELECT
GROUP_CONCAT(t.scorecard_id) AS scorecardId
FROM
(
SELECT
scorecard_id
FROM scorecard
WHERE
partner_id = 121
AND client_id = 15
ORDER BY scorecard_id DESC
LIMIT 2
) AS t;

Fetch only 2 results, one for each different value of a concrete field

In a MYSQL table with those 5 fields: id, user_id, date, type, uid where type can be 1 or 2, I'm looking for a single query where I can fetch 2 results, one for type=1 and another one for type=2 based on date field.
Right now i have the following query which only gives me the last uid without taking care of the type field.
SELECT t.uid
FROM table AS t
WHERE t.user_id = 666
ORDER BY t.date
DESC LIMIT 1
Does anyone know how should modify this query so i can get the last uid for type=1 and the last one for type=2 based on date field? I would like to keep a a single query
Union all is probably the simplest method:
(select t.*
from t
where t.user_id = 666 and t.type = 1
order by date desc
limit 1
) union all
(select t.*
from t
where t.user_id = 666 and t.type = 2
order by date desc
limit 1
)
Finally i updated the query following this "paradigm":
http://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html
http://jan.kneschke.de/projects/mysql/groupwise-max/
This is how the query ended up:
SELECT s1.type, s1.uid
FROM t AS s1
LEFT JOIN t AS s2 ON s1.type = s2.type AND s1.date < s2.date
WHERE s2.date IS NULL;
Here's a visual example: http://hastebin.com/ibinidasuw.vhdl
Credits are for snoyes from #sql on Freenode. :)

MYSQL DISTINCT and ORDER BY together

I have a table like the following
item_id position_number position_date
1 9 2013-06-29 15:12:58
2 7 2013-07-25 15:12:58
18 5 2013-07-08 12:07:00
13 9 2013-07-08 12:07:00
I want to get the items group by position_number and order by position_date DESC, so the query will return the following:
item_id position_number position_date
13 9 2013-07-08 12:07:00
2 7 2013-07-25 15:12:58
18 5 2013-07-08 12:07:00
I've been implementing some of the solutions that use DISTINCT and GROUP BY, but not get the desired result.
Does anyone have an idea about how to solved it?
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT position_number, MAX(position_date) position_date
FROM tableName
GROUP BY position_number
) b ON a.position_number = b.position_number AND
a.position_date = b.position_date
ORDER BY a.position_number DESC
SQLFiddle Demo
Given your example data, this query will return the specified resultset:
SELECT t.item_id
, t.position_number
, t.position_date
FROM ( SELECT MAX(n.item_id) AS max_item_id
FROM mytable n
GROUP BY position_number
) m
JOIN mytable t
ON t.item_id = m.max_item_id
ORDER BY t.position_number DESC
NOTE This is choosing a single item_id for each position_number. This is assuming that a given item_id will appear only once, and have a single position_number. (If an item_id can be associated with multiple postion_number, the query can be tweaked. This is using the MAX() function to choose the item_id with the largest value. (The only example of a row being excluded is item_id=1.)
try this
select * from your_table group by position_number order by position_date DESC
EDIT:
SELECT `item_id`, max(`position_number`) position_number , max(`position_date`) position_date FROM TABLENAME
GROUP BY POSITION_NUMBER
ORDER BY POSITION_DATE DESC
DEMO
Here is SQLFiddle
SELECT * FROM TABLE_NAME
GROUP BY POSITION_NUMBER
ORDER BY POSITION_DATE DESC
Try this code:
SELECT * FROM TABLE_NAME GROUP BY POSITION_NUMBER ORDER BY POSITION_DATE DESC
Use GROUP_BY and either MIN or MAX to decide which date in the group you want to use for sorting.
SELECT * FROM my_table GROUP BY position_number ORDER BY MAX(position_date) DESC;

Get last record in each group and SUM some of them

i have a problem with sql query to mysql to take the last record in each group and sum some field in one query.i have a table:
name date interested made_call
andrew.h 2011-02-04 10 10
andrew.h 2011-02-11 20 10
andrew.h 2011-02-13 2 10
sasha.g 2011-02-11 5 20
sasha.g 2011-02-12 5 1
i need to sum made_call column grouping by name and return the last record from interested.
here what i want to get in result:
name date interested made_call
andrew.h 2011-02-13 2 30
sasha.g 2011-02-12 5 21
i tried to get result with this query
SELECT a.name,a.date,a.interested,sum(made_call) as made_call
FROM `resultboard` a
WHERE a.attendence = 1
AND NOT EXISTS (select 1 from resultboard where name = a.name
and id > a.id and attendence = 1)
GROUP BY name
but in result i got
andrew.h 2011-02-13 2 10
sasha.g 2011-02-12 5 1
so the query didnot sum, just return the last record from group
help)
That may be a little slow if the table is very big, but it will get the wanted result:
SELECT a.name, t.date, a.interested, t.calls
FROM resultboard a
JOIN (SELECT name, MAX(date) AS date, SUM(made_call) AS calls FROM resultboard
GROUP BY name) AS t
ON a.name = t.name AND a.date = t.date
Your WHERE clause is eliminating all but the last row from consideration as part of the sum.
In some other DB's you could use the LAST aggregate function. MySQL doesn't have that, but you can emulate it like so for your case:
SELECT
a.name,
SUBSTRING_INDEX(
GROUP_CONCAT(CAST(a.date AS CHAR) ORDER BY date desc),
',', 1
) AS date,
SUBSTRING_INDEX(
GROUP_CONCAT(CAST(a.interested AS CHAR) ORDER BY date desc),
',', 1
) AS interested,
sum(made_call) as made_call
FROM `resultboard` a
WHERE a.attendence = 1
GROUP BY name
It might not be fast on large data sets, but it should at least do the job if my research is correct. I haven't tested this, so YMMV.
I think that using WITH ROLLUP for GROUP BY modifier may help you. http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html
Edit; I got i wrong , no need for WITH ROLLUP
SELECT r.name,
MAX(r.date) as date,
(SELECT r2.interested FROM resultboard r2 WHERE r2.name = r.name ORDER BY r.date DESC LIMIT 1),
SUM(made_call) as made_call
FROM resultboard r
GROUP BY name;