X
X
xakslim2019-11-12 17:50:32
SQL Server
xakslim, 2019-11-12 17:50:32

How to copy grouped data to another table?

There is such a table: it is
5dcac4a36f51b344238252.png
necessary to copy the rows to another table, while it must be taken into account that the objectid field must be maximum for a certain person (unique personnelnumber field).
The second table should look like this:
5dcac6a2ce92b047926370.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-11-12
@xakslim

INSERT INTOSELECTINNER JOIN ( SELECT personnelnumber, MAX(objectid) … GROUP BY personnelnumber ) …
Every day is the same.
Inserting data from other tables .

E
Elvis, 2019-11-12
@Dr_Elvis

more or less like this:

INSERT INTO newtablename 
(SELECT id, objectid, lastname, firstname, middlename, position, status, personnelnumber, organization_id
FROM tablename AS t1
WHERE t1.objectid = (SELECT max( t2.objectid ) FROM tablename AS t2 WHERE t1.personnelnumber = t2.personnelnumber)
GROUP BY personnelnumber, id, objectid, lastname, firstname, middlename, position, status, organization_id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question