vithika vithika - 8 days ago 5
SQL Question

How to update date column in partitions of n rows?

I am trying to update my date field.

Table structure is like:

date id
2016-11-14 1
2016-11-14 2
2016-11-14 3
2016-11-14 4
-
-
-
2016-11-14 100


How to update first ten records with different date, second ten records with different date and so on?

Answer

You could use a CASE

  UPDATE yourTable
  SET "date" = CASE WHEN id <= 10 then '2016-11-01'::timestamp 
                    WHEN id <= 20 then '2016-11-02'::timestamp 
                    ....
                    WHEN id <= 100 then '2016-11-10'::timestamp 
               END;
Comments