How to get last record from single group - mysql

I have following type of table.I want to output of last record(most recent) of particular group.Please suggest My sql query.
Id Name random number
-------------------------
1 A 1233
2 A 1778
3 A 1221
4 B 1298
5 B 1289
6 C 1267
I want a last record of group A
e.g.
ID Name Random number
----------------------
3 A 1221

select id, name, random from table where Name='A' order by id desc limit 1

Here is query :
select * from tbl where id IN (select max(id) from tbl group by name);
And here is fiddle: http://sqlfiddle.com/#!2/01d69/8

SELECT * From Table1 Where [Id] in (
SELECT Max([Id]) as [maxId] From Table1 Where [Name] = 'A')
Fiddle

Related

MySql , How to select the not repeated rows only

I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;

MySQL - How to select first string in group by clause by date

Given this transactions table
ID Date Name Stat1
1 1 a 1
1 2 b 1
1 3 c 1
2 4 d 1
2 5 e 1
I want to group the records by ID, and have the column 'Name' per group ID to show the last name by date as it is appears in table (high number in date is chronologically last). final result:
ID Date Name Stat1
1 3 c 3
2 5 e 2
Unfortunately this code:
Select ID,Date,Name,sum(Stat1)
From myTable
Group by ID
will generate "random" name,
how do i force choose the first/last record name(by date) in each group (ID)?
Thanks
You can do it with a query like this.
Change min to max if you want to have the first or last row of a group:
SELECT
result.id,
result.mdate,
mt.name,
result.stat1
FROM (
Select
ID,
min(date) mdate,
sum(Stat1) as stat1
From myTable
Group by ID
) AS result
LEFT JOIN myTable mt
ON mt.id = result.id
AND mt.date = result.mdate;

select only rowwith highest id value if there are identical column values

how can I select all from a table, and if there are identical values of column name then only select the row that has the greatest id value so If there was a table like this:
id name age country
---+------+---+-------
1 bob 24 UK
2 john 48 USA
3 janet 72 USSR
4 bob 96 Ukraine
it would only select the 'bob' with the highest id so the result would return:
id name age country
---+------+---+-------
2 john 48 USA
3 janet 72 USSR
4 bob 96 Ukraine
Thank you.
Try this query
select * from table_name where ID in(select MAX(ID) from table_name group by name)
You could use a subquery to calculate the maximum ID for every name, and then return all rows that matches the IDs returned by the subquery:
SELECT *
FROM People
WHERE id IN (SELECT MAX(id) FROM People GROUP BY Name)
Please see fiddle here.
You could use a not exists subquery that filters out rows with with the same name and a greater id:
select *
from People p1
where not exists
(
select *
from People p2
where p1.Name = p2.Name
and p2.Id > p1.Id
)
you can do it this way :
Select Table_1.* from table_1 inner join (
Select Max(ID) as ID from Table_1 Group by ID) x On Table.Id on x.ID

Counting unique numbers in a column MySQL

I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number

How to achieve next and previous row in a query

I have a table like this in MYSQL:
SELECT * FROM test_table
id u_id name date
1 101 name2 2012-05-14
2 305 name3 2012-05-11
3 506 name4 2012-05-05
4 207 name5 2012-05-12
5 108 name6 2012-05-03
SELECT id,u_id from test_table order by date;
id u_id
5 108
3 506
2 305
4 207
1 101
I have a application where where these things are displayed
on clicking on any u_id it takes me to a different page which displyed details stuff
Now I want to write a query which willl give me the next record
How do I achive the next record from here like
when I say u_id>305 it shoud give me 207?
and u_id<305 it should give me 506
I think this is what you are looking for:
SELECT id, u_id
FROM test_table
WHERE uid > 305
ORDER BY date ASC
LIMIT 1;
SELECT id, u_id
FROM test_table
WHERE uid < 305
ORDER BY date DESC
LIMIT 1;
Given the u_id is 305, to get the next record by date:
SELECT id,u_id
FROM test_table a
WHERE a.date > (SELECT date FROM test_table b WHERE b.u_id = 305)
ORDER BY a.date
LIMIT 1;
And to get the previous record by date:
SELECT id,u_id
FROM test_table a
WHERE a.date < (SELECT date FROM test_table b WHERE b.u_id = 305)
ORDER BY a.date DESC
LIMIT 1;
try this:
select id,u_id from
(SELECT id,u_id,#rownum:= #rownum+1 AS Sno
from test_table , (SELECT #rownum:=0) r;
order by date)a
where Sno=<#ID-1/+1>
lets say if your current Sno=5 then 4 will give the previous record and 6 will give the next record