0

I have a table as follows

SeriesVariables_ID     SeriesVariables_Label     SeriesVariables_Value    Series_ID
143                    Batch Number              Test20PassAll            28
144                    SerialNo                  SIMTEST                  28
145                    Seg Pull Date             20/1/2014                28
146                    Seg Pull Time             101010                   28
147                    Batch Number              Test20PassAll            28
148                    SerialNo                  SIMTEST                  28
149                    Seg Pull Date             20/1/2014                28
150                    Seg Pull Time             101010                   28
151                    Batch Number              Test20PassAll            7
152                    SerialNo                  SIMTEST2                 7
153                    Seg Pull Date             20/2/2014                7
154                    Seg Pull Time             202020                   7

What I would like is to keep the order of the table and remove the duplicates. The result will look something like this

SeriesVariables_ID     SeriesVariables_Label     SeriesVariables_Value    Series_ID
143                    Batch Number              Test20PassAll            28
144                    SerialNo                  SIMTEST                  28
145                    Seg Pull Date             20/1/2014                28
146                    Seg Pull Time             101010                   28
151                    Batch Number              Test20PassAll            7
152                    SerialNo                  SIMTEST2                 7
153                    Seg Pull Date             20/2/2014                7
154                    Seg Pull Time             202020                   7

Any help is greatly appreciated!

TheEwook
  • 11,037
  • 6
  • 36
  • 55
Abe
  • 1,879
  • 2
  • 24
  • 39

1 Answers1

0

If you are running SQL, I've found the answer at https://stackoverflow.com/a/18949/1781207 to be very helpful for these situations. It will also keep the lowest SeriesVariables_ID as you want.

For example, in your code it should be (where "MyTable" is the name of your table)

DELETE MyTable 
FROM MyTable
LEFT OUTER JOIN (
   SELECT MIN(SeriesVariables_ID) as RowId, SeriesVariables_Label, SeriesVariables_Value, Series_ID
   FROM MyTable 
   GROUP BY SeriesVariables_Label, SeriesVariables_Value, Series_ID
) as KeepRows ON
   MyTable.SeriesVariables_ID = KeepRows.RowId
WHERE
   KeepRows.RowId IS NULL

You can test to see the selected rows by running the inner select statement alone

SELECT MIN(SeriesVariables_ID) as RowId, SeriesVariables_Label, SeriesVariables_Value, Series_ID
FROM MyTable
GROUP BY SeriesVariables_Label, SeriesVariables_Value, Series_ID
Community
  • 1
  • 1
Jem
  • 4,313
  • 2
  • 18
  • 20