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
Related
I have two tables, 1 look like
tableA
id name value
------------------
1 Joe 22
2 John 50
3 Joe 38
4 Joe 10
5 John 20
I need to add all the value of value with same name from tableA and store in the tableB like this
tableB
id name value
---------------
1 Joe 70
2 John 70
PLEASE HELP
Assuming the tableb already exists, you can insert the data like this:
insert into tableb(id, name, value)
select min(id), name, sum(value)
from tablea
group by name;
Hi use (Select * INTO ) like this.
SELECT * INTO Table2
FROM Table1
insert into tableb(id, name, value)
select id, name, sum(value)
from tablea
group by name,id;
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;
SELECT *
FROM (SELECT id, user, MAX(score) FROM table_1 GROUP BY user) AS sub
ORDER BY 'sub.score' ASC;
This SQL query should select from a table only a score per user, and for accuracy, the highest.
The table structure is this:
+-----------------------+
| id | score | username |
+-----------------------+
| 1 | 15 | mike |
| 2 | 23 | tom |
| 3 | 16 | mike |
| 4 | 22 | jack |
etc..
The result should be like:
3 mike 16
2 tom 23
4 jack 22
And then reordered:
3 mike 16
4 jack 22
2 tom 23
But the query does not reorder the subquery by score. How to do so?
Let's look at what you are doing step by step:
SELECT id, user, MAX(score) FROM table_1 GROUP BY user
Here you are grouping by user name, so you get one result row per user name. In this result row you select the user name, the maximum score found for this user name (which is 16 for 'mike') and one of the IDs found for the user name (which can be 1 or 3 for 'mike', the DBMS is free to choose one). This is probably not what you want.
SELECT * FROM (...) AS sub ORDER BY 'sub.score' ASC;
'sub.score' is a string (single quotes). You want to order by the max score from your subquery instead. So first give the max(score) a name, e.g. max(score) as max_score, and then access that: ORDER BY sub.max_score ASC.
Anyway, if you want the record with the maximum score for a user name (so as to get the according ID, too), you could look for records for which not exists a record with the same user name and a higher score. Sorting is easy then: as there is no aggregation, you simply order by score:
select * from table_1 t1 where not exists
(select * from table_1 higher where higher.name = t1.name and higher.score > t1.score)
order by score;
Assuming user|score is unique..:
SELECT x.*
FROM table_1 x
JOIN ( SELECT user, MAX(score) score FROM table_1 GROUP BY user) y
ON y.user = x.user
AND y.score = x.score
ORDER BY x.score
No need to write sub queries.
Simply you can use this way:
SELECT id, `user`, MAX(score) FROM table_1 GROUP BY `user`
ORDER BY MAX(score);
If you want query with sub query:
SELECT * FROM (SELECT id, `user`, MAX(score) as max_score FROM table_1
GROUP BY `user`) AS sub ORDER BY max_score;
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
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