0

have a duplicate fields and i need to update only one row how do i do this with sql 2005?

my database is as seen below:

+----------------+-------+-------------+------------------+---------------+
| Transaction_no | User  |  Check-In   |    Check-Out     |    barcode    |
+----------------+-------+-------------+------------------+---------------+
| 01-2013        | User1 |  --/--/--   | 12/28/2013 11:10 | APH009300L030 |
| 01-2013        | User1 |  --/--/--   | 12/28/2013 11:10 | APH009300L030 |
| 01-2013        | User1 |  --/--/--   | 12/28/2013 11:10 | APH009300L030 |
| 01-2013        | User1 |  --/--/--   | 12/28/2013 11:10 | APH009300L030 |
+----------------+-------+-------------+------------------+---------------+
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
Ron
  • 49
  • 3
  • 8
  • Please correct the format.. – Mudassir Hasan Dec 28 '13 at 04:37
  • sorry i'm just new here and a newbiee in sql programming – Ron Dec 28 '13 at 04:40
  • can you try using update query – Nagaraj S Dec 28 '13 at 04:41
  • if i update this 01-2013 | User1 | --/--/-- | 12/28/2013 11:10 | APH009300L030 all of the other data will be updated since there is no unique identifier for each of them – Ron Dec 28 '13 at 04:50
  • Please add primary key in your table – Nagaraj S Dec 28 '13 at 05:17
  • do you hv any specific criteria for selecting any one row among duplicate ?also there is always some pattern for duplicate data also. your sample data do not follow any pattern.Because of which in @Vigness query all column has been use in partition.you are not clear. Why in first place there is duplicate ?why no validation before insert ? – KumarHarsh Dec 29 '13 at 14:11
  • i did not add any validation during insert because there are certain cases that multiple same item may be check out at once. I cannot do it by quantity because what if i check out 3 items and returned only 2 then someone will checkout again that type of item. then that one that hasn't been checked in will be difficult to trace (sory for my bad english) but as they suggest i added a primary column which will act as a unique identifier for the duplicates. – Ron Jan 03 '14 at 09:56

3 Answers3

0

Try Like this

;WITH NumberedTbl AS 
 (
  SELECT ROW_NUMBER() OVER (
    PARTITION User,Check-In,Check-Out,barcode,Transaction_no
    ORDER BY User,Check-In,Check-Out,barcode,Transaction_no
  ) AS RowNumber,
  User,Check-In,Check-Out,barcode,Transaction_no
  FROM tbl
)

UPDATE T
SET Check-In= GETDATE()
FROM NumberedTbl T
WHERE T.RowNumber = 1

SELECT * FROM tbl

Please see Demo here

Source

Community
  • 1
  • 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • i already tried the code above but when i update the field it updates all the others since there are no unique identifier – Ron Dec 28 '13 at 04:48
  • Still doesn't work maybe adding a new field as a unique identifier maybe its really impossible to get it without an identifier – Ron Dec 28 '13 at 05:02
  • Still id dint work means, you need to add one Primay key to your table. This is my suggestion – Vignesh Kumar A Dec 28 '13 at 05:03
  • yes i added a primary key. thanks for the help :) (sorry for the late reply i was out on vacation ) – Ron Jan 03 '14 at 09:58
0

What you need in your table is a primary key column. You can just add an identity field. Then you'll be able to easily write queries like the one you need.

Z .
  • 12,657
  • 1
  • 31
  • 56
0

RAnking functions like row_number , rank , dense_rank wont work as all th field values are same and applying ORDER BY on any column after partitioninh will generate same rank for all rows.

Instead add another column habing unique values eg Identity column qfter which you can use this columm in ORDER By clause.

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133