user2171512
user2171512

Reputation: 561

Insert 3 million rows of data from one table to other in SQL server

I have one AuditPlayers and AuditPlayersHistorytable from which i want to insert data to one new table, for example table name will be Players.

In Auditplayers table I have a lot of players and some dates. I want to insert from AuditPlayers & AuditPlayersHistory all players with their max(Date) into my new table Players.

How can i do this ? Is better with batch or without? How to do with batch?

Example of my AuditPlayers table:

PlayerId Date
1        2019-01-01
1        2019-02-01
100      2019-08-01

Data in new Players table should be

PlayerId Date
1        2019-02-01
100      2019-08-01

Upvotes: 0

Views: 554

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

I think you want something like this:

select playerId, max(date) as date
into players
from ((select playerId, date as date
       from AuditPlayers
      ) union all
      (select playerId, date as date
       from AuditPlayersHistorytable
      )
     ) p
group by playerId;

You can use insert if Players already exists.

Upvotes: 0

DineshDB
DineshDB

Reputation: 6193

The Simple MAX and GROUP BY solve your problem

SELECT PlayerID, MAX(Date)Date
FROM YourTable
GROUP BY PlayerID

Upvotes: 1

Related Questions