Insert only the highest value with the same identifier

I am inserting a text file into the mssql database and this works. But I can not find a solution, if I have a 2-line with the same [bar_id], [time]and [Q]that it needs to insert the highest in dQ / dt table.

Help is appreciated.

This is a table.

bar_ID      time    dQ/dt   Q

barcode1    0:02:44 125,24  4,57 -- this one needs to insert

barcode1    0:02:44 125,11  4,57 -- this one must be deleted or skipped i need to
+4
source share
1 answer

You can import all the data into a table, and then use the query to filter duplicate data as follows:

select bar_ID, time, max([dQ/dt]) as [dQ/dt], Q from yourtable1
group by bar_ID, time, Q

If you want, you can transfer duplicates of free data to another table in the same way:

select bar_ID, time, max([dQ/dt]) as [dQ/dt], Q into yourtable2 from yourtable1
group by bar_ID, time, Q

If you already have a target table, you can use this query

insert into yourTargetTable(bar_ID, time, [dQ/dt], Q)
select bar_ID, time, max([dQ/dt]), Q from yourtable1
group by bar_ID, time, Q
+1
source

Source: https://habr.com/ru/post/1694353/


All Articles