I want to retrieve only the first row from each duplicate id in a table.
The issue is that every other field except the id is unique, so that means I cannot "filter" the rows.
Here is my scientist table:
id
firstname
lastname
1
albert
einstein
1
albert1
einstein1
1
albert2
einstein2
1
albert3
einstein3
2
isaac
newton
2
isaac1
newton1
3
marie
curie
3
marie1
curie1
3
marie2
curie2
Note: The firstname and the lastname are irrelevant I just want to extract the first row of each id.
And below you can find my desired output:
id
firstname
lastname
1
albert
einstein
2
isaac
newton
3
marie
curie
I have tried to GROUP BY id but it is not working.
Also, I have tried to select the scientist table based on the distinct ids of the same table
SELECT * FROM scientist WHERE id IN (SELECT DISTINCT id FROM scientist)
But then I realised that my logic was incorrect.
Try it here enter link description here
select id,firtname,lastname
from(
select *, ROW_NUMBER() OVER(PARTITION BY scientist.id) c
from scientist) t
Where c = 1
Related
I have a table in which I want to compare an ID column with another column in the same table.
This is my table
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
4 Alex 5
For example I want to use
select * from mytable where ID = BossID
and I expected to get that Peter is the Boss of John and Alex is the Boss of Max and Peter, but when I use it this way, I dont get any info on the query...
Any idea to get it?
The expected query result are:
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
seems like you want the list of employees who their boss exists in the list as well:
select *
from mytable
where bossid in (select id from mytable);
to get the boss name :
select t.ID, t.Name, b.Name as BossName
from mytable t
join mytbale b
on t.bossid = b.id
you can use left join to return all the employees in the list
If I have a table that is Names, which has just first name and last name as columns and I want to select everything (first and last names), but limit the number of repeated first names to 3. So for example,
First Name
Last Name
John
Smith
John
Doe
Ryan
Green
Mike
Williams
John
Thompson
John
Brown
becomes
First Name
Last Name
John
Smith
John
Doe
Ryan
Green
Mike
Williams
John
Thompson
How do I construct a query to do that?
You can use window functions. For instance:
select firstname, lastname
from (select t.*,
row_number() over (partition by firstname order by rand()) as seqnum
from t
) t
where seqnum <= 3;
Note: This returns random rows for each name. You can control which rows you get using the order by clause.
I need to create a digest of records which are grouped by names in a table. The problem is, my table has two columns for names and the same names can be in either column. The digest should list all names and the records in which they exist, regardless of which column.
I can see doing this by first generating a unique list of names from both columns, then scanning through the result to see if the names appear in either column. I want to know if MySQL can do this in one query.
Example, here's my table:
Id ColumnA Column B
1 Bill NULL
2 NULL Dennis
3 Adam Carl
4 NULL Adam
5 Adam Bill
6 Dennis NULL
7 Frank Bill
The result of the query (NULL sorts to the top)
NULL 4 NULL Adam
NULL 1 Bill NULL
NULL 2 NULL Dennis
NULL 6 Dennis NULL
Adam 5 Adam Bill
Adam 7 Adam Carl
Bill 1 Bill NULL
Bill 5 Adam Bill
Bill 6 Frank Bill
Carl 3 Adam Carl
Dennis 2 NULL Dennis
Dennis 6 Dennis NULL
Frank 7 Frank Bill
It would be nice if I could eliminate the NULL entries at the top of the list. I only put them there because everything I'm trying so far has them at the top. I'd rather the result looks like this:
Adam 5 Adam Bill
Adam 7 Adam Carl
Bill 1 Bill NULL
Bill 5 Adam Bill
Bill 6 Frank Bill
Carl 3 Adam Carl
Dennis 2 NULL Dennis
Dennis 6 Dennis NULL
Frank 7 Frank Bill
You need to do a union of ID, ColumnA with ID, ColumnB, then summarize and order the result.
Like so http://sqlfiddle.com/#!2/eeec6/5/0:
SELECT name, GROUP_CONCAT(DISTINCT id ORDER BY id) AS ids
FROM (
SELECT id, a AS name
FROM names
UNION
SELECT id, b AS name
FROM names
) AS t
WHERE name IS NOT NULL
GROUP BY name
ORDER BY name
This will give you one row per name, with a list of IDs (row numbers) containing that name.
Or you can simply display the name, id columns one at a time(http://sqlfiddle.com/#!2/eeec6/6/0):
SELECT name, id
FROM (
SELECT id, a AS name
FROM names
UNION
SELECT id, b AS name
FROM names
) AS t
WHERE name IS NOT NULL
ORDER BY name, id
Finally, if you want to show the whole row with each name, you can do this (http://sqlfiddle.com/#!2/eeec6/8/0):
SELECT name, n.id, n.a, n.b
FROM (
SELECT id, a AS name
FROM names
UNION
SELECT id, b AS name
FROM names
) AS t
JOIN names AS n ON t.id = n.id
WHERE name IS NOT NULL
ORDER BY name, n.id
One approach:
SELECT s.ColumnA AS digest_name
, s.ID
, s.ColumnA
, s.ColumnB
FROM mydata s
WHERE s.ColumnA IS NOT NULL
UNION ALL
SELECT t.ColumnB
, t.ID
, t.ColumnA
, t.ColumnB
FROM mydata t
WHERE t.ColumnB IS NOT NULL
ORDER BY 1, 3, 4
One query gets all the rows where ColumnA is not null, the second query gets the rows where Column B is not null. (This effectively omits rows where the digest name would have been NULL.)
The UNION ALL set operator combines the two sets of rows into a single set. (NOTE: We prefer the more efficient UNION ALL operator over the UNION operator, because it doesn't perform the extra work of identifying and removing duplicate rows, when that operation isn't required.)
The ORDER BY specifies that the rows are to be ordered by the digest name first, then by the names in ColumnA and ColumnB.
This produces ten rows of output, and includes row:
digest Id ColumnA ColumnB
-------- -- -------- --------
Adam 4 NULL Adam
It's not clear why this row is omitted from the resultset specified in the question. (That resultset in the question shows only nine rows.)
So, I'm not sure if I'm missing something about the requirement. It's entirely possible I've not understood the specification.
I have a table that looks like this
Id FirstName
5 Adam
6 Bob
8 Bob
5 Carl
5 Dewie
8 Ernest
When two rows have the same Id, I'd like to keep only one of them. On this example, I would obtain
Id FirstName
5 Adam
6 Bob
8 Bob
Is there concise command to that? I was thinking of
SELECT * FROM Persons HAVING(COUNT(Id)=1)
or
SELECT DISTINCT(Id), FirstName FROM Persons
but my syntax isn't correct.
Hope you are looking for this::
SELECT * from Persons GROUP BY Id
SELECT Id, MIN(FirstName)
FROM Persons
GROUP By Id
Your DISTINCT query will also work, you just need to add GROUP BY id
SELECT DISTINCT(Id), FirstName
FROM Persons
GROUP BY id;
Demo
I'd like to have a query for the first and last name on an enrollment list so that only one result show. However, if only the last name is chosen in the query multiple answers will show.
You can GROUP BY the last name field in your query.
For example if you had this data:
MyTable:
id last_name first_name
1 Smith John
2 Smith Jane
3 Jones Paul
Running a query like this:
SELECT t.last_name
FROM MyTable t
GROUP BY t.last_name
ORDER BY t.last_name
...would return these two rows:
Jones
Smith